Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,8 @@ Reference an elevation token from a node as `shadow: "$elevation.raised"` (resol

Then use in nodes: `{ fill: "$primary", padding: "$md", cornerRadius: "$sm", transition: "$motion.fast" }`

A name defined in both `spacing` and `radius` (the presets define `sm`, `md` and `lg` in both) resolves by the property it sits on: `cornerRadius` reads `radius` first, so `cornerRadius: "$sm"` above is 4; every other property reads `spacing` first, so `padding: "$md"` is 16.

`get_variables` leaves out a colour still holding an old scaffold seed (`accent-tint` `#152A47`, `success-tint` `#0F2E1B`, `warning-tint` `#33260A`, `danger-tint` `#3A1715`, `neutral-tint` `#25282D`, `danger` `#F87171`, matched by name and value, any case): it counts as unset, so an inherited value or the derived one shows through, and the canvas's `sha256r:` version hash moves once (an approval recorded on it reads STALE). The stored JSON is untouched. A `$ref` no token defines is an `unknown-token` warning (see `canvas_evaluate`); copy props (`content`, `label`, `value`…) never resolve.

### `workspace_set_design_system` / `workspace_get_design_system` / `workspace_apply_preset`
Expand Down
2 changes: 2 additions & 0 deletions docs/GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ Authoring rules:
- **It ships a dark theme too.** The dark mapping lands in a sparse `dark.colors` override layer alongside the light tokens (or set one by hand via `set_variables`, which also accepts `dark.elevation` for depth-token overrides) — status colors get a second, dark-specific pass here so they clear AA against the dark surface as well. Render it with `theme: "dark"` on `screenshot` / `screenshot_responsive` / `export`; `canvas_evaluate`'s `color` category then contrast-checks both themes automatically and scores the worse one — dark-run failures point at the dark token layer (a node literal would break light). Contrast is measured as rendered: translucent fills and text (`rgba`, its alpha a fraction or a percent, or `#RRGGBBAA`) composite over what is behind them, so a tinted selected or hover row is judged by its real color. WCAG 2.2 is the gate; **APCA** Lc values appear as info-only advisories on perceptually weak pairs (APCA is a candidate method, not a standard — it never blocks).
- **Give motion the same treatment as color and type.** Declare `motion` tokens via `set_variables` (`{ fast: { duration: 150, easing: "ease-out" } }`) and reference them as `transition: "$motion.<name>"` — ad-hoc duration/easing pairs sprawling across a canvas with no declared motion tokens get an advisory.

A name defined in both `spacing` and `radius` (the presets define `sm`, `md` and `lg` in both) resolves by the property it sits on: `cornerRadius` reads `radius` first, every other property reads `spacing` first. Under the dark preset `cornerRadius: "$md"` is 8 and `gap: "$md"` is 16.

Merge semantics are per-category: a project that only sets `colors` doesn't reset the workspace's `spacing`/`radius`/`typography`. A canvas that only overrides `colors.primary` keeps every other workspace color.

## Designing with character
Expand Down
2 changes: 1 addition & 1 deletion docs/specs/PHASE-32-SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ And one is housekeeping the follow-ups will land in. 140 tests and `test-env.ts`
**Slice C — verify round 1 (recorded 2026-11-21).** Four verifiers on fc3da95 (spec conformance, mutation, review and gate attack, docs-steward) and a cold-reader. Fixes, each with a test red on fc3da95:
- **V1:** `getCanvasTokens` threw on a stored canvas with no `variables` key (`withoutRetiredSeeds` read `v.colors` unguarded); a colour layer holding only seeds now drops whole, so it hashes as absent, not `{}`. An override written inside a component definition was one finding per outer instance; its origin is now where the instance node is written. Lone letters (`m`, `s`, `l`) are not ladder steps, so `$space-m` → `$space-md` is fixed. An id-less node inside an instance no longer gets the id `<instance>/undefined` (`expandTree`). The viewer's token-source label and `generate_color_system`'s `overwrote` list read through `withoutRetiredSeeds`.
- **V3 (changed M3, M4, M5, M7):** `$colors.accent-tint` and the other dotted derived names are valid. The tint surface reads the same chain as every other default (`bg-surface`, `surface`, `bg-primary`, `background`), and N1's seeding skips a canvas that resolves any of them (a design.md import names its colours `surface` and `background`). A token whose value is itself a `$ref` (`brand: "$accent"`) is a fifth case: the resolver does not follow it, so it renders raw; it warns, no fix. The dark-only suggestion names the elevation layer for an elevation ref. Candidates are names within two edits (three padded the list with noise: `$space-mdd` suggested `$space-2xl`). The Tailwind importer no longer maps non-colour `text-` and `bg-` utilities (`text-balance`, `bg-cover`, `bg-gradient-to-r` and the like) to colour refs.
- **Not fixed, named:** `lookupToken` reads spacing before radius, so `cornerRadius: "$md"` under a preset that defines both renders the spacing value (16, not 8). The rule sees one `number` kind and stays silent, and the README's own `get_variables` example (`cornerRadius: "$sm"`) hits it. It predates this slice and changing the order changes existing renders, so it is a follow-up. A node id containing `"` gives an op the U parser rejects (pre-existing in `formatUpdateOp`). `withDerivedColors` costs about 1.4 ms per resolve, unmemoised.
- **Named, then fixed after slice D:** `lookupToken` read spacing before radius, so `cornerRadius: "$md"` under a preset that defines both rendered the spacing value (16, not 8). The rule saw one `number` kind and stayed silent, and the README's own `get_variables` example (`cornerRadius: "$sm"`) hit it (it now resolves to 4). It predates this slice and changing the order changes existing renders, so it is a follow-up. *Fixed after slice D, as its own change: `lookupToken` takes the property, and `cornerRadius` reads radius before spacing.* A node id containing `"` gives an op the U parser rejects (pre-existing in `formatUpdateOp`). `withDerivedColors` costs about 1.4 ms per resolve, unmemoised.

**Slice C — verify round 2 and the last fix round (recorded 2026-11-21).** One verifier on the round-1 fix diff (fc3da95 → 2cff888). It found:
- **Round 1's id-less fix broke attribution.** Giving an id-less node inside an instance no id (instead of `<instance>/undefined`) emptied the owners map for it, so every other rule's finding on such a node lost its instance (`nodeId` undefined) and a viewer comment on it failed. Reverted; the `…/undefined` inner id is cosmetic. Consequence, accepted: an id-less node in a component master that also sits in the tree is reported twice, once as the stored master node and once through the instance.
Expand Down
2 changes: 1 addition & 1 deletion src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ function checkUnknownTokens(canvas: Canvas, tokens: DesignVariables): Evaluation
issues.push({ ...base, message: `"${ref}" on ${path} is ${KIND_NAMES[kind]}, not ${KIND_NAMES[accepts[0]]}${where}.`, suggestion: `Use ${KIND_NAMES[accepts[0]]}${examples}.${fromOverride}` });
return;
}
const value = lookupToken(name, tokens);
const value = lookupToken(name, tokens, site.key);
if (kind && typeof value === 'string' && value.startsWith('$')) {
issues.push({ ...base, message: `Token "${ref}" on ${path} is defined as another ref ("${value}"), which the resolver does not follow, so it renders raw${where}.`, suggestion: `Set "${name}" to a value, or write "${value}" here instead.${fromOverride}` });
return;
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ const designVariablesSchema = z.object({
// --- workspace_set_design_system ---
server.tool(
'workspace_set_design_system',
'Set the workspace-level design system (inherited by every project + canvas under it). Merges per-category with existing tokens — pass `{ colors: { primary: "#..." } }` to update colors without resetting spacing/radius/typography.',
'Set the workspace-level design system (inherited by every project + canvas under it). Merges per-category with existing tokens — pass `{ colors: { primary: "#..." } }` to update colors without resetting spacing/radius/typography. A name defined in both spacing and radius resolves by property: cornerRadius reads radius first, every other property reads spacing first.',
{
workspaceId: z.string().describe('Workspace ID'),
variables: designVariablesSchema.describe('Design tokens to set'),
Expand Down Expand Up @@ -510,7 +510,7 @@ server.tool(
// --- project_set_design_system ---
server.tool(
'project_set_design_system',
'Set the project-level design system, which sits between the parent workspace and individual canvases in the resolution chain. Merges per-category with existing project tokens.',
'Set the project-level design system, which sits between the parent workspace and individual canvases in the resolution chain. Merges per-category with existing project tokens. A name defined in both spacing and radius resolves by property: cornerRadius reads radius first, every other property reads spacing first.',
{
projectId: z.string().describe('Project ID'),
variables: designVariablesSchema.describe('Design tokens to set'),
Expand Down Expand Up @@ -945,7 +945,7 @@ server.tool(
// --- set_variables ---
server.tool(
'set_variables',
'Set design variables (tokens) for a canvas. Merges with existing variables.',
'Set design variables (tokens) for a canvas. Merges with existing variables. A name defined in both spacing and radius resolves by the property it sits on: cornerRadius reads radius first, every other property reads spacing first (under the dark preset cornerRadius "$md" is 8, gap "$md" is 16).',
{
canvasId: z.string().describe('Canvas ID'),
variables: z.object({
Expand Down
16 changes: 10 additions & 6 deletions src/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function deepResolve(node: SceneNode, variables: DesignVariables): SceneNode {
if (node.letterSpacing === undefined && typo.letterSpacing !== undefined) node.letterSpacing = typo.letterSpacing;
continue;
}
const resolved = lookupToken(tokenName, variables);
const resolved = lookupToken(tokenName, variables, key);
if (resolved !== undefined) {
// Phase 27 — an elevation ref on `shadow` resolves to the structured
// shadows array (the renderer's plural path); explicit `shadows` wins.
Expand All @@ -208,7 +208,7 @@ function deepResolve(node: SceneNode, variables: DesignVariables): SceneNode {
if (Array.isArray(arr)) {
(node as unknown as Record<string, unknown>)[key] = arr.map((entry) => {
if (typeof entry !== 'string' || !entry.startsWith('$')) return entry;
const resolved = lookupToken(entry.slice(1), variables);
const resolved = lookupToken(entry.slice(1), variables, key);
return resolved !== undefined ? resolved : entry;
});
}
Expand Down Expand Up @@ -566,11 +566,15 @@ function onFill(fill: string | undefined): string {
return 1.05 / (l + 0.05) >= (l + 0.05) / (lum('111827') + 0.05) ? '#FFFFFF' : '#111827';
}

export function lookupToken(name: string, variables: DesignVariables): unknown {
// Check each category
/** Resolves a token name. `prop` is the property the ref sits on:
* cornerRadius reads the radius scale before spacing, so a design system that
* defines `md` in both renders a radius, not a spacing step. */
export function lookupToken(name: string, variables: DesignVariables, prop?: string): unknown {
if (variables.colors?.[name] !== undefined) return variables.colors[name];
if (variables.spacing?.[name] !== undefined) return variables.spacing[name];
if (variables.radius?.[name] !== undefined) return variables.radius[name];

const scales = prop === 'cornerRadius' ? [variables.radius, variables.spacing] : [variables.spacing, variables.radius];
for (const scale of scales) if (scale?.[name] !== undefined) return scale[name];

if (variables.typography?.[name] !== undefined) {
// For typography, return the fontSize as a simple value
return variables.typography[name].fontSize;
Expand Down
33 changes: 33 additions & 0 deletions test/unit/test-radius-token-precedence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// cornerRadius refs read the radius scale before spacing: a preset that
// defines `md` in both (spacing 16, radius 8) must render cornerRadius "$md"
// as 8, while gap/padding "$md" stay 16.
import '../env.js';
import { resolveVariables } from '../../src/variables.js';
import { getPreset } from '../../src/presets.js';
import type { SceneNode } from '../../src/types.js';

let failures = 0;
const check = (label: string, actual: unknown, expected: unknown) => {
const ok = JSON.stringify(actual) === JSON.stringify(expected);
if (!ok) failures++;
console.log(`${ok ? 'PASS' : 'FAIL'} ${label}: got ${JSON.stringify(actual)}, want ${JSON.stringify(expected)}`);
};

const vars = getPreset('dark')!.variables;
const node: SceneNode = {
id: 'card', type: 'frame', cornerRadius: '$md', gap: '$md', padding: ['$md', '$sm'],
children: [{ id: 'inner', type: 'frame', cornerRadius: ['$sm', 0, '$lg', '$md'] }],
} as unknown as SceneNode;
const out = resolveVariables(node, vars);
const inner = out.children![0];

check('cornerRadius "$md" reads radius', out.cornerRadius, vars.radius!.md);
check('gap "$md" reads spacing', out.gap, vars.spacing!.md);
check('padding array reads spacing', out.padding, [vars.spacing!.md, vars.spacing!.sm]);
check('cornerRadius array reads radius', inner.cornerRadius, [vars.radius!.sm, 0, vars.radius!.lg, vars.radius!.md]);

const spacingOnly = resolveVariables({ id: 'a', type: 'frame', cornerRadius: '$space-2' } as unknown as SceneNode, { spacing: { 'space-2': 6 } });
check('cornerRadius falls back to spacing when radius lacks the name', spacingOnly.cornerRadius, 6);

if (failures) { console.error(`${failures} failed`); process.exit(1); }
console.log('All radius-precedence checks passed');
Loading