Trousers

@trousers/core

@trousers/core

Default export

Source

Define static styles that the bundler plugin extracts into CSS. Apply the result with classnames, styled, or the JSX css prop. Use React's style prop for runtime values. Give the style definition a readable prefix in the emitted class names.

ts
function css(styles: CSSObject): CollectorReturn;
function css(id: string, styles: CSSObject): CollectorReturn;

Example

ts
const button = css('button', { borderRadius: 8 })
    .modifier('primary', { backgroundColor: '#f95b5b' });

classnames

Source

Resolve compiled styles and active selections into a string for className. Use selection keys without the $ prefix. Boolean modifiers activate on true, and named variants activate when the value matches a choice.

ts
function classnames<Styles extends CompiledStyles<VariantOptions>>(styles: Styles | false | null | undefined, selections?: Readonly<Record<string, VariantSelection>> & VariantSelections<Styles>): string;

Example

ts
classnames(button, { primary: true, size: 'm' });

Collector

Source

The overloaded function type of css.

ts
export type Collector = typeof css;

CollectorReturn

Source

A style definition. Chain these methods directly on css() so the compiler can extract them.

ts
export interface CollectorReturn<Variants extends VariantOptions = Record<never, never>> extends CompiledStyles<Variants> {
    modifier(id: string, styles: CSSObject): CollectorReturn<Variants>;
    variant<Name extends string, Choices extends Readonly<Record<string, CSSObject>>>(name: Name, choices: Choices): CollectorReturn<Variants & Record<Name, `${Extract<keyof Choices, string | number>}`>>;
    global(styles: CSSObject): CollectorReturn<Variants>;
    theme(theme: ThemeObject): CollectorReturn<Variants>;
}
modifier

Add styles enabled by a boolean flag, such as $selected in JSX or selected in classnames.

variant

Add a finite set of named choices. Omitting a choice leaves the base styles active.

global

Emit unscoped rules, such as a reset or @font-face, when this module is included in the build.

theme

Flatten tokens into CSS custom properties scoped to the generated class.

CompiledStyles

Source

The class names and selections produced by the Trousers compiler.

ts
export interface CompiledStyles<Variants extends VariantOptions = Record<never, never>> {
    readonly classes: readonly string[];
    readonly modifiers: readonly (readonly [prop: string, className: string])[];
    readonly variants?: readonly (readonly [
        prop: string,
        choice: string,
        className: string
    ])[];
}
classes

Class names generated by the Trousers bundler plugin.

modifiers

Pairs of JSX modifier props and the classes they enable.

variants

JSX variant props, their choices, and the corresponding class names.

Define static styles that the bundler plugin extracts into CSS. Apply the result with classnames, styled, or the JSX css prop. Use React's style prop for runtime values. Give the style definition a readable prefix in the emitted class names.

ts
function css(styles: CSSObject): CollectorReturn;
function css(id: string, styles: CSSObject): CollectorReturn;

Example

ts
const button = css('button', { borderRadius: 8 })
    .modifier('primary', { backgroundColor: '#f95b5b' });

CSSObject

Source

Static CSS declarations, fallback arrays, nested selectors, and at-rules.

ts
export interface CSSObject extends CSSPropertiesWithFallbacks, CSSPseudos, CSSOthersObject {
}

Definition

Source

Describes a style rule and how it is scoped.

ts
export interface Definition {
    id: string;
    type: 'element' | 'modifier' | 'theme' | 'global';
    className: string;
    styles: CSSObject;
}

hyphenate

Source

Convert a camel-cased property to CSS syntax, preserving custom properties.

ts
function hyphenate(property: string): string;

isBrowser

Source

Check whether the current environment provides a browser document.

ts
function isBrowser(): boolean;

namespace

Source

Expand nested selectors in a style object under the supplied selector.

ts
function namespace(id: string, style: Record<string, any>): RuleSet;

Format a CSS declaration and append applicable vendor-prefixed forms.

ts
const prefix: (prop: string, value: string | number) => string;

process

Source

Serialise a style object into a map of selectors and CSS declaration strings.

ts
function process(id: string, styles: CSSObject): ProcessedRuleSet;

ThemeObject

Source

A nested token tree that .theme() flattens into CSS custom properties.

ts
export interface ThemeObject {
    [key: string]: ThemeValue;
}

themify

Source

Flatten nested theme tokens into a map of CSS custom properties.

ts
function themify(theme: ThemeObject | readonly ThemeValue[], prefix?: string): Record<string, string | number>;

VariantOptions

Source

Map of variant names to their allowed string choices.

ts
export type VariantOptions = Readonly<Record<string, string>>;

VariantSelection

Source

A boolean modifier flag, named variant choice, or omitted selection.

ts
export type VariantSelection = boolean | string | undefined;