Tokens

Open in playground

import { reset } from "@cssints/reset" with { type: "cssints" };
import * as css from "cssints" with { type: "cssints" };

reset();

// A token is a typed value that prints as var(--name). A name makes it settable; without one it is private to the file.
const brand = css.token.color("#2563eb", "brand");
const gap = css.token.length("12px", { var: "swatch-gap", inherit: false });
const ink = css.token.color("#0f172a");

const swatches = css.cn(css.flex().wrap("wrap").gap(gap), css.p(4));
const swatch = css.cn(
  css.width("6rem"),
  css.height("6rem"),
  css.borderRadius("12px"),
  css.bg(brand),
  css.color("white"),
  css.flex().alignItems("flex-end").justifyContent("flex-start"),
  css.p(2),
  css.fontSize("0.8125rem"),
  css.border("2px solid", ink),
);

// token.set() and style() give an inline style for one element: the whole subtree follows the new value
const palette = [
  ["brand", undefined],
  ["rose", css.style(css.token.set(brand, "#e11d48"))],
  ["amber", css.style(css.token.set(brand, "#d97706"))],
  ["teal", css.style(css.token.set(brand, "#0d9488"))],
] as const;

export default function App() {
  return (
    <div className={swatches}>
      {palette.map(([name, style]) => (
        <div className={swatch} style={style}>
          {name}
        </div>
      ))}
    </div>
  );
}

A token is a Typed OM value whose toString() is var(--name). It fits anywhere a value of its type fits: color(blue), border("1px solid", blue). In math and in other CSS functions it is a part of a call: calc(t.space.md, "*", 2), var(t.space.sm, "4px") (see "Functions").

import * as import csscss from "cssints" with { type: "cssints" };

const const blue: Token<"color", true, string>blue = import csscss.
const token: {
    readonly number: TokenFn<"number">;
    readonly color: TokenFn<"color">;
    readonly length: TokenFn<"length">;
    readonly percentage: TokenFn<"percentage">;
    readonly integer: TokenFn<"integer">;
    readonly angle: TokenFn<"angle">;
    readonly time: TokenFn<"time">;
    readonly resolution: TokenFn<"resolution">;
    readonly url: TokenFn<"url">;
    readonly image: TokenFn<"image">;
    readonly shadow: TokenFn<"shadow">;
    readonly lengthPercentage: TokenFn<"lengthPercentage">;
    readonly customIdent: TokenFn<...>;
    readonly transformList: TokenFn<...>;
    readonly easingFunction: TokenFn<...>;
} & {
    ...;
}
`token.<type>(initial, name?)`: a Typed OM value of the type, `var(--name)`. With a name it can be set.
token
.color: <"blue", "color-primary">(initial: "blue", name?: "color-primary" | undefined) => Token<"color", true, string>color("blue", "color-primary"); // --color-primary, can be set
const const gap: Token<"length", true, string>gap = import csscss.
const token: {
    readonly number: TokenFn<"number">;
    readonly color: TokenFn<"color">;
    readonly length: TokenFn<"length">;
    readonly percentage: TokenFn<"percentage">;
    readonly integer: TokenFn<"integer">;
    readonly angle: TokenFn<"angle">;
    readonly time: TokenFn<"time">;
    readonly resolution: TokenFn<"resolution">;
    readonly url: TokenFn<"url">;
    readonly image: TokenFn<"image">;
    readonly shadow: TokenFn<"shadow">;
    readonly lengthPercentage: TokenFn<"lengthPercentage">;
    readonly customIdent: TokenFn<...>;
    readonly transformList: TokenFn<...>;
    readonly easingFunction: TokenFn<...>;
} & {
    ...;
}
`token.<type>(initial, name?)`: a Typed OM value of the type, `var(--name)`. With a name it can be set.
token
.
length: <"16px", {
    readonly var: "gap";
    readonly inherit: false;
}>(initial: "16px", name?: {
    readonly var: "gap";
    readonly inherit: false;
} | undefined) => Token<"length", true, string>
length
("16px", { var: "gap"var: "gap", inherit: falseinherit: false });
const const muted: Token<"color", false, string>muted = import csscss.
const token: {
    readonly number: TokenFn<"number">;
    readonly color: TokenFn<"color">;
    readonly length: TokenFn<"length">;
    readonly percentage: TokenFn<"percentage">;
    readonly integer: TokenFn<"integer">;
    readonly angle: TokenFn<"angle">;
    readonly time: TokenFn<"time">;
    readonly resolution: TokenFn<"resolution">;
    readonly url: TokenFn<"url">;
    readonly image: TokenFn<"image">;
    readonly shadow: TokenFn<"shadow">;
    readonly lengthPercentage: TokenFn<"lengthPercentage">;
    readonly customIdent: TokenFn<...>;
    readonly transformList: TokenFn<...>;
    readonly easingFunction: TokenFn<...>;
} & {
    ...;
}
`token.<type>(initial, name?)`: a Typed OM value of the type, `var(--name)`. With a name it can be set.
token
.color: <"#6b7280", undefined>(initial: "#6b7280", name?: undefined) => Token<"color", false, string>color("#6b7280"); // --_<hash>, private
export const const A: () => anyA = () => <IntrinsicElements[string]: anydiv className?: string | undefinedclassName={import csscss.function cn(...styles: css.Style[]): css.Style (+1 overload)
Merges styles into one class string. What is known at build time merges (the later property wins); a runtime value (`on && p(4)`, `props.class`, a state call) is joined when the code runs, and a style in it replaces the classes it overrides while it is active.
cn
(import csscss.const color: <readonly [Token<"color", true, string>]>(parts_0: Token<"color", true, string>) => css.Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/color)
color
(const blue: Token<"color", true, string>blue), import csscss.const gap: <readonly [Token<"length", true, string>]>(parts_0: Token<"length", true, string>) => css.Style
`<'row-gap'> <'column-gap'>?`. Chrome 57, Edge 16, Firefox 52, Safari 10.1, iOS 10.3, Android 57. [MDN](https://developer.mozilla.org/docs/Web/CSS/gap)
gap
(const gap: Token<"length", true, string>gap), import csscss.const color: <readonly [Token<"color", false, string>]>(parts_0: Token<"color", false, string>) => css.Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/color)
color
(const muted: Token<"color", false, string>muted))} />;
export const const B: () => anyB = () => <IntrinsicElements[string]: anydiv style?: Record<string, string> | undefinedstyle={import csscss.const style: (...styles: (css.Style | StyleObject)[]) => StyleObject
css values (declarations, no conditions) and `token.set()` results, as an inline `style` object.
style
(import csscss.const color: <readonly [Token<"color", true, string>]>(parts_0: Token<"color", true, string>) => css.Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/color)
color
(const blue: Token<"color", true, string>blue), import csscss.
const token: {
    readonly number: TokenFn<"number">;
    readonly color: TokenFn<"color">;
    readonly length: TokenFn<"length">;
    readonly percentage: TokenFn<"percentage">;
    readonly integer: TokenFn<"integer">;
    readonly angle: TokenFn<"angle">;
    readonly time: TokenFn<"time">;
    readonly resolution: TokenFn<"resolution">;
    readonly url: TokenFn<"url">;
    readonly image: TokenFn<"image">;
    readonly shadow: TokenFn<"shadow">;
    readonly lengthPercentage: TokenFn<"lengthPercentage">;
    readonly customIdent: TokenFn<...>;
    readonly transformList: TokenFn<...>;
    readonly easingFunction: TokenFn<...>;
} & {
    ...;
}
`token.<type>(initial, name?)`: a Typed OM value of the type, `var(--name)`. With a name it can be set.
token
.set: <"color", "red">(token: Token<"color", true, string>, value: "red") => StyleObjectset(const blue: Token<"color", true, string>blue, "red"))} />; // { "--color-primary": "red", color: "var(--color-primary)" }
import csscss.
const token: {
    readonly number: TokenFn<"number">;
    readonly color: TokenFn<"color">;
    readonly length: TokenFn<"length">;
    readonly percentage: TokenFn<"percentage">;
    readonly integer: TokenFn<"integer">;
    readonly angle: TokenFn<"angle">;
    readonly time: TokenFn<"time">;
    readonly resolution: TokenFn<"resolution">;
    readonly url: TokenFn<"url">;
    readonly image: TokenFn<"image">;
    readonly shadow: TokenFn<"shadow">;
    readonly lengthPercentage: TokenFn<"lengthPercentage">;
    readonly customIdent: TokenFn<...>;
    readonly transformList: TokenFn<...>;
    readonly easingFunction: TokenFn<...>;
} & {
    ...;
}
`token.<type>(initial, name?)`: a Typed OM value of the type, `var(--name)`. With a name it can be set.
token
.set: <"color", "red">(token: Token<"color", true, string>, value: "red") => StyleObjectset(muted, "red"); // error: private tokens cannot be set
Argument of type 'Token<"color", false, string>' is not assignable to parameter of type 'Token<"color", true, string>'. Type 'false' is not assignable to type 'true'.
@property --color-primary {
  syntax: "<color>";
  inherits: true;
  initial-value: blue;
}