@trousers/core
@trousers/core
Default export
SourceDefine 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.
function css(styles: CSSObject): CollectorReturn;
function css(id: string, styles: CSSObject): CollectorReturn;
Example
const button = css('button', { borderRadius: 8 })
.modifier('primary', { backgroundColor: '#f95b5b' });
classnames
SourceResolve 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.
function classnames<Styles extends CompiledStyles<VariantOptions>>(styles: Styles | false | null | undefined, selections?: Readonly<Record<string, VariantSelection>> & VariantSelections<Styles>): string;
Example
classnames(button, { primary: true, size: 'm' });
Collector
SourceThe overloaded function type of css.
export type Collector = typeof css;
CollectorReturn
SourceA style definition. Chain these methods directly on css() so the compiler can extract them.
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>;
}
modifierAdd styles enabled by a boolean flag, such as
$selectedin JSX orselectedinclassnames.variantAdd a finite set of named choices. Omitting a choice leaves the base styles active.
globalEmit unscoped rules, such as a reset or
@font-face, when this module is included in the build.themeFlatten tokens into CSS custom properties scoped to the generated class.
CompiledStyles
SourceThe class names and selections produced by the Trousers compiler.
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
])[];
}
classesClass names generated by the Trousers bundler plugin.
modifiersPairs of JSX modifier props and the classes they enable.
variantsJSX variant props, their choices, and the corresponding class names.
css
SourceDefine 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.
function css(styles: CSSObject): CollectorReturn;
function css(id: string, styles: CSSObject): CollectorReturn;
Example
const button = css('button', { borderRadius: 8 })
.modifier('primary', { backgroundColor: '#f95b5b' });
CSSObject
SourceStatic CSS declarations, fallback arrays, nested selectors, and at-rules.
export interface CSSObject extends CSSPropertiesWithFallbacks, CSSPseudos, CSSOthersObject {
}
Definition
SourceDescribes a style rule and how it is scoped.
export interface Definition {
id: string;
type: 'element' | 'modifier' | 'theme' | 'global';
className: string;
styles: CSSObject;
}
hyphenate
SourceConvert a camel-cased property to CSS syntax, preserving custom properties.
function hyphenate(property: string): string;
isBrowser
SourceCheck whether the current environment provides a browser document.
function isBrowser(): boolean;
namespace
SourceExpand nested selectors in a style object under the supplied selector.
function namespace(id: string, style: Record<string, any>): RuleSet;
prefix
SourceFormat a CSS declaration and append applicable vendor-prefixed forms.
const prefix: (prop: string, value: string | number) => string;
process
SourceSerialise a style object into a map of selectors and CSS declaration strings.
function process(id: string, styles: CSSObject): ProcessedRuleSet;
ThemeObject
SourceA nested token tree that .theme() flattens into CSS custom properties.
export interface ThemeObject {
[key: string]: ThemeValue;
}
themify
SourceFlatten nested theme tokens into a map of CSS custom properties.
function themify(theme: ThemeObject | readonly ThemeValue[], prefix?: string): Record<string, string | number>;
VariantOptions
SourceMap of variant names to their allowed string choices.
export type VariantOptions = Readonly<Record<string, string>>;
VariantSelection
SourceA boolean modifier flag, named variant choice, or omitted selection.
export type VariantSelection = boolean | string | undefined;