Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
12 KiB
Variable collections, shared libraries and assets
Why
Variables are a flat list of colors plus untyped tokens, copied into every page's Document and kept in step by hand (two writers fan out to every page, the remove buttons in src/ui/confirm.rs do not). There are no groups, no types beyond color, no aliases, and only fills can bind. Components work only on the page that holds the main: the Design tab lists the active page's components, so a Design System page's components are invisible everywhere else, and create_instance / swap_instance refuse other pages. Nothing can be shared between files except a one-off package import.
Goal: Figma's model, local-first. Collections with modes, slash groups, typed variables with aliases; components usable from any page; any file can publish itself as a library that other files enable and update; a proper place to see and edit all of it.
Decisions (user, 2026-10-21)
- Libraries: Figma-style publish. A file publishes a versioned snapshot; consumers keep a copy and get "Updates available". Files keep working when a library is missing.
- Variables: collections + groups + types + aliases, number variables bindable to geometry and type.
- UI: a Variables modal, a Libraries modal, and an Assets side view. Design mock-ups first.
What exists today (checked 2026-10-21)
- Storage:
Document.variables(active colors),modes,mode,tokens(src/model/document.rs:15), one copy per page. Writers:Tx::set_variable,remove_variable,rename_variable,set_token,remove_token,set_design_system,remove_mode,set_variable_in_mode,set_mode(src/history.rs); UIon_every_page(src/ui/design.rs:15); MCPset_variables,set_tokens,set_variable_mode,import_design_system,create_page(src/mcp/exec.rs). - Binding:
Paint::Solid { color, var: Option<String> }holds the CSS name; the color is baked in andapply_variablerewrites it. Renderer, SVG and PDF read the baked color only. Tokens are substituted as text when CSS is written (css::set_variables_in_scope), no link kept. - Readers: codegen (
:root,[data-mode], Tailwind, DTCG),get_variables,tokens_css, lint (lint.rsmatch and auto-link),diff_pages, inspector var picker. - Components:
Node.component,InstanceLink { main: NodeId, overrides };Editor::fixupsyncs instances on the sameDocument.import_componentscopies mains between pages with no link back. - Project:
project.json(version 2: v1 to v2 is a no-op manifest step; the version bump makes older apps refuse),pages/<id>.json(version 3, migration chain),versions/,runs/. Projects live in one store folder; relay library mode already syncs every project to every device. - Figma import ignores variables and does not link instances.
Model
Variables (project level, design.json in the project folder)
DesignSystem
collections: [Collection]
Collection { id, name, modes: [Mode { id, name }], default_mode, variables: [Variable] }
Variable { id, name: "color/brand/primary", kind: Color | Number | String | Boolean,
values: { mode_id: Literal | Alias(variable id) }, description, hidden: bool }
- Groups are the
/segments of the name (color/brand/primarysits in groupcolor/brand). Renaming a group renames every variable under it. - CSS name: segments joined by
-, sanitized (--color-brand-primary). Two variables with the same CSS name across all enabled collections are refused at write time. - Aliases point to a variable of the same kind in any collection, including a library's. Cycles are refused. A value resolves through aliases per mode.
- Active mode is per page per collection (
PageFile.modes: {collection_id: mode_id}, default =default_mode). A new page inherits the active page's choice. - Numbers carry no unit; they bind as px. Strings substitute as text (font family, shadow). Booleans bind to visibility.
- One source, one cache.
design.jsonis the only stored truth. Each page'sDocument.variables/modes/mode/tokensbecome a derived cache rebuilt from it (resolved with that page's modes, keyed by CSS name), so the renderer, codegen, lint, diff and export keep reading what they read today. Every writer goes through one design-system API that updatesdesign.json, rebuilds every page's cache and re-applies bindings in one undo step per page. - Migration (project version 1 → 2): the first page's
variables+modesbecome collectionColors(one mode per existing mode, elseDefault);tokensbecome collectionTokens(a px value such as16pxbecomes Number; anything else, including unitless1.5, stays String). Existing fill bindings keep working because CSS names do not change. Pages keep their fields as the cache. The manifest version is 2, so an older app refuses the project ("made by a newer PaperCraft") rather than write pages without bindings and never updatedesign.json.
Bindings
- Fills keep
Paint::Solid.var(CSS name). - New
Node.bindings: BTreeMap<field, css name>forstroke(color),text-color,width,height,gap,padding(all four or per side),radius,font-size,line-height,opacity,visible(boolean). A bound field is re-applied when the variable, an alias target or the page's mode changes, exactly like fills today. Detaching writes the resolved value and drops the binding. - Instances inherit bindings from mains; an overridden field drops it on the instance only.
Components across pages
- Every main gets a stable
component_key(created oncreate_component, migrated for existing mains). InstanceLinkgainssource: Option<Source>:None= same page (current behaviour),Page(page_id),Library(library_id).mainkeeps pointing at the node id inside that source document.- Editing a main on page A re-syncs instances on every other page whose link is
Page(A), in the same commit (one undo step per touched page). Deleting a main leaves its instances as they are, marked "main missing" (Assets offers Detach or Restore). create_instanceandswap_instanceaccept a component from any page or enabled library (componentIdaspageId:num, orcomponentKey).import_componentsstays (copy) but is no longer needed for reuse.
Libraries
- Publish (Libraries modal, or MCP
publish_library): writeslibrary.jsonin the file's project folder:{ library_id (= project id), name, description, version (counter), published (ms), collections (non-hidden variables), components (every main not named with a leading_or., with its subtree and a 256px PNG preview) }. A diff against the previous publish is shown first ("3 variables changed, 1 component added"). - Available libraries = every project in the store with a
library.json(relay library mode makes other devices' libraries appear with no extra work). - Enable in a consumer copies the snapshot to
<consumer>/libraries/<library_id>.jsonand records{library_id, version}indesign.json. Library collections show read-only in the Variables modal (aliasable, bindable); library components show in Assets. - Updates: when the library's
versionis newer than the copy, the Libraries modal and a dot on the Assets tab say "Updates available". Review lists changed variables and components; Update replaces the snapshot, rebuilds caches and re-syncsLibrary(id)instances (instance overrides kept). Updates are never applied silently. - Missing library: the copy keeps everything working; the modal shows "Source not found" with Disable (bindings are detached to resolved values, instances detached) after a confirm.
- Disable with uses left: confirm, then detach as above.
UI
- Variables modal (from Design tab "Open variables", palette "Variables…", ⌘⌥V): left column collections (local, then each library, read-only) with their group tree; main area a table, one row per variable in the selected group, one column per mode; cells edit inline (swatch + hex, number, text, toggle) with an alias picker ("→ color/brand/primary"); add variable (type menu), add mode, add collection, rename and delete (confirm), search, a details pane (description, hidden from publishing, CSS name, where used count). Copy CSS / JSON / Tailwind move here.
- Libraries modal (Share menu "Libraries…", palette, Assets header): "This file" (publish state, description, changes since last publish, Publish), "Libraries" (available libraries with Enable toggles, version, source status), "Updates" (per library review and Update).
- Assets side view (replaces the Library tab icon slot, keeps My shapes as its last section): search; sections This file (grouped by page), each enabled library, My shapes; previews in a grid; click to place at the viewport centre, drag onto the canvas; context menu: Go to main, Detach all, Rename (local only). Instances whose main is missing show a warning row.
- Design tab slims to: modes per collection for this page, a short variables summary with "Open variables", lint. The components list moves to Assets.
- Inspector: every bindable field gets the variable picker (filtered by kind), a bound field shows the variable chip with Detach.
Mock-ups: PaperCraft design file, new page Design libraries, artboards Modal / Variables, Modal / Libraries, Modal / Library updates, Panel / Assets (344x1440 like the other panel states), Inspector / Bound fields (344x1440). Modals drawn at 2560x1440 window size and checked at 1280x800.
MCP and CLI
get_variablesreturns collections, groups, kinds, per-mode values and aliases (current formats keep working; DTCG nests groups).set_variablesgainscollection,type,mode,alias,description,hidden; old calls (color by name) still work and land in the first collection.- New:
create_collection,delete_variables,bind_variable(node field ← variable),publish_library,list_libraries,enable_library,update_library,disable_library. get_componentslists every page and enabled library withcomponentKeyandsource.- Every new tool is classified in
edits_doc(the existing test enforces it).
Not in scope (v1)
- Text styles, effect styles, grid styles (roadmap row 9; next).
- Component variants and properties (row 10).
- Per-frame mode override (mode is per page).
- Live links between files without publish.
- Figma variable import (separate item; the model now has room for it).
Acceptance
- Migration: a project with variables, modes and tokens opens as version 2 with collections
ColorsandTokens; every existing fill binding still resolves; 0.4.0 refuses a migrated project with a clear "made by a newer PaperCraft" error instead of losing data. - Groups: renaming group
color/brandrenames its variables and every binding follows. - Aliases resolve per mode; a cycle is refused; deleting an aliased variable asks first and detaches dependents to resolved values.
- Number bindings: changing
space/mdfrom 16 to 24 updates the gap of every bound frame on every page in one undo step per page; switching a page's mode updates its bound fields only. - Removing a variable or mode from the UI affects every page (fixes today's page-only removal).
- An instance on page B of a main on page A updates when the main changes; undo on A and B each restores its side.
- Publish → enable in another project → instance and binding work; change and republish → consumer shows Updates available → Update re-syncs instances and keeps overrides; deleting the library project leaves the consumer working with "Source not found".
- Two libraries defining
--color-bgcannot both be enabled without a clear error. - Variables modal, Libraries modal and Assets view match their mock-ups at 1440p and 1280x800 (screenshot check).
cargo test --no-fail-fast, wasm and Windows checks pass; web build loads.