Fallbacks: firstThatWorks

Open in playground

import { reset } from "@cssints/reset" with { type: "cssints" };
import {
  bg,
  cn,
  color,
  firstThatWorks,
  flex,
  fontSize,
  minHeight,
  p,
  token,
} from "cssints" with { type: "cssints" };

reset();

const brand = token.color("#0057b8", "brand");

// The wanted value comes first, then what to use where it fails: one class, the declarations in CSS order, the wanted one last.
const hero = cn(
  minHeight(firstThatWorks("100dvh", "100vh")),
  flex().direction("column").justifyContent("center").alignItems("center").gap(3),
  bg(firstThatWorks("oklch(55% 0.18 250)", brand)),
  color("white"),
  p(6),
);
const heading = cn(
  fontSize("2rem"),
  color(firstThatWorks("color-mix(in srgb, white, #bae6fd)", "white")),
);

export default function App() {
  return (
    <section className={hero}>
      <h1 className={heading}>Full height, with a fallback</h1>
      <p>min-height: 100vh, then 100dvh where the browser knows it.</p>
    </section>
  );
}

firstThatWorks(wanted, ...fallbacks) is a part of any property: the value you want comes first, then what to use where it fails.

import { const border: Prop<"border">
`<line-width> || <line-style> || <color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/border)
border
, const color: Prop<"color">
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/color)
color
, const firstThatWorks: <const V extends readonly [Alt, Alt, ...Alt[]]>(...values: V) => Fallback<V>
`minHeight(firstThatWorks("100dvh", "100vh"))`: the value you want, then what to use where it fails. A part of any property; the build emits one class with all the declarations, the wanted one last. Each value is checked for the property.
firstThatWorks
, const minHeight: Prop<"min-height">
`auto | <length-percentage [0,∞]> | min-content | max-content | fit-content | fit-content(<length-percentage [0,∞]>) | <calc-size()> | <anchor-size()>`. Chrome 1, Edge 12, Firefox 3, Safari 1.3, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/min-height)
minHeight
,
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
} from "cssints" with { type: "cssints" };
const const brand: Token<"color", true, string>brand =
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: <"#0057b8", "brand">(initial: "#0057b8", name?: "brand" | undefined) => Token<"color", true, string>color("#0057b8", "brand");
export const const a: Stylea = minHeight<readonly [Fallback<readonly ["100dvh", "100vh"]>]>(parts_0: Fallback<readonly ["100dvh", "100vh"]>): Style
`auto | <length-percentage [0,∞]> | min-content | max-content | fit-content | fit-content(<length-percentage [0,∞]>) | <calc-size()> | <anchor-size()>`. Chrome 1, Edge 12, Firefox 3, Safari 1.3, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/min-height)
minHeight
(firstThatWorks<readonly ["100dvh", "100vh"]>(values_0: "100dvh", values_1: "100vh"): Fallback<readonly ["100dvh", "100vh"]>
`minHeight(firstThatWorks("100dvh", "100vh"))`: the value you want, then what to use where it fails. A part of any property; the build emits one class with all the declarations, the wanted one last. Each value is checked for the property.
firstThatWorks
("100dvh", "100vh"));
export const const b: Styleb = color<readonly [Fallback<readonly ["oklch(55% 0.18 250)", Token<"color", true, string>]>]>(parts_0: Fallback<readonly ["oklch(55% 0.18 250)", Token<"color", true, string>]>): Style
`<color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/color)
color
(firstThatWorks<readonly ["oklch(55% 0.18 250)", Token<"color", true, string>]>(values_0: "oklch(55% 0.18 250)", values_1: Token<"color", true, string>): Fallback<readonly ["oklch(55% 0.18 250)", Token<"color", true, string>]>
`minHeight(firstThatWorks("100dvh", "100vh"))`: the value you want, then what to use where it fails. A part of any property; the build emits one class with all the declarations, the wanted one last. Each value is checked for the property.
firstThatWorks
("oklch(55% 0.18 250)", const brand: Token<"color", true, string>brand));
export const const c: Stylec = border<readonly ["1px", Fallback<readonly ["solid", "dashed"]>, "red"]>(parts_0: "1px", parts_1: Fallback<readonly ["solid", "dashed"]>, parts_2: "red"): Style
`<line-width> || <line-style> || <color>`. Chrome 1, Edge 12, Firefox 1, Safari 1, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/border)
border
("1px", firstThatWorks<readonly ["solid", "dashed"]>(values_0: "solid", values_1: "dashed"): Fallback<readonly ["solid", "dashed"]>
`minHeight(firstThatWorks("100dvh", "100vh"))`: the value you want, then what to use where it fails. A part of any property; the build emits one class with all the declarations, the wanted one last. Each value is checked for the property.
firstThatWorks
("solid", "dashed"), "red");
export const const d: Styled = minHeight<readonly [Fallback<readonly ["100dvh", "100vhh"]>]>(parts_0: "min-height: unexpected \"100vhh\" in \"100vhh\", grammar auto | <length-percentage [0,∞]> | min-content | max-content | fit-content | fit-content(<length-percentage [0,∞]>) | <calc-size()> | <anchor-size()>"): Style
`auto | <length-percentage [0,∞]> | min-content | max-content | fit-content | fit-content(<length-percentage [0,∞]>) | <calc-size()> | <anchor-size()>`. Chrome 1, Edge 12, Firefox 3, Safari 1.3, iOS 1, Android 18. [MDN](https://developer.mozilla.org/docs/Web/CSS/min-height)
minHeight
(firstThatWorks("100dvh", "100vhh")); // error: the fallback is not a min-height
Argument of type 'Fallback<readonly ["100dvh", "100vhh"]>' is not assignable to parameter of type '"min-height: unexpected \"100vhh\" in \"100vhh\", grammar auto | <length-percentage [0,∞]> | min-content | max-content | fit-content | fit-content(<length-percentage [0,∞]>) | <calc-size()> | <anchor-size()>"'.