vyrn

std/tw

1 export, from std/tw.vyrn.

Theme-derived utility classes as a checked type. tw(theme) reads a flat theme.json at generation time and returns a module with TwClass (one class; finite, so the LSP completes it), Tw (space-separated classes), cls(c: Tw) -> Attr, and css() -> String, the baked stylesheet. A literal passed to cls is checked against Tw at compile time.

import { tw } from "std/tw"
import * as theme from tw("./theme.json")

theme.json:

{
  "colors":      { "brand": { "500": "#4f46e5" }, "white": "#ffffff" },
  "spacing":     { "0": "0", "1": "0.25rem" },
  "radius":      { "DEFAULT": "0.5rem", "full": "9999px" },
  "fontSize":    { "sm": "0.875rem" },
  "breakpoints": { "sm": "640px", "md": "768px" },
  "safelist":    ["book-card"]
}

radius.DEFAULT drives the bare rounded. Safelisted names join the type but get no CSS rule. An unknown top-level key, a non-string leaf, an unsafe class name or an unsafe CSS value fails generation, naming the offender.

Dependencies

The import graph shows the whole library.

Imported by
gen fn tw(theme: String) -> String

Reads the flat theme.json and returns the typed module. A malformed theme fails the load with a report naming the offending key.

Tests

20 test blocks in std/tw.vyrn. CI runs all of them.

Test names
  • twParseTheme flattens nested keys with dots
  • twParseTheme rejects a non-string leaf
  • a scalar directly under a top-level family is refused, not silently ignored
  • twAxisOf dashes color shade paths and keeps theme order
  • a theme that cannot be read is its own diagnostic, not a parse error
  • twClassSafe accepts utility names and rejects unsafe segments
  • twBuildVocab emits families in the locked order
  • twBoxDecl expands the shorthand sides
  • twRoundedClasses maps DEFAULT to bare rounded
  • twRuleFlat escapes the colon in a variant selector
  • twBuildCss is deterministic and puts media queries last
  • twTokenRegex factors the prefixes over the base alternation
  • safelist folds names into the token grammar after the derived vocabulary
  • safelist rejects a name that is not CSS-safe
  • safelist is a recognised top-level key (not an unknown-key error)
  • twUnknownKeyErrors names an unrecognised top-level key
  • twValueSafe accepts hex/length/ident and rejects injection
  • twCssValueSafe routes values through the std/scan single-token gate
  • twUnsafeValueErrors names a CSS-injecting leaf value by key
  • twUnsafeBreakpointErrors names a forging breakpoint key

Examples

4 programs in examples/ import this module. Each runs as WebAssembly and as a native binary.