CSS-in-TS

Open in playground

import { reset } from "@cssints/reset" with { type: "cssints" };
import {
  bg,
  borderRadius,
  cn,
  color,
  cv,
  flex,
  fontSize,
  fontWeight,
  gap,
  global,
  hover,
  margin,
  media,
  p,
} from "cssints" with { type: "cssints" };

import { t } from "./theme.ts" with { type: "cssints" };

reset();

// What cssints is, in one screen: calls that look like CSS, checked against the spec, compiled to atomic classes.
global("body", bg(t.color.bg), color(t.color.text), margin("0"));

const wide = media("(width >= 36rem)");

const card = cn(
  flex().direction("column").gap(t.space.sm),
  p(4),
  bg(t.color.card),
  borderRadius(t.radius.md),
  wide(p(6)),
);
const title = cn(fontSize("1.25rem"), fontWeight(700), margin("0"));
const note = cn(color(t.color.muted), margin("0"));
const button = cv({
  base: cn(
    p(2),
    borderRadius(t.radius.md),
    bg(t.color.accent),
    color(t.color.onAccent),
    hover(bg(t.color.text)),
  ),
  variants: { tone: { quiet: cn(bg("transparent"), color(t.color.accent)) } },
});

export default function App() {
  return (
    <main className={cn(flex().direction("column"), gap(4), p(4))}>
      <section className={card}>
        <h1 className={title}>Atomic classes, typed values</h1>
        <p className={note}>
          Resize the preview: the padding changes at 36rem. Switch the site to dark: the theme
          follows the system colour scheme.
        </p>
        <div className={flex().gap(2)}>
          <button className={button()}>Save</button>
          <button className={button({ tone: "quiet" })}>Cancel</button>
        </div>
      </section>
    </main>
  );
}
import { check, createGlobalTheme, createTokens, media } from "cssints" with { type: "cssints" };

// Tokens are typed values that print as var(--name); a theme sets them again under a condition.
export const t = createTokens({
  color: {
    $type: "color",
    bg: { $value: "#ffffff" },
    card: { $value: "#f4f6fb" },
    text: { $value: "#16181d" },
    muted: { $value: "#5b6272" },
    accent: { $value: "#2563eb" },
    onAccent: { $value: "#ffffff" },
  },
  space: { $type: "dimension", sm: { $value: "8px" }, md: { $value: "16px" } },
  radius: { $type: "dimension", md: { $value: "12px" } },
});

createGlobalTheme(t, media("(prefers-color-scheme: dark)"), {
  color: { bg: "#0f1115", card: "#1a1d24", text: "#eceef3", muted: "#a1a8b8", accent: "#7aa2ff" },
});

// the build fails when a theme makes text unreadable
check.contrast(t.color.text, t.color.card, { min: 7 });
check.contrast(t.color.muted, t.color.card, { min: 4.5 });

cssints is compile-time CSS-in-TypeScript. You write calls such as css.flex().alignItems("center"). A Vite plugin runs them at build time and replaces each one with atomic, content-hashed class names in @layers. Nothing of the styling code ships to the browser except a few lines that join class names: for a cn() with runtime arguments and for cv/scv.

Alpha. cssints is on npm at 0.x. The API can still change between minor versions, the plugin packages may move, and some browsers and frameworks are checked less than others (docs/API.md, "Browser support and what 0.1 does not have"). Pin the version, and report what breaks.

Decisions and findings are in docs/wayfinder.md; the API reference is docs/API.md. Both are also a site with search and type hovers: apps/docs (cd apps/docs && bun run dev); deployed to Cloudflare with Alchemy: https://cssints.dev.