Runtime: cn, cv

Open in playground

import { reset } from "@cssints/reset" with { type: "cssints" };
import { bg, cn, flex, p } from "cssints" with { type: "cssints" };

import { button } from "./button.tsx";

reset();

// cn() merges at build time what it can; a runtime argument (a prop) stays a choice made when the code runs, and the later
// part replaces the earlier one it overrides: `p(4)` replaces `p(2)` while `roomy` is true.
const toolbar = (props: { roomy?: boolean; quiet?: boolean }) =>
  cn(
    flex().alignItems("center").gap(2),
    p(2),
    props.roomy && p(4),
    props.quiet ? bg("#f1f5f9") : bg("#e0e7ff"),
  );

export default function App() {
  return (
    <div className={cn(flex().direction("column").gap(3), p(4))}>
      <div className={toolbar({})}>
        <button className={button()}>Save</button>
        <button className={button({ tone: "danger", size: "lg" })}>Delete</button>
      </div>
      <div className={toolbar({ roomy: true, quiet: true })}>
        <button className={button({ disabled: true })}>Save</button>
        <button className={button({ tone: "danger", disabled: true })}>Delete</button>
      </div>
    </div>
  );
}
import {
  bg,
  borderRadius,
  cn,
  color,
  cursor,
  cv,
  fontSize,
  hover,
  opacity,
  p,
} from "cssints" with { type: "cssints" };

// cv() takes css values and returns the function you call: the props are typed from the variants.
export const button = cv({
  base: cn(p(2), borderRadius("8px"), fontSize("1rem"), cursor("pointer"), color("white")),
  variants: {
    tone: {
      primary: cn(bg("#2563eb"), hover(bg("#1d4ed8"))),
      danger: cn(bg("#dc2626"), hover(bg("#b91c1c"))),
    },
    size: { sm: fontSize("0.875rem"), lg: cn(p(4), fontSize("1.25rem")) },
    disabled: { true: opacity(0.5), false: opacity(1) },
  },
  defaultVariants: { tone: "primary", size: "sm", disabled: false },
  compoundVariants: [{ tone: "danger", disabled: true, class: opacity(0.3) }],
});
import { const bg: Prop<"background-color">
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/background-color)
bg
, function cn(...styles: Style[]): 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
, const hover: StateWrap
Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/:hover)
hover
, const p: Prop<"padding">
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
} from "cssints" with { type: "cssints" };
export const
const box: (props: {
    on: boolean;
    class?: string;
}) => string
box
= (
props: {
    on: boolean;
    class?: string;
}
props
: { on: booleanon: boolean; class?: string | undefinedclass?: string }) =>
function cn(...parts: ClassValue[]): string (+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
(p<readonly [2]>(parts_0: 2): Style
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
(2), bg<readonly ["white"]>(parts_0: "white"): Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/background-color)
bg
("white"),
props: {
    on: boolean;
    class?: string;
}
props
.on: booleanon && p<readonly [4]>(parts_0: 4): Style
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
(4), function hover(...styles: Style[]): Style
Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/:hover)
hover
(bg<readonly ["#eee"]>(parts_0: "#eee"): Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/background-color)
bg
("#eee")),
props: {
    on: boolean;
    class?: string;
}
props
.class?: string | undefinedclass);
export const const row: (kind: "a" | "b", items: boolean[]) => stringrow = (kind: "a" | "b"kind: "a" | "b", items: boolean[]items: boolean[]) => function cn(...parts: ClassValue[]): string (+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
(
p<readonly [1]>(parts_0: 1): Style
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
(1),
kind: "a" | "b"kind === "a" ? p<readonly [2]>(parts_0: 2): Style
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
(2) : bg<readonly ["red"]>(parts_0: "red"): Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/background-color)
bg
("red"),
items: boolean[]items.Array<boolean>.map<false | Style>(callbackfn: (value: boolean, index: number, array: boolean[]) => false | Style, thisArg?: any): (false | Style)[]
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@paramcallbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
map
((x: booleanx) => x: booleanx && p<readonly [3]>(parts_0: 3): Style
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
(3)),
); export const const bad: (n: number) => Stylebad = (n: numbern: number) => function cn(...styles: Style[]): 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
(p<readonly [2]>(parts_0: 2): Style
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
(2), p<readonly [number]>(parts_0: number): Style
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
(n: numbern)); // the build refuses it: css arguments must be known at build time
import { _cx as __cx } from "cssints";

export const box = (props) =>
  __cx([["_lwugp7", "_1t8gqy5"]], "_1t8gqy5 _1ebepn0 _16f8jrr", props.on && "_lwugp7", props.class);
import { function cn(...styles: Style[]): 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
, const cv: <V extends Record<string, Record<string, Style>> = Record<never, never>>(config: CvConfig<V>) => (props?: CvProps<V>) => string
The plugin replaces the call by a runtime function of class strings: `button({ size: "lg" })` gives the class names.
cv
, const fontSize: Prop<"font-size">
`<absolute-size> | <relative-size> | <length-percentage [0,∞]> | math`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/font-size)
fontSize
, const opacity: Prop<"opacity">
`<opacity-value>`. Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/opacity)
opacity
, const p: Prop<"padding">
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
} from "cssints" with { type: "cssints" };
const
const button: (props?: CvProps<{
    size: {
        sm: Style;
        lg: Style;
    };
    disabled: {
        true: Style;
        false: Style;
    };
}> | undefined) => string
button
=
cv<{
    size: {
        sm: Style;
        lg: Style;
    };
    disabled: {
        true: Style;
        false: Style;
    };
}>(config: CvConfig<{
    size: {
        sm: Style;
        lg: Style;
    };
    disabled: {
        true: Style;
        false: Style;
    };
}>): (props?: CvProps<{
    size: {
        sm: Style;
        lg: Style;
    };
    disabled: {
        true: Style;
        false: Style;
    };
}> | undefined) => string
The plugin replaces the call by a runtime function of class strings: `button({ size: "lg" })` gives the class names.
cv
({
CvConfig<V extends Record<string, Record<string, Style>>>.base?: Style | undefinedbase: function cn(...styles: Style[]): 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
(p<readonly [2]>(parts_0: 2): Style
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
(2), fontSize<readonly ["1rem"]>(parts_0: "1rem"): Style
`<absolute-size> | <relative-size> | <length-percentage [0,∞]> | math`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/font-size)
fontSize
("1rem")),
CvConfig<{ size: { sm: Style; lg: Style; }; disabled: { true: Style; false: Style; }; }>.variants?: {
    size: {
        sm: Style;
        lg: Style;
    };
    disabled: {
        true: Style;
        false: Style;
    };
} | undefined
variants
: {
size: {
    sm: Style;
    lg: Style;
}
size
: { sm: Stylesm: p<readonly [1]>(parts_0: 1): Style
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
(1), lg: Stylelg: function cn(...styles: Style[]): 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
(p<readonly [4]>(parts_0: 4): Style
`<'padding-top'>{1,4}`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/padding)
p
(4), fontSize<readonly ["1.25rem"]>(parts_0: "1.25rem"): Style
`<absolute-size> | <relative-size> | <length-percentage [0,∞]> | math`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/font-size)
fontSize
("1.25rem")) },
disabled: {
    true: Style;
    false: Style;
}
disabled
: { true: Styletrue: opacity<readonly [0.5]>(parts_0: 0.5): Style
`<opacity-value>`. Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/opacity)
opacity
(0.5), false: Stylefalse: opacity<readonly [1]>(parts_0: 1): Style
`<opacity-value>`. Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/opacity)
opacity
(1) },
},
CvConfig<{ size: { sm: Style; lg: Style; }; disabled: { true: Style; false: Style; }; }>.defaultVariants?: CvProps<NoInfer<{
    size: {
        sm: Style;
        lg: Style;
    };
    disabled: {
        true: Style;
        false: Style;
    };
}>> | undefined
defaultVariants
: { size?: "sm" | "lg" | undefinedsize: "sm", disabled?: boolean | undefineddisabled: false },
CvConfig<{ size: { sm: Style; lg: Style; }; disabled: { true: Style; false: Style; }; }>.compoundVariants?: (CvProps<NoInfer<{
    size: {
        sm: Style;
        lg: Style;
    };
    disabled: {
        true: Style;
        false: Style;
    };
}>> & {
    class: Style;
})[] | undefined
Styles added when every value named here is active, and that win over the variants they combine.
compoundVariants
: [{ size?: "sm" | "lg" | undefinedsize: "lg", disabled?: boolean | undefineddisabled: true, class: Styleclass: opacity<readonly [0.3]>(parts_0: 0.3): Style
`<opacity-value>`. Chrome 1, Edge 12, Firefox 1, Safari 2, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/opacity)
opacity
(0.3) }],
}); export const const classes: () => stringclasses = () =>
const button: (props?: CvProps<{
    size: {
        sm: Style;
        lg: Style;
    };
    disabled: {
        true: Style;
        false: Style;
    };
}> | undefined) => string
button
({ size?: "sm" | "lg" | undefinedsize: "lg" }); // "_lwugp7 _z6yj9k _1t0bw2x"
export const const off: () => stringoff = () =>
const button: (props?: CvProps<{
    size: {
        sm: Style;
        lg: Style;
    };
    disabled: {
        true: Style;
        false: Style;
    };
}> | undefined) => string
button
({ size?: "sm" | "lg" | undefinedsize: "lg", disabled?: boolean | undefineddisabled: true });
export const const bad: () => stringbad = () =>
const button: (props?: CvProps<{
    size: {
        sm: Style;
        lg: Style;
    };
    disabled: {
        true: Style;
        false: Style;
    };
}> | undefined) => string
button
({ size: "xl" }); // error: not a size
Type '"xl"' is not assignable to type '"sm" | "lg" | undefined'.
export const const bad2: (props?: CvProps<Record<never, never>> | undefined) => stringbad2 = cv<Record<never, never>>(config: CvConfig<Record<never, never>>): (props?: CvProps<Record<never, never>> | undefined) => string
The plugin replaces the call by a runtime function of class strings: `button({ size: "lg" })` gives the class names.
cv
({ base: "padding: 1rem" }); // error: a base is a css value, not a CSS string
Type 'string' is not assignable to type 'Style'. Type 'string' is not assignable to type '{ readonly __cssints: "style"; }'.