Skip to content

Latest commit

 

History

History
232 lines (191 loc) · 17 KB

AGENTS.md

File metadata and controls

232 lines (191 loc) · 17 KB

AGENTS.md — @ui8kit/codegen

UI8Kit 3 ecosystem

Pipeline: codegen (spec → generated/) → registry (pack r/<rt>/) → CDN (unpkg / jsDelivr) → cli (init / add) → app

Spec-driven generator: bricks/<name>/<name>.def.ts is the single source of truth; emitters in src/emitters/ print it into React, Svelte 5, Vue 3, SolidJS, Web Components and Go templ. generated/ is build output (git-ignored) — never edit it, change the brick or the emitter.

Terminology

  • The set of outputs is 11 targets (render targets): 5 UI frameworks (React, Vue 3, Svelte 5, SolidJS, Web Components)
    • 6 server-side template engines (templ, html/template, Twig, Latte, Jinja2, MiniJinja). Say "targets", not "runtimes"/"frameworks", for the set (see README → Terminology); code identifiers and CLI flags (--runtimes, runtime/, r/<runtime>/) keep their names.

Documentation

  • docs/ is the single, exhaustive home for primitives docs (docs/README.md). Read docs/primitives/<name>.md before using or changing a primitive; runtime specifics in docs/runtimes/<rt>.md, rules in docs/contract.md.
  • docs/primitives/<brick>.md and the index in docs/README.md are generated by bun run docs (scripts/docs.ts) — after changing a brick/fixture run it and commit the result. Hand-written notes go only between <!-- ui8kit-docs:prose:start/end -->; runtimes/, contract.md, slot.md are fully hand-written. bun run docs:check (in verify) fails on stale docs; tests/docs.test.ts executes every doc snippet in all 11 targets.

Commands

  • bun run verify — must stay green (check, generate, docs:check, tsc, generated typecheck, all tests).
  • bun run docs / bun run docs:check — regenerate / verify the generated docs.
  • bun run generate [-- --runtimes …,templ] — writes generated/.
  • templ: cd generated/templ && templ generate && go vet ./... && go build ./....

Runtime contract

  • Text is compared strictly by the parity suites (tests/support/normalize.ts): server templates (Twig/Latte/Jinja2/MiniJinja) print children flush against their tags (<a>admin</a>, exact <textarea> value) — never wrap {children} / slots in newlines or indentation. Only whitespace-only nodes between sibling elements are ignored.

  • React boolean attrs are forwarded as-is (checked={checked}), never || undefined: checked={false} must stay controlled (tests/react-controlled.test.ts).

  • generate --cn join swaps only the TS utils/cn.ts for runtime/ts-join/cn.ts (no tailwind-merge, no conflict resolution) for catalog builds; the default merge is what the registry packs and stays byte-identical (tests/cn-join.test.ts). Never pack the registry with --cn join.

  • cn/CnMerge ports (Go, PHP, Python) keep tailwind-merge groups: ring-offset-N, ring-offset-<color>, ring-N and ring-<color> are four separate groups (tests/cn-merge.test.ts).

  • Forms: Form (<form>) is the only path — Box no longer accepts tag="form". Separator renders <hr>; decorative ⇒ role="none" (see docs/primitives/separator.md).

  • Every target renders the same DOM, classes (tokens) and ARIA as React / the canonical renderer (src/domain/render.ts). Parity tests prove it: tests/parity*.test.ts.

  • Attribute rules (docs/contract.md#attributes-precedence-empty-values-urls): extra attrs win over the named attribute, then blank optional attrs (all but value) and unsafe URLs are omitted; data:image/… only in <img src> (Latte >= 3.1.9 drops data: in src itself: not portable, the parity fixtures use https there). tests/parity.attrs.test.ts checks every rule in all 9 real targets against explicit HTML.

  • New runtime ⇒ add an emitter, a real-engine parity test (no mocks), CI setup, rules entry in templates/rules/runtimes.json, README section.

  • UI8KIT_REQUIRE_ALL_RUNTIMES=1 (set in CI) turns a missing toolchain (Go/templ) into a failure; locally the suite skips with a message.

Go templ specifics

  • Output generated/templ/ (own Go module, templ v0.3.1070, Go 1.26); helpers come from runtime/go/*.go (+ go.sum), tag groups are generated from src/domain/tags.ts.
  • No asChild/Slot: each part with a class contract exports <Name>Classes(p <Name>Props) string — the escape hatch for custom roots.
  • Dynamic tags (Box/Text Tag) become a switch with one static element per allowed tag; when branches (Button/Link Href) repeat { children... }; items (Select Options) loop with for _, item := range.
  • Go zero values mean "unset": empty strings omit attributes (Value is *string: new("") = value=""); prop defaults are unsupported (the emitter throws). A key in Attrs replaces the named attribute (uiutils.HasAttr); Attrs go through uiutils.RestAttrs (unsafe URLs, blank optional attrs dropped).
  • Children are trusted HTML ({ children... }, templ.Raw in the harness). Props and text are escaped by templ; never route user input into raw children (XSS).

Jinja2 specifics

  • Output generated/jinja2/ (one ui/<brick>/<part>.html.j2 per part); Python helpers come from runtime/python/ (ui8kit_jinja, ported from xgen tag xgen-archive-2026-10-23), ui8kit_jinja/classes.py is generated. Local check: pip install jinja2 then bun test tests/parity.jinja2.test.ts (PYTHON= overrides the interpreter).
  • No asChild/Slot: each part with a class contract gets a <name>_classes(p) Jinja global.
  • Children are trusted HTML ({{ children|safe }}, repeated per branch); never route user input into children (XSS). Props/text are autoescaped.

html/template specifics

  • Output generated/htmltemplate/ (src/emitters/htmltemplate.ts): package ui (one <brick>.go + embedded ui/<brick>/<part>.html per brick, registered in init()), utils/ = runtime/go/ (engine-free core) + runtime/go-htmltemplate/ (props coercion, AttrsHTML, registry, Funcs()); stdlib only.
  • Every computed value is a Go method X<n> on <Part>Props (printed with go-expr.ts, as templ); templates contain only if/else if eq/range/end, $.X<n> and $.Children (tests/emitters.test.ts checks). Never generate template.HTML(/HTMLAttr(/JS(/CSS( from data; URLs go through uiutils.SafeURL/SafeImgURL, then template.URL (otherwise html/template rewrites tel:/data:image to #ZgotmplZ); computed JS/CSS attributes are an emitter error.
  • html/template quirks: no dynamic tag or attribute names ({{if eq}} branches per tag), comments stripped, {{if}} branches must end in the same context, URL values are percent-normalized (the parity suites normalize the expected side with tests/support/htmltemplate.ts).
  • Local check: Go on PATH, bun test tests/parity.htmltemplate.test.ts.
  • runtime/go-templ/templ_attrs.go holds the templ-only helpers: not *_templ.go, which templ generate deletes.

MiniJinja specifics

  • Output generated/minijinja/: the local crate ui8kit_minijinja (no crates.io). The templates are the Jinja2 emitter's minijinja dialect (src/emitters/jinja2.ts, .html, same bodies as .html.j2); the crate files come from src/emitters/minijinja.ts (Cargo.toml, build.rs, classes.rs, recipes.rs, ui8kit.html macros, parity harness) and runtime/rust/*.rs (helpers, cn_merge port, register).
  • MiniJinja is not Jinja2: no Python methods (.update(), .items()), no macro kwargs/varargs, a bare bool or none prints true/none (Laravel Rust: true/empty), undefined['x'] fails under SemiStrict. Templates must never print a bare bool/none (tests/emitters.test.ts checks); helpers take Value and treat undefined as unset.
  • Local check: cargo on PATH, bun test tests/parity.minijinja.test.ts (CI also runs clippy -D warnings).
  • Children are trusted HTML, as in Jinja2.

Twig / Latte specifics

  • Output generated/twig/ and generated/latte/ (Composer packages, twig/twig ^3, latte/latte ^3); emitters src/emitters/{twig,latte,php-common}.ts, PHP helpers copied from runtime/php/*.php (__NAMESPACE__ → UI8Kit\Twig / UI8Kit\Latte); Tags.php and Classes.php are generated.
  • No asChild: Classes::<part>Classes(array $props) (Twig: ui8kit_classes('<part>Classes', {…})).
  • Dynamic tags: if chain, one static element per allowed tag; slot repeated per branch; Select options loop over options.
  • Children are trusted HTML (|raw / |noescape); never route user input into them (XSS).
  • Parity: tests/parity.{twig,latte}.test.ts (shared tests/support/php-parity.ts); CI uses shivammathur/setup-php (PHP 8.3 + Composer).

generated/

  • Never commit generated/; it is build output of bun run generate and is ignored.
  • Change specs/emitters in src/, then run bun run verify (regenerates and checks all targets).
  • @ui8kit/registry is packed from a fresh bun run generate, never from a stale tree.

Installing primitives with the ui8kit CLI

This repo generates the primitives (source of truth). You normally do not install from here — apps install the packed output via the CLI:

Quick start per target

Always call the CLI with a pinned major: npx ui8kit@3. add all and add --all are equivalent.

# UI frameworks (writes package.json peers clsx + tailwind-merge for utils)
npx ui8kit@3 init --yes --framework react        --dir src && npx ui8kit@3 add button
npx ui8kit@3 init --yes --framework vue          --dir src && npx ui8kit@3 add button
npx ui8kit@3 init --yes --framework svelte       --dir src && npx ui8kit@3 add button
npx ui8kit@3 init --yes --framework solid        --dir src && npx ui8kit@3 add button
npx ui8kit@3 init --yes --framework webcomponent --dir src && npx ui8kit@3 add button
npx ui8kit@3 add --all            # everything in r/<runtime>/index.json

# Server template engines (no package.json is written or required)
npx ui8kit@3 init --yes --framework templ  --dir internal  && npx ui8kit@3 add button
npx ui8kit@3 init --yes --framework twig   --dir templates && npx ui8kit@3 add button
npx ui8kit@3 init --yes --framework latte  --dir templates && npx ui8kit@3 add button
npx ui8kit@3 init --yes --framework jinja2 --dir templates && npx ui8kit@3 add button
npx ui8kit@3 init --yes --framework minijinja --dir crates/ui8kit && npx ui8kit@3 add button   # ui8kit >= 3.6
npx ui8kit@3 init --yes --framework htmltemplate --dir internal/ui8kit && npx ui8kit@3 add button   # ui8kit >= 3.7

Post-install for server template engines (the CLI prints the exact commands from item meta; it never runs them and never edits composer.json / go.mod):

  • templ: go get github.com/a-h/templ@<printed version> → templ generate (after every add, and in CI; *_templ.go is generated, not shipped) → go build ./.... With go.mod present the utils import is rewritten to <module>/<dir>/utils.
  • twig / latte: composer require "twig/twig:^3.0" (or the printed latte package), add the printed PSR-4 entry to composer.json, then composer dump-autoload:
    { "autoload": { "psr-4": { "UI8Kit\\Twig\\": "templates/runtime/" } } }
    (UI8Kit\\Latte\\ for latte). Twig: $twig->addExtension(new \UI8Kit\Twig\TwigExtension()).
  • jinja2: pip install "Jinja2>=3.1,<4"; from ui8kit_jinja import environment → environment().get_template("ui/button/button.html.j2").render(p={...}, children=...).
  • minijinja: add ui8kit_minijinja = { path = "crates/ui8kit" } to Cargo.toml (minijinja 2.24+, preserve_order), then ui8kit_minijinja::register(&mut env) (Laravel Rust: app.views().minijinja(ui8kit_minijinja::register)) → {% import "ui8kit.html" as ui %}{{ ui.button({'Variant': 'outline'}, children='Save') }}.
  • htmltemplate: nothing to install (stdlib); parse views with ui.Funcs() (Laravel Go: laravel.Options{ViewFuncs: …}) → {{ ui8kit_render "button" (ui8kit_dict "Children" "Save") }}.

Commands and flags (ui8kit 3.2)

Global: -c, --cwd <dir>, -v, --verbose, --no-cache, -V, --version.

Command Flags
init -y, --yes, --framework <react|vue|svelte|solid|webcomponent|templ|latte|twig|jinja2|none>, --runtime <name> (defaults to framework), --dir <path>, --skip-core, -r, --registry <type> (default ui), --registry-url <url>, --registry-version <v>, --strict-cdn, --import-style <alias|package> (package deprecated)
add [components...] -a, --all (or add all), -f, --force, --runtime <name>, --path <key=dir> (repeatable, e.g. ui=kit), --dry-run, --retry, -r, --registry <type>, --registry-url, --registry-version, --strict-cdn
check offline verify vs ui8kit.lock.json; --json, --adopt (fetches registry, adopts only matching files), --runtime, --registry-url, --registry-version, --strict-cdn (the last three only with --adopt)
list --json, -r, --registry, --registry-url, --registry-version, --strict-cdn
diff [component] --json, -r, --registry, --registry-url, --registry-version, --strict-cdn
info --json, --cdn (resolved CDN order and overrides)
scan -r, --registry <type|path>, -o, --output <file>, -s, --source <dir>
build [registry] default ./src/registry.json; -o, --output <path> (default ./packages/registry/r)
cache clear —
registry clean --dry-run, --all, -f, --force
reset --dry-run, --with-cache, -y, --yes, -f, --force

Where registries live

  • Default: @ui8kit/registry@<CLI major> from npm via CDN, tried in order unpkg → jsDelivr:
    • https://unpkg.com/@ui8kit/registry@3/r/<runtime>/index.json
    • https://cdn.jsdelivr.net/npm/@ui8kit/registry@3/r/<runtime>/index.json
  • Per target (react, vue, svelte, solid, webcomponent, templ, twig, latte, jinja2) under r/<runtime>/:
    • index.json — catalogue
    • components/ui/<name>.json — primitives (e.g. button.json)
    • utils/utils.json — registry:utils (installed by init)
    • rules/rules.json — registry:rules → .cursor/rules/ui8kit-primitives.mdc (installed by init)
  • $schema: https://cdn.jsdelivr.net/npm/@ui8kit/registry@3/schema/{schema,registry,registry-item}.json (config / index / item).
  • Version: --registry-version or "registryVersion" in ui8kit.config.json accepts exact (3.1.0), range (3.1) or dist-tag (canary); default is the CLI major (3 → newest 3.x.y).
  • Resolution order: --registry-url / registryUrl → cdnBaseUrls (config) → built-in unpkg / jsDelivr. With --strict-cdn (or strictCdn) and an explicit URL, only that URL is used.
  • Brand registries: @ui8kit/registry ships primitives only. Brand kits (blocks, composites) host their own registry, e.g. --registry-url https://registry.buildy.tw/react, and reference primitives by absolute URL in registryDependencies (pinned digest, recorded in meta.primitives). Brand items get their own lock entries.
  • Lock: init / add write ui8kit.lock.json (source URL, exact version, sha256 per file). Commit it; verify with npx ui8kit@3 check (offline, exit 1 on missing / modified / mixed-versions / rules-version). No lock yet → npx ui8kit@3 check --adopt once.

Where source lives

codegen  specs/ + bricks/ ─gen─▶ generated/<rt>/   (build output, NOT committed)
registry pack:runtimes  ──────────▶ r/<rt>/*.json     (published: @ui8kit/registry)
npm ──▶ unpkg / jsDelivr ──▶ cli: ui8kit init / add ──▶ app {dir}/ui, {dir}/utils + ui8kit.lock.json

Fix a primitive in codegen, repack in registry, publish, then add <name> --force in the app. Never fix it in the app or in r/ by hand.

Rules for agents (imperative)

  • Always pin the major: npx ui8kit@3 …, never bare npx ui8kit.
  • Install primitives only via init / add; never copy files from r/ or generated/ by hand.
  • Never edit ui8kit.lock.json manually; never edit files under {dir}/ui or {dir}/utils — style via className / tokens.
  • Run npx ui8kit@3 check before every commit; fix drift with add <name> --force.
  • Upgrade deliberately: change registryVersion, add --all --force, re-run init (rules), check, commit.
  • children passed to server-template primitives is trusted HTML (not escaped) — escape user input yourself.
  • Server template engines: after add, run the printed toolchain steps (templ generate, composer dump-autoload, pip install).
  • New UI that is not a primitive goes to a brand registry (--registry-url), never into @ui8kit/registry.