diff --git a/.github/workflows/tokens.yml b/.github/workflows/tokens.yml index 80c183a..54d50e1 100644 --- a/.github/workflows/tokens.yml +++ b/.github/workflows/tokens.yml @@ -7,8 +7,8 @@ on: - main jobs: - staleness: - name: Generated artifacts are current + valid: + name: tokens.json is valid and the app builds runs-on: ubuntu-latest steps: @@ -29,8 +29,6 @@ jobs: - name: Install dependencies run: pnpm install --frozen-lockfile - - name: Regenerate token artifacts - run: node build - - - name: Check generated artifacts are committed - run: git diff --exit-code -- src/tokens.css + # The build validates tokens.json (both modes, aliases) and typechecks the app. + - name: Build the app + run: pnpm --dir north-star build diff --git a/README.md b/README.md index d618fe3..31553e7 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Kilo's canonical design source for product UI. Agent guidance will be written fr This repo keeps Kilo's UI decisions in one place so Desktop, Cloud, Mobile, VS Code and the other Kilo surfaces do not each invent their own design system. -It is intentionally small: one token source, one generated stylesheet, and one app that shows the language. +It is intentionally small: one token file and one app that shows the language. > 🚧 This project is still in progress. The language is being defined from Kilo Desktop first; no product consumes these tokens yet. @@ -14,7 +14,6 @@ It is intentionally small: one token source, one generated stylesheet, and one a | --- | --- | --- | | Colour tokens | Done | `tokens.json` is the only place values are written. Dark and light. Role names may still change. | | Type, radius, spacing tokens | Drafted | Defined in `tokens.json`, but the screens in `north-star/` still hard-code these values. | -| Generated stylesheet | Done | `node build` writes `src/tokens.css`. | | Language app | Done | `north-star/` shows foundations, components and product screens from the tokens. | | VS Code surface | Next | Not in the app yet. The host editor owns its colours, so it needs its own study. | | Desktop adoption | Next | Desktop is the first product to take its theme from this repo. | @@ -51,33 +50,20 @@ It is intentionally small: one token source, one generated stylesheet, and one a | Surface | What it is | Authority | | --- | --- | --- | | `tokens.json` | Hand-authored design token source, dark and light | Canonical values | -| `src/tokens.css` | Generated CSS variables | Committed output from `node build` | | `north-star/` | Local Vite + React app: Foundations, Components, and target screens for Desktop, Cloud and Mobile | Reference for people and agents; reads only the tokens | | `skill-arena/` | Repeatable prompt-pass workflow | Skill evaluation harness; its Kilo condition is off until a new skill exists | Product code does not live here. Product repos consume this repo's artifacts from their own codebases. -## 🔁 How The Pieces Connect - -```text -tokens.json - -> node build - -> src/tokens.css - -> north-star/ (every screen and the component page) - -> north-star/ Foundations page (reads the JSON directly) -``` - -`north-star/` holds no token values of its own. To change a colour, edit `tokens.json` and run `node build`. - ## 📦 Tokens -Colour roles live under `color.dark` and `color.light`, and every role must exist in both. A value written as `{other-role}` is an alias. Each role becomes a CSS variable of the same name, for example `color.dark.act-bg` becomes `--act-bg`. +`tokens.json` is the only file that holds values, and there is no build step. People and agents read it directly. -```bash -node build -``` +Colour roles live under `color.dark` and `color.light`, and every role must exist in both. A value written as `{other-role}` is an alias. + +The app in `north-star/` turns the file into CSS variables when it starts: each role becomes a variable of the same name, so `color.dark.act-bg` becomes `--act-bg`. Dark is the default, and `data-mode="light"` or `data-mode="dark"` on any element switches. Save `tokens.json` while the app is running and it reloads. -This regenerates `src/tokens.css`. Dark is the default; set `data-mode="light"` or `data-mode="dark"` on any element to switch. The generated file is committed and never edited by hand. CI fails if it drifts from `tokens.json`. +The app refuses to start or build if a role is missing in one mode or an alias points nowhere. CI runs that build on every pull request. ## 🧭 Language App @@ -117,9 +103,8 @@ It shows stacked rounds of no-skill and frontend-design outputs, with a fixed pr Use this order when sources disagree: 1. `tokens.json` -2. `src/tokens.css` -3. the Components page in `north-star/` -4. nearby shipped product UI +2. the Components page in `north-star/` +3. nearby shipped product UI ## Decisions @@ -127,11 +112,11 @@ These decisions still hold. Earlier decision records are in git history. - **Tokens and the Components page are the target.** Token values and the target column of the Components page define where products are going, and shipped code that disagrees is drift to migrate. Each target records which shipped product it was taken from, so it is never an invented ideal. - **Standalone repo, pulled by pinned hash.** The system lives in its own repo so that no product monorepo owns it, and consumers pull a pinned commit. No npm package, because registry and publish auth across three toolchains is too much upkeep, and no git submodule, because one was tried in `kilo-cloud` (PR #3507) and reverted days later (#3535). -- **One hand-authored source, generated artifacts.** Values are edited in one source, `tokens.json`, and every product format is generated from it by `node build`. Today that is one CSS file; Mobile and VS Code will need their own generated formats. Generated files are never edited by hand. +- **One hand-authored source, nothing generated yet.** Values are edited in one file, `tokens.json`, and nothing derived from it is committed. When a product needs its own format, such as a CSS file for Desktop, it is generated from `tokens.json` and never edited by hand. That format is decided with the product's developer at adoption. - **Products are grouped by who owns the colours.** A product family is set by its theming contract, not by the code library it imports: surfaces that adapt to one host's theme are hosts within a family, and sharing a component library does not merge two families. - **No `DESIGN.md` in product repos.** Design rules live only in this repo's agent guidance, and a product repo gets one path-scoped line in its existing `AGENTS.md` that points to it when UI files are edited. The trigger is never the word "design", because the old keyword-triggered `DESIGN.md` loaded design rules into unrelated backend tasks. - **Governance starts manual, then graduates.** The design owner reviews design-touching PRs by hand until that becomes a bottleneck, then automated visual and drift checks become the first reviewer, and per-team champions follow once teams have stable ownership. Staying manual forever is a failure mode, not the plan. -- **Manual version bumps, CI staleness check.** Consumers pick up changes through small manual bump PRs until that becomes a chore, then a Renovate-style bot opens them. CI fails if the generated artifacts are stale against `tokens.json`. +- **Manual version bumps.** Consumers pick up changes through small manual bump PRs until that becomes a chore, then a Renovate-style bot opens them. CI fails if `tokens.json` is invalid. - **Primary actions are neutral.** The primary button uses the foreground colour (`act-bg`). The yellow `#FFDD00` appears only in the logo. Yellow on white is about 1.35:1, so it cannot carry text or small marks in light mode. - **Dark and light.** Every colour role has a value in both modes, and the build fails if one is missing. Desktop already ships both. - **Desktop first.** The language is defined from Kilo Desktop, and Desktop adopts it before the other products. @@ -149,7 +134,7 @@ These decisions still hold. Earlier decision records are in git history. When editing this repo: -1. If token values change, edit `tokens.json`, then run `node build` and commit `src/tokens.css`. +1. If token values change, edit `tokens.json`. Nothing else needs to be run or committed. 2. Never write a colour, radius or shadow value into `north-star/`. Add a role to `tokens.json` instead. -3. If the artifact name or location changes, update `build/`, CI, this README, `north-star/src/main.tsx` and `skill-arena/server.mjs`. +3. If the shape of `tokens.json` changes, update `north-star/tokens-css.ts`, the Foundations page and `skill-arena/server.mjs`. 4. If the change is planning or status only, update Linear instead of adding a repo plan. diff --git a/build/index.js b/build/index.js deleted file mode 100644 index dde675c..0000000 --- a/build/index.js +++ /dev/null @@ -1,116 +0,0 @@ -#!/usr/bin/env node - -// Generates src/tokens.css from tokens.json. No dependencies. -import { promises as fs } from "node:fs"; -import path from "node:path"; -import { fileURLToPath } from "node:url"; - -const __dirname = path.dirname(fileURLToPath(import.meta.url)); -const REPO_ROOT = path.resolve(__dirname, ".."); -const TOKENS_PATH = path.join(REPO_ROOT, "tokens.json"); -const OUTPUT_PATH = path.join(REPO_ROOT, "src", "tokens.css"); - -const MODES = ["dark", "light"]; -const ALIAS = /^\{([a-z0-9-]+)\}$/; - -function fail(message) { - throw new Error(message); -} - -function stringMap(value, name) { - if (typeof value !== "object" || value === null || Array.isArray(value)) fail(`${name} must be an object.`); - for (const [key, val] of Object.entries(value)) { - if (typeof val !== "string" || val === "") fail(`${name}.${key} must be a non-empty string.`); - } - return value; -} - -/** Follows `{role}` aliases within one mode to a literal value. */ -export function resolveColor(roles, name, seen = []) { - if (seen.includes(name)) fail(`Alias cycle: ${[...seen, name].join(" -> ")}.`); - const value = roles[name]; - if (value === undefined) fail(`Unknown color role "${name}"${seen.length ? ` (aliased from ${seen.at(-1)})` : ""}.`); - const alias = value.match(ALIAS); - return alias ? resolveColor(roles, alias[1], [...seen, name]) : value; -} - -export function validateTokens(tokens) { - if (typeof tokens.version !== "string") fail("tokens.version must be a string."); - stringMap(tokens.brand, "tokens.brand"); - stringMap(tokens.radius, "tokens.radius"); - stringMap(tokens.spacing, "tokens.spacing"); - - for (const group of ["color", "shadow"]) { - for (const mode of MODES) stringMap(tokens[group]?.[mode], `tokens.${group}.${mode}`); - // Every role must exist in both modes, or a surface breaks when the mode flips. - const [dark, light] = MODES.map((mode) => Object.keys(tokens[group][mode])); - const missing = [...dark.filter((k) => !light.includes(k)), ...light.filter((k) => !dark.includes(k))]; - if (missing.length) fail(`tokens.${group}: not defined in both modes: ${missing.join(", ")}.`); - } - for (const mode of MODES) { - for (const name of Object.keys(tokens.color[mode])) resolveColor(tokens.color[mode], name); - } - - for (const [role, def] of Object.entries(tokens.typography ?? fail("tokens.typography is required."))) { - if (typeof def.fontFamily !== "string") fail(`tokens.typography.${role}.fontFamily must be a string.`); - if (typeof def.fontSize !== "string") fail(`tokens.typography.${role}.fontSize must be a string.`); - if (typeof def.fontWeight !== "number") fail(`tokens.typography.${role}.fontWeight must be a number.`); - if (typeof def.lineHeight !== "number") fail(`tokens.typography.${role}.lineHeight must be a number.`); - } -} - -const decl = (name, value) => ` --${name}: ${value};`; -const prefixed = (prefix, map) => Object.entries(map).map(([name, value]) => decl(`${prefix}-${name}`, value)); - -export function generateCss(tokens) { - const shared = [ - ...prefixed("brand", tokens.brand), - ...prefixed("radius", tokens.radius), - ...prefixed("spacing", tokens.spacing), - ...Object.entries(tokens.typography).flatMap(([role, def]) => [ - decl(`type-${role}-family`, JSON.stringify(def.fontFamily)), - decl(`type-${role}-size`, def.fontSize), - decl(`type-${role}-weight`, def.fontWeight), - decl(`type-${role}-leading`, def.lineHeight), - ...(def.letterSpacing ? [decl(`type-${role}-tracking`, def.letterSpacing)] : []), - ...(def.textTransform ? [decl(`type-${role}-transform`, def.textTransform)] : []), - ]), - ]; - const mode = (name) => [ - ` color-scheme: ${name};`, - ...Object.entries(tokens.color[name]).map(([role, value]) => decl(role, value.replace(ALIAS, "var(--$1)"))), - ...prefixed("shadow", tokens.shadow[name]), - ]; - return [ - "/* Generated from tokens.json by `node build`. Do not edit this file by hand. */", - `/* Kilo design tokens ${tokens.version}. Dark is the default; set data-mode="light" or "dark" on any element. */`, - ":root {", - ...shared, - "}", - ':root,\n[data-mode="dark"] {', - ...mode("dark"), - "}", - '[data-mode="light"] {', - ...mode("light"), - "}", - "", - ].join("\n"); -} - -export async function readTokens(tokensPath = TOKENS_PATH) { - const tokens = JSON.parse(await fs.readFile(tokensPath, "utf8")); - validateTokens(tokens); - return tokens; -} - -const cliTarget = process.argv[1] ? path.resolve(process.argv[1]) : ""; -if (cliTarget === fileURLToPath(import.meta.url) || cliTarget === __dirname) { - try { - await fs.mkdir(path.dirname(OUTPUT_PATH), { recursive: true }); - await fs.writeFile(OUTPUT_PATH, generateCss(await readTokens()), "utf8"); - console.log(path.relative(REPO_ROOT, OUTPUT_PATH)); - } catch (error) { - console.error(error instanceof Error ? error.message : error); - process.exitCode = 1; - } -} diff --git a/build/package.json b/build/package.json deleted file mode 100644 index 664a2a0..0000000 --- a/build/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "@kilo-design/token-generator", - "private": true, - "type": "module", - "main": "index.js", - "scripts": { - "build": "node ." - } -} diff --git a/north-star/AGENTS.md b/north-star/AGENTS.md index 890778b..4c25481 100644 --- a/north-star/AGENTS.md +++ b/north-star/AGENTS.md @@ -1,10 +1,10 @@ # North star app: agent rules -The Kilo design language in one app: Foundations, Components, and target screens for every Kilo surface. This is a design reference, not product code. Every value comes from the repo root `tokens.json` through the generated `src/tokens.css`. How to run it and add screens: `README.md`. +The Kilo design language in one app: Foundations, Components, and target screens for every Kilo surface. This is a design reference, not product code. Every value comes from the repo root `tokens.json`, which the app turns into CSS variables when it starts. How to run it and add screens: `README.md`. ## Rules -- **Colour by role, never by hex.** This app defines no token values. If a role is missing, add it to the root `tokens.json` in both modes and run `node build`. Use the roles: `--act-bg/fg/bd/hover` (primary action), `--ink`, `--soft-bg/fg`, `--bubble-bg/fg`, `--send-bg/fg`, `--live`, `--check-bg/fg`, `--ring`, plus the neutrals and status colours. `#FFDD00` (`--brand-logo`) appears only in the logo tile. +- **Colour by role, never by hex.** This app defines no token values. If a role is missing, add it to the root `tokens.json` in both modes. Use the roles: `--act-bg/fg/bd/hover` (primary action), `--ink`, `--soft-bg/fg`, `--bubble-bg/fg`, `--send-bg/fg`, `--live`, `--check-bg/fg`, `--ring`, plus the neutrals and status colours. `#FFDD00` (`--brand-logo`) appears only in the logo tile. - **One screen per file.** Add screens under `src/surfaces//screens/`. A `.tsx` screen exports `meta: ScreenMeta` and a default component. A `.html` drop-in must be self-contained and must not load assets from other repos. Do not edit `src/registry.ts` to register anything; discovery is automatic. - **Reuse the frames.** Build on `DesktopWindow`, `ComposerRow`, `SessionCanvas`, `CloudWindow`, `Phone` and `TabBar`. Do not copy a window or phone frame. - **Prefix every class.** All surface CSS loads globally. Use the surface prefix (`d-` desktop, `c-` cloud, `m-` mobile, or a new one for a new surface), and put the styles in that surface's own `.css`. diff --git a/north-star/README.md b/north-star/README.md index 93f8583..ff0a0dc 100644 --- a/north-star/README.md +++ b/north-star/README.md @@ -2,7 +2,7 @@ The Kilo design language in one app: **Foundations** (the tokens), **Components** (one target per component), and target screens for every Kilo product. Primary actions use the foreground color; `#FFDD00` appears only in the logo. -Every value comes from the repo's `tokens.json` through the generated `src/tokens.css`. This app defines no token values. +Every value comes from the repo's `tokens.json`, which the app turns into CSS variables when it starts (`tokens-css.ts`, wired in `vite.config.ts`). This app defines no token values. ```sh pnpm --dir north-star dev # http://localhost:8732 @@ -40,7 +40,7 @@ export default function SettingsPanel() { } ``` -Use the roles from `tokens.json` (`--act-bg`, `--ink`, `--soft-bg`, `--bubble-bg`, `--send-bg`, `--live`, …), never a raw value. If a role is missing, add it to `tokens.json` and run `node build`. +Use the roles from `tokens.json` (`--act-bg`, `--ink`, `--soft-bg`, `--bubble-bg`, `--send-bg`, `--live`, …), never a raw value. If a role is missing, add it to `tokens.json` in both modes. **HTML drop-in** (a self-contained mock exported from another repo or tool). It renders in an iframe at the surface frame size. Optional head tags: @@ -52,7 +52,7 @@ Use the roles from `tokens.json` (`--act-bg`, `--ink`, `--soft-bg`, `--bubble-bg ``` -The app injects `src/tokens.css` into the iframe and sets ``, so a drop-in uses the same `var(--role)` names as the React screens and needs no colors of its own. +The app injects the token variables into the iframe and sets ``, so a drop-in uses the same `var(--role)` names as the React screens and needs no colors of its own. ## Add a surface diff --git a/north-star/src/main.tsx b/north-star/src/main.tsx index a1453da..f4831fe 100644 --- a/north-star/src/main.tsx +++ b/north-star/src/main.tsx @@ -1,7 +1,10 @@ import { createRoot } from "react-dom/client"; -import "../../src/tokens.css"; +import tokensCss from "virtual:kilo-tokens"; import "./shell/theme.css"; import "./shell/shell.css"; import { App } from "./App"; +// Tokens first, so the app's own styles can override nothing by accident. +document.head.insertAdjacentHTML("afterbegin", ``); + createRoot(document.getElementById("app")!).render(); diff --git a/north-star/src/shell/ScreenView.tsx b/north-star/src/shell/ScreenView.tsx index 6dbc4bd..4256f17 100644 --- a/north-star/src/shell/ScreenView.tsx +++ b/north-star/src/shell/ScreenView.tsx @@ -1,6 +1,6 @@ import { useEffect, useRef } from "react"; import type { Screen } from "../registry"; -import tokensCss from "../../../src/tokens.css?inline"; +import tokensCss from "virtual:kilo-tokens"; export type Mode = "dark" | "light"; @@ -30,7 +30,7 @@ function HtmlScreen({ screen, mode }: { screen: Extract]*>/i, (head) => `${head}`)} onLoad={onLoad} style={{ inset: 0, width: "100%", height: "100%" }} diff --git a/north-star/src/shell/theme.css b/north-star/src/shell/theme.css index a3339ad..9d86584 100644 --- a/north-star/src/shell/theme.css +++ b/north-star/src/shell/theme.css @@ -1,6 +1,6 @@ /* ========================================================================== - North-star shell. Every design value comes from ../../../src/tokens.css, - generated from the repo's tokens.json. Do not define token values here. + North-star shell. Every design value comes from the repo's tokens.json + (see ../../vite.config.ts). Do not define token values here. ========================================================================== */ *{box-sizing:border-box;margin:0;padding:0} html{-webkit-font-smoothing:antialiased} diff --git a/north-star/src/surfaces/components/screens/Parity.html b/north-star/src/surfaces/components/screens/Parity.html index 26538d1..8b67d8d 100644 --- a/north-star/src/surfaces/components/screens/Parity.html +++ b/north-star/src/surfaces/components/screens/Parity.html @@ -10,7 +10,7 @@