@cssints/fluid

Open in playground

import { reset } from "@cssints/reset" with { type: "cssints" };
import {
  bg,
  cn,
  color,
  containerType,
  fontSize,
  margin,
  maxWidth,
  padding,
} from "cssints" with { type: "cssints" };
import { fluid } from "@cssints/fluid" with { type: "cssints" };

reset();

// fluid(min, max) is a clamp() between two sizes over the width of the viewport (20rem to 80rem unless told). The build refuses a
// range that fails WCAG 1.4.4, where zooming the text to 200% would not make it larger.
const page = cn(maxWidth("48rem"), margin("0", "auto"), padding(fluid("1rem", "2rem")));
const title = cn(fontSize(fluid("1.75rem", "3.5rem")), margin("0"));
const lead = cn(fontSize(fluid("1rem", "1.375rem")));
// against the inline size of the nearest container instead of the viewport
const card = cn(
  padding(fluid.container("1rem", "2rem", { to: "40rem" })),
  bg("#e0e7ff"),
  color("#0f172a"),
);

export default function App() {
  return (
    <main className={page}>
      <h1 className={title}>Type that scales with the window</h1>
      <p className={lead}>
        Resize the preview: the title, this lead and the padding move between their two sizes.
      </p>
      <div className={containerType("inline-size")}>
        <div className={card}>Padding against the width of the container around it</div>
      </div>
    </main>
  );
}

Fluid lengths for cssints: fluid(min, max) is a length that is min on a narrow viewport, max on a wide one, and grows linearly between. The clamp() is computed at build time, and a range that breaks text resizing (WCAG 1.4.4) fails the build. Two value members of cssints/plugin scope() (fn({ args, value }), positional arguments and an options object). No runtime.

import { fontSize, padding } from "cssints" with { type: "cssints" };
import { fluid } from "@cssints/fluid" with { type: "cssints" };

// fluid(min, max, { from?, to?, zoom? }): from and to are the widths, 20rem and 80rem when left out
export const title = fontSize(fluid("1rem", "1.5rem"));
export const lead = fontSize(fluid("16px", "24px", { from: "40rem", to: "90rem" }));
// against the inline size of the nearest container instead of the viewport
export const card = padding(fluid.container("1rem", "2rem", { to: "60rem" }));

export const hero = fontSize(fluid("1rem", "4rem")); // build error: fails WCAG 1.4.4
._bkcdcr {
  font-size: clamp(1rem, 0.8333vw + 0.8333rem, 1.5rem);
}
._1wkr2lu {
  font-size: clamp(16px, 1vw + 0.6rem, 24px);
}
._ekb8uj {
  padding: clamp(1rem, 2.5cqi + 0.5rem, 2rem);
}

Rules

`).

Limits

cd packages/fluid && bun run test   # fixtures through the engine (output, errors), and the type test via bun run typecheck

Credits

The WCAG 1.4.4 check follows fluid-tailwind (MIT, Maxwell Barvian). The check is written here; no code is copied.