Skip to content

Importing existing CSS

Importing means turning a theme you already ship, hand-written CSS custom properties, into Airframe token files, and proving the switch changes nothing. airframe import css reads your stylesheets, maps each block to a mode key, writes the token files, and then checks that the CSS airframe build would generate from them resolves every custom property to the same value as your stylesheets do, in every browser environment they can tell apart. If either step finds an error, nothing is written.

This guide is for teams with a CSS theme (:root { --surface: …; } plus dark-mode and density overrides) who want Airframe to own it from now on. Pass token-only CSS files: a rule with a selector other than :root (plus attribute conditions), or an at-rule such as @font-face, fails the import by design, so strip those rules or split them into another file first.

Import runs inside a project: it needs an airframe.yaml, and it never invents a mode. Each axis’s web: spellings are how the importer recognises a block, so they must match the selectors and media queries your CSS already uses. For a theme with a dark mode (by system preference unless a data-theme attribute says otherwise), a density attribute that coarse pointers default to relaxed, and a per-app accent:

version: 1
content: design-system
modes:
appearance: {values: [light, dark], binding: runtime, web: {light: ":root", dark: ["@media (prefers-color-scheme: dark) :root:not([data-theme='light'])", ":root[data-theme='dark']"]}}
density: {values: [compact, standard, relaxed], base: standard, binding: runtime, web: {attr: data-density, also: {relaxed: "@media (pointer: coarse)"}}}
app: {values: [drafts, slides], base: drafts, binding: runtime, web: {attr: data-app}}
platforms:
web: {emit: css}

Write the file by hand, or start from airframe init and then delete the *.tokens.json files it wrote under design-system/: the import writes its own, and the equivalence check does not see others (see Limitations). Every axis needs a web: spelling, and a platform must emit css.

The repository’s testdata/import directory holds this config and a theme.css to try it on. Copy it somewhere outside the checkout first: run in place, the import writes token files into the repository.

Terminal window
cp -R testdata/import /tmp/airframe-import && cd /tmp/airframe-import

Check the import first, writing nothing:

Terminal window
airframe import css --dry-run theme.css
imported 25 tokens from 38 declarations (1 non-custom ignored); equivalent in 192 environments across 25 properties
would write design-system/tokens/primitive.tokens.json
would write design-system/tokens/semantic.tokens.json

The summary says what was read (color: var(--ink) is not a custom property, so it is counted and ignored) and what was proved: 192 combinations of data-theme, data-density, data-app, prefers-color-scheme and pointer, each property compared in each. Then import for real:

Terminal window
airframe import css theme.css
imported 25 tokens from 38 declarations (1 non-custom ignored); equivalent in 192 environments across 25 properties
wrote design-system/tokens/primitive.tokens.json
wrote design-system/tokens/semantic.tokens.json

Each block became a mode key: :root[data-density='compact'] is density:compact, and both :root[data-theme='dark'] and the prefers-color-scheme block are appearance:dark. A comment right before a :root declaration becomes its $description, as does one after its ; on the same line (--gap: 4px; /* row gap */) when none comes before it; a trailing comment never describes the next declaration. Pass several files to import them together; later files win the cascade, as in the browser.

The import refuses to overwrite either token file (exit 2) unless you pass --force, and --dry-run refuses the same way, so a dry run predicts the real one. With --force, a token file this import does not produce (the semantic file, when the CSS has no var()) is removed and listed as removed <path>, so the project holds exactly what was checked. Exit status is 0 when it wrote the files (warnings allowed), 1 when it found an error, and 2 for a usage or I/O error.

Each finding names the file and line. Every ID has a page at https://airframe.uijig.com/rules/<id>/, and airframe explain <id> prints it.

Finding Means Usual fix
import-css-syntax The CSS is malformed, or nested Close the block, string or comment; un-nest rules
import-unsupported-at-rule @layer, @supports, @import… Move the properties to the top level or an @media block
import-unsupported-selector Not :root plus attribute conditions Theme the root: :root[data-theme='dark'], not .dark
import-unsupported-media A range feature, not, or, a media type Keep breakpoints out of the token import
import-unmapped-scope No mode key is spelled that way Add the value, or fix the web: spelling, in airframe.yaml
import-unsupported-value calc(), a keyword, !important, a dangling var() Write a literal, or alias a token
import-name-invalid Not lowercase words joined by - Rename the property and its readers
import-type-conflict A colour in one mode, a length in another Split it into two properties
import-no-base Set only under a mode, never on bare :root Give it a base value on :root
import-duplicate (warn) Declared twice for one mode Delete the losing declaration
import-fallback-dropped (warn) var(--x, fallback) Drop the fallback; the alias alone is imported
import-not-equivalent The generated CSS differs somewhere Make the config’s spellings match what the CSS does
import-too-many-environments Over 200,000 environments to check Import fewer files, or fewer unrelated attributes
import-nothing Nothing to import, or nothing to check against Pass the theme’s files; add a platform that emits css
  • Types come from values, and two from names. A word list is a fontFamily only when the property name ends in -font, -family or font-family; 400 is a fontWeight only when the name contains weight. Otherwise 400 is a number and bold is an unsupported value.
  • A unitless 0 is a number. --x: 0 on :root with --x: 4px in a mode is a type conflict; write 0px.
  • A quoted generic is a font name. "sans-serif" (quoted) names a font called sans-serif, not the generic family, as in the browser; it stays quoted in the token file and the generated CSS.
  • html is rejected. It matches the root element but with lower specificity than :root; spell it :root.
  • Only discrete media features, joined by and: prefers-color-scheme, prefers-contrast, pointer, hover, forced-colors, display-mode and the like. No min-width, not, or or media types.
  • !important is rejected. Airframe never emits it.
  • The check covers the imported files only. It compares your stylesheets with the CSS generated from the two files the import writes, not with other token files already in the project. Import into a project without its own token files, or check the combined result yourself.
  • Tiers are a first guess. A token whose values are all literals is primitive; one with any var() is semantic; none is component.
  1. Assign tiers. Read both files and set each token’s tier to what it really is: a token one component owns is component, and a semantic token should read primitives rather than hold literals. Move tokens between files as you like; the file a token is in does not set its tier.
  2. Build. airframe build writes the generated CSS; replace your hand-written stylesheet with it.
  3. Lint. airframe lint now holds the tiers, aliases and modes; fix what it reports, and add airframe lint --format gate and airframe build --check to CI.