Mixins / foldkit-mixins/theme

Theme

namespace · packages/mixins/src/theme.ts:117

const Theme = {
  ...Core.Theme,
  root,
  scoped,
  tokens,
  standard,
  ladder,
  role,
  nested,
  nests,
  concentric,
  raise,
  raisedSurface,
  breakpointWidths,
  inContainer,
  make,
  set,
  describe,
  variables,
  tokensOf,
  print,
  fonts,
  fluidType,
  fluidRatio,
  containerFluidProgress,
  containerFluidRatio,
  scope,
  schemeScript,
  chooseScheme,
  currentScheme,
  knobScript,
  chooseKnobs,
}

In the guides

Members

Theme.define

function · packages/mixins/src/theme/core.ts:16

define<T extends ThemeTokens>(tokens: T): Readonly<T>

Theme.ref

function · packages/mixins/src/theme/core.ts:35

ref<T extends ThemeTokens>(theme: T): Refs<T>

The theme's tokens as references: Theme.ref(theme).surface.overt is var(--fk-surface-overt), and a group or name the theme lacks is a type error. Only the names are read, never the values. Built once per theme.

Theme.variables

function · packages/mixins/src/theme/knobs.ts:722

variables<S extends KnobSet>(self: KnobTheme<S>): Knob.Variables

Every knob variable self declares: the values it sets, every other knob's unset value, and the sRGB boundary of each palette hue they place.

Theme.lightDark

function · packages/mixins/src/theme/core.ts:69

lightDark(light: string, dark: string): string

A token that follows the user's color scheme with CSS light-dark(): no JavaScript and no Model field. A theme the user chooses is the other case, and belongs in the Model as a class on the root; variables compiles both.

Theme.compose

function · packages/mixins/src/theme/core.ts:82

compose<A extends ThemeTokens, B extends ThemeTokens>(
  base: A,
  over: B,
): Readonly<Merge<A, B>>

Themes merged at definition time, later tokens winning within a group.

Theme.root

function · packages/mixins/src/theme/root.ts:32

root(theme: ThemeTokens, options?: RootOptions): StyleValue

Theme.scoped

function · packages/mixins/src/theme/root.ts:55

scoped<T extends ThemeTokens>(
  _theme: T,
  selector: string,
  overrides: Overrides<T>,
): StyleValue

Token overrides under selector: a named theme the Model selects with a root attribute (:root[data-theme="ocean"]), or a scheme the user chose (:root[data-color-scheme="dark"]). Which one is active is the Model's fact; this only says what it means. On the root element an overridden knob re-derives every token; under a subtree it changes only the tokens it overrides itself, since a token resolves where it is declared: scope a theme to a subtree with Theme.scope, which declares the derivations there too.

theme is the theme being overridden and is read only for its type, so a group or token it lacks is a type error rather than a variable nothing reads.

Theme.tokens

const · packages/mixins/src/theme/tokens.ts:42

tokens: Readonly<{ knob: { "relief-raised": string; "relief-pressed": string; "relief-field": string; "shadow-inset": string; "shadow-xs": string; "shadow-sm": string; "shadow-md": string; "shadow-lg": string; "shadow-xl": string; "shadow-2xl": string; "glyph-width": string; "icon-scale": string; ease: string; "reading-align": string; "body-wrap": string; "heading-wrap": string; measure: string; "fluid-viewport-max": string; "fluid-viewport-min": string; fluid: string; "heading-tracking": string; "heading-weight": string; "font-heading": string; "font-body": string; "border-width": string; leading: string; "heading-scale": string; "text-scale": string; "shadow-strength": string; "control-round": string; step: string; travel: string; motion: string; "radius-factor": string; density: string; }; fluid: { ratio: string; progress: string; }; space: { '3xs': string; '2xs': string; xs: string; sm: string; md: string; lg: string; xl: string; '2xl': string; '3xl': string; }; radius: { xs: string; sm: string; md: string; lg: string; xl: string; full: string; control: string; }; font: { body: string; heading: string; mono: string; }; size: { xs: string; sm: string; md: string; lg: string; xl: string; '2xl': string; '3xl': string; '4xl': string; }; leading: { tight: string; snug: string; normal: string; relaxed: string; }; weight: { normal: string; medium: string; semibold: string; bold: string; heading: string; }; tracking: { heading: string; }; motion: { fast: string; moderate: string; slow: s …;

Theme.standard

function · packages/mixins/src/theme/themes.ts:37

standard(theme?: KnobTheme<Standard>): StandardTokens

Every shipped token with theme's knob values, for a page's root: the derivations are the same for every theme, so only the knobs differ. With no theme, every knob at its default.

Theme.ladder

function · packages/mixins/src/theme/tokens.ts:307

ladder(name: Rung): string

A control's dimension on the size ladder: name's default end blended toward its compact one by the step knob, and spacing multiplied by density. The blend is written here, not in a token, because a custom property resolves where it is declared: a token would read the step at :root, while this expression reads it on the element that uses it, so --fk-knob-step: 1 on a toolbar makes everything in it compact, a menu it opens included while the panel stays in the page (an anchor's portal: false); a portaled panel takes only the size its recipe chooses. A step of -0.5 is shadcn's large size, 40px.

Theme.role

function · packages/mixins/src/theme/tokens.ts:391

role(
  name: FluidRole,
  options: {
    readonly fluid: true
  },
): {
  readonly fontSize: string
  readonly lineHeight: string
}
role(name: Role): {
  readonly fontSize: string
  readonly lineHeight: string
}

Text in role: its size and leading on the size ladder, read on the element like ladder, so a compact region's text steps down with its controls. A display, title or subtitle may also grow with { fluid: true }, its leading growing with its size.

Theme.nested

function · packages/mixins/src/theme/tokens.ts:326

nested(): string
nested(outer: string, inset: string, border?: string): string

The radius of a rounded surface inset in another, so their corners are concentric: the outer radius less the inset between them and the outer surface's border, never below zero. Past a large radius, or with uneven gaps, the two read as independent and may be tuned by eye.

With no arguments, it reads what the nearest container that nests its children published, so a row, a pill or a highlight inside a padded panel follows the panel's corner without repeating its arithmetic. The values inherit, so it reads the nearest ancestor that nests, however deep and in whatever component; it is square only where no ancestor does.

Theme.nests

function · packages/mixins/src/theme/tokens.ts:341

nests(
  radius: string,
  inset: string,
  border?: string,
): {
  readonly '--fk-outer-radius': string
  readonly '--fk-inset': string
  readonly '--fk-control-corner': string
}

What a container that pads its children publishes for nested() and concentric(): its corner inside its border, the inset to them, and the corner a control there takes. Spread it beside the container's own radius and padding. The values are read on each child, so a container deeper in that nests again publishes its own.

Theme.concentric

function · packages/mixins/src/theme/tokens.ts:367

concentric(bounds: { readonly min: string; readonly max: string }): string

A control's corner, concentric with the nearest container that nests (a card, a dialog, a toast) and kept between min and max: a deep inset would square it off, and a wide corner round it past what a control is drawn with. Outside every such container it is max. Like nested(), it reads what that ancestor published, so it is written in the control's own rule; as a token it would resolve once, at the root.

A control that nests its own parts (a segmented tray) reads this and publishes from it on one element, a cycle that voids both: after nests, it sets '--fk-control-corner': 'inherit', passing its container's on.

Theme.raise

function · packages/mixins/src/theme/raise.ts:43

raise(offset: number): StyleValue

An element offset levels above the raised element holding it, or the page: popovers and menus are 2, dialogs 4. Paint it with raisedSurface.

Theme.raisedSurface

function · packages/mixins/src/theme/raise.ts:55

raisedSurface(): string

The background of a raised element at its level, from the palette's base surface. In a light scheme the first two steps go toward white and the rest stay there, so shadow alone carries elevation; in a dark scheme each level is an even step lighter (knob.raise-dark). Written as an expression, not a token, because the level is the element's own and a token resolves at :root. In OKLab, so the base keeps its tint whatever space it was written in: Chrome 157 reads no hue from a color of little chroma that is not OKLCH.

Theme.breakpointWidths

function · packages/mixins/src/theme/tokens.ts:425

breakpointWidths(theme: {
  readonly breakpoint: {
    readonly [name: string]: string
  }
}): Readonly<Record<string, number>>

Each breakpoint query's min-width in pixels (rem at 16), so CSS and the Model agree on what md means. A query that is not a plain min-width is refused: it has no single threshold. A container's is its container's.

Theme.inContainer

function · packages/mixins/src/theme/tokens.ts:453

inContainer<Breakpoints extends Readonly<Record<string, string>>>(
  name: string,
  breakpoints: Breakpoints,
): {
  readonly [K in keyof Breakpoints]: string
}

The same breakpoints measured on a named container rather than the window: Theme.inContainer('page', Theme.tokens.breakpoint) gives md as '@container page (min-width: 48rem)', for Style.responsive and a responsive look. An element must be that container (container: page / inline-size) for them to match.

Theme.make

function · packages/mixins/src/theme/knobs.ts:686

make(values: Values<Standard>): KnobTheme<Standard>
make<S extends KnobSet>(knobs: S, values: Values<NoInfer<S>>): KnobTheme<S>

A theme of the standard knobs, or of knobs: the values it sets, checked, every other knob keeping its default. A misspelt knob or a choice outside its options is a type error; a number out of its range throws here.

Theme.set

function · packages/mixins/src/theme/knobs.ts:696

set<S extends KnobSet>(
  values: Values<NoInfer<S>>,
): (self: KnobTheme<S>) => KnobTheme<S>

theme with values over its own, group by group.

Theme.describe

function · packages/mixins/src/theme/knobs.ts:708

describe(meta: {
  readonly title?: string
  readonly description?: string
}): <S extends KnobSet>(self: KnobTheme<S>) => KnobTheme<S>

A title and a description for a list of themes; neither is the theme's identity.

Theme.tokensOf

function · packages/mixins/src/theme/knobs.ts:748

tokensOf<S extends KnobSet>(
  self: KnobTheme<S>,
): Readonly<{ knob: Readonly<Record<string, string>> }>

self's knob variables as tokens, for Theme.root and Theme.scoped.

Theme.print

function · packages/mixins/src/theme/knobs.ts:796

print<S extends KnobSet>(self: KnobTheme<S>): string

The source of self as Theme.make({ … }): each knob it sets that writes other than the knob's default does, a group to a line (a knob a line past a hundred characters), so the code an application pastes says only what makes the theme its own.

Theme.fonts

const · packages/mixins/src/theme/fonts.ts:6

fonts: {
  readonly system: 'system-ui, sans-serif'
  readonly geist: '"Geist", ui-sans-serif, system-ui, sans-serif'
  readonly roboto: 'Roboto, "Segoe UI", system-ui, sans-serif'
  readonly serif: 'ui-serif, Georgia, "Times New Roman", serif'
  readonly mono: '"Geist Mono", ui-monospace, monospace'
  readonly rounded: 'ui-rounded, "SF Pro Rounded", Nunito, system-ui, sans-serif'
  readonly classic: '"MS Sans Serif", "Microsoft Sans Serif", Tahoma, Verdana, sans-serif'
}

Font stacks a type.body or type.heading knob may name: each a family and the fallbacks a page without it draws in. A knob takes any stack; these are the ones /create offers.

Theme.fluidType

function · packages/mixins/src/theme/fluid.ts:93

fluidType(options: FluidTypeOptions): string

min at the small viewport, max at the large one, by the theme's fluid strength: calc(min + (max - min) * var(--fk-fluid-progress)). A still theme (type.fluid: 0) gives min everywhere.

Theme.fluidRatio

function · packages/mixins/src/theme/fluid.ts:73

fluidRatio(viewport: Viewport): string

The ratio for a checked px range; see fluidRatioOf.

Theme.containerFluidProgress

function · packages/mixins/src/theme/fluid.ts:69

containerFluidProgress(): string

containerFluidRatio() at the theme's strength, for --fk-fluid-progress on a container's content.

Theme.containerFluidRatio

function · packages/mixins/src/theme/fluid.ts:65

containerFluidRatio(): string

The theme's range measured on the nearest inline-size container.

Theme.scope

function · packages/mixins/src/theme/themes.ts:96

scope(theme: KnobTheme<Standard>, selector: string): StyleValue

theme on the elements selector matches, below the root: its knobs and every derivation again. A token resolves where it is declared, so knobs alone under a subtree would change nothing derived from them there.

Theme.schemeScript

function · packages/mixins/src/theme/scheme.ts:26

schemeScript(key?: string): string

The text of a <script> for the document's head, before any stylesheet draws: it applies the scheme kept under key, so a reader who chose dark never sees one light frame. Storage that throws (a private window) or holds anything else leaves the stylesheet's choice.

Theme.chooseScheme

function · packages/mixins/src/theme/scheme.ts:41

chooseScheme(scheme: Scheme, key?: string): Effect.Effect<void>

Applies scheme to the root element and keeps it under key for the next visit, as a Command's effect. 'system' removes both. Storage that throws still applies the scheme to this page.

Theme.currentScheme

function · packages/mixins/src/theme/scheme.ts:30

currentScheme(): Scheme

The scheme the root element shows now: what schemeScript or chooseScheme applied.

Theme.knobScript

function · packages/mixins/src/theme/knobsKept.ts:56

knobScript({ key = KNOBS_KEY, selector = ':root' }?: KnobsPlace): string

The text of a <script> for the document's head: it applies the knobs kept under key to selector, so a reader who made a theme never sees the default for a frame. Storage that throws, an entry of another format, and anything that is not a knob variable are left out.

Theme.chooseKnobs

function · packages/mixins/src/theme/knobsKept.ts:90

chooseKnobs<S extends KnobSet>(
  theme: KnobTheme<S>,
  { base, key = KNOBS_KEY, selector = ':root' }: KnobsChoice<S>,
): Effect.Effect<void>

Applies theme's knobs to selector and keeps them under key for the next visit, as a Command's effect: the knobs that differ from base, so the base theme writes and keeps nothing. Storage that throws still applies them to this page.