Skip to content
Toolkit

ADR-0039: The component whose stylesheet half Studio already had — CodeEditor, and the second vocabulary nobody knew about

Context

ADR-0037 left a standing instruction that ADR-0038 restated: a remaining authoring row is "a claim to re-check, not a fact." Three rows have turned out to be deletions, one turned out to be a kind rather than a name, and one was what it said it was.

CodeEditor is the sixth, and it is a new shape again: the row was real, and half of what it needed was already in Studio, shipped and unused.

The census

ProbeResult
files named CodeEditor1 — apps/web/src/components/ui/CodeEditor.tsx, 319 lines
<CodeEditor in the working tree4 — BiolinkSettingsPanel.tsx:462/:474, SplashPagesPage.tsx:344/:356
<CodeEditor in any commit, pickaxe over --all5 commits, and in every one the only files holding it are those same two
is either file a story, a preview or its own definition?no — one is a page, one is an editor panel
are both reachable?yes — SplashPagesPage is routed; BiolinkSettingsPanel from BiolinkEditorPage.tsx:438
a storynone exists
a test naming itnone, direct or indirect. Zero assertions covered it

Four call sites, two files, stable across the whole history. The row survives.

What Studio already had, and did not know it had

Asking ADR-0027's question — what is the real counterpart? — turned up no component. CodeEditor, CodeBlock, Code, Editor are exported by nothing; Textarea is a bare <textarea>; the EditorShell family is a full-viewport document surface, the ADR-0029 decoy.

But studio/packages/core/src/80-code.css has shipped since the kit was written: seven syntax classes — .tok-k keyword, .tok-s string, .tok-n number, .tok-a attribute, .tok-f function, .tok-c comment, .tok-p punctuation — fed by six --code-* custom properties that flip per theme. Its only consumers are two static demo pages.

The app, meanwhile, had grown its own: tok-kw, tok-str, tok-num, tok-key, tok-com, tok-fn, carrying six hard-coded oklch() literals and a [data-theme='dark'] override for five of them.

Two syntax vocabularies for one job, in one repository, colliding on not one class name, neither aware of the other. This is the hand-copied token file (ADR-0004) in a second medium, and it is exactly what a single source of truth is supposed to prevent.

It is also the reason the app grew one. 80-code.css is in the full bundle and in none of the four layers apps/web/src/index.css imports. An app author wanting coloured code had nothing to reach for, so they wrote it again — and the ADR-0023 trap, which has bitten every conversion since DataTable, here bit in the authoring direction for the first time: not "the component names a class the layer lacks", but "the layer has a class no component names, where the consumer cannot see it."

Four editors, none of them named, and none of them monospaced

ADR-0026 counts what is reachable. Of twelve declared props, five were ever passed: value, onChange, language, placeholder, minHeight. label, error, className, disabled, readOnly, lineNumbers and validateJson have zero call sites, which makes is-disabled, is-invalid, the error <span>, the whole JSON path (jsonError, formatJson, the format button, the json ruleset) and the lineNumbers === false branch unreachable code in production.

label is the one that matters. All four call sites hand-rolled a <label> above the editor — two spellings, small medium muted and tiny medium muted, neither the component's — with no htmlFor, over a <textarea> with no id. So the component's own label prop rendered nowhere, and every code editor in the product was an unnamed textarea. This is the defect ADR-0032 found 32 times behind Input, ADR-0035 recorded against FormField and ADR-0038 against MultiSelect — a fourth time, and the same answer: labelling is Field's.

And the surface was never monospaced. .code-editor-input set font-family: var(--font-mono), which is var(--font-label) — Space Grotesk, proportional, marked deprecated in DESIGN.md's own typography section. A code editor with tab-size: 2 in a proportional face is an editor where a column means nothing. Studio ships no monospaced file at all, so there was no token to reach for either.

The keyboard trap

handleKeyDown called e.preventDefault() on every Tab:

if (e.key === 'Tab') {
  e.preventDefault()
  … insert two spaces …
}

No shiftKey term, so Shift+Tab inserted two spaces too. No readOnly or disabled term, so a read-only editor both edited itself and refused to be left. There is no other key that moves focus out of a textarea.

A keyboard user who reached one of these four editors could not leave it by any key at all. That is WCAG 2.1.2 (No Keyboard Trap), level A — the one success criterion whose failure can strand a user on a page — and it shipped on four call sites for the component's whole life, with no test to notice.

Decision

Author CodeEditor into Studio, on Studio's existing syntax vocabulary; move the --code-* tokens into the token layer so a component-layer consumer can reach them; convert all four call sites to Field + CodeEditor; delete the app's.

Like ADR-0038 this is an authoring and a conversion on one commit, and the app component is removed rather than kept, because the app was left owning nothing.

The vocabulary question, answered by restatement rather than by renaming

Studio's .tok-* is the survivor. The alternative — namespaced .lk-code__tok--k classes — would satisfy the layer's containment check just as well and would have left the system with two vocabularies again, one of them Studio's own and still unreachable. So the component emits .tok-k and the component layer restates the seven rules scoped under .lk-code:

.lk-code .tok-k { color: var(--code-key); }

The leftmost compound is ours, so the rule can only match inside a .lk-code and the namespace check passes. This is .glass's precedent from ADR-0024, where a non-lk- class could not be exported into the layer and was duplicated under a namespaced selector instead. The duplication is seven single-var() lines, so the two copies cannot drift on anything but the mapping itself.

The --code-* values move to the token layer, which is the other half and the more important one. A token file declaring tokens outside the token layer is the defect this system exists to end, and ADR-0020 rule 3 says a component may not depend on a different layer for its own styling. They could not be added to the component layer's allowlist: 80-code.css declares them on :root and [data-theme="dark"], and the containment check rejects a stray subject on the first build. In tokens/studio.tokens.json they are semantic.code, light and dark, and 80-code.css now only spends them.

--font-code is new and is a system stack, not a self-hosted face: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace. A code face is the one place a reader's own configured monospace beats a downloaded one, and shipping a fifth font file for four call sites is not a trade this workstream should make. --font-mono is left exactly as it was — deprecated, aliasing --font-label — because other consumers still spend it.

Six raises over what the app had

ADR-0019: the authored component is better than the thing it replaces, and the difference is stated rather than absorbed.

  1. The keyboard trap is gone. Tab still indents. Shift+Tab now always moves focus. Escape arms a forward exit that the next Tab spends, and any other key re-arms indentation — the two-step model every serious editor uses. A read-only or disabled editor lets Tab straight through.
  2. The exit is told, not discovered. A non-standard keyboard exit is only conformant if the user is advised of it, so exitHint renders in the toolbar (a sighted keyboard user needs it too) and is joined onto aria-describedby rather than replacing it, so a Field's helper survives beside it. It is always in the DOM: a hint rendered only on focus would be a dangling IDREF for as long as the editor is idle. It is not .visually-hidden — the class ADR-0023 caught a component naming and not having, and which this app still does not import.
  3. The control is named. id, aria-invalid and aria-describedby land on the <textarea>, which is the tab stop, so <label for> reaches what the user types into. Four unnamed controls named.
  4. The paint layer is nodes, not an HTML string. The old one built markup by hand and injected it with dangerouslySetInnerHTML, which made escaping its own problem and made find-highlighting a regex over its own tags — its comment called marking inside a tag "acceptable collateral". Emitting React nodes makes escaping the framework's job and lets the query be applied to token text, where a match cannot be part of the markup because there is no markup yet. So the marking is now exact, and <img src="x" onerror="boom"> in a CSS box is text in every sense.
  5. Copy can fail, and says so. The old one awaited navigator.clipboard.writeText bare: a refusal was an unhandled rejection and the button claimed success, because setCopied(true) sat on the line the throw skipped. It is guarded twice now, and the button's accessible name carries the confirmation rather than the swapped glyph alone — title="Copy" said the same thing before and after.
  6. The JSON verdict is announced. It was a bare <span> with no role and no id, so a parse error reached nobody. It is a role="status" inside the frame, wired into aria-describedby — and status, not alert, because it changes on nearly every keystroke while an object is half-typed and an assertive region would interrupt the typing it describes.

Two props did not survive, and one is a genuine split

label and error are Field's, on ADR-0032's and ADR-0038's precedent. Neither had a call site.

validateJson does survive, and the reason is a distinction worth naming: the parser's message is not the field's error. The caller did not supply it and cannot clear it — it is the editor's own live reading of its content, like a linter gutter — where Field's error is the form's verdict. They render in different places and a call site can show both at once. Keeping the JSON path also keeps ADR-0019's superset rule honest: it is unreachable in production today only because no call site edits JSON yet.

Two deliberate visual changes

The labels change, at all four sites, and in two different directions, because the two files disagreed with each other and neither agreed with the spec. DESIGN.md's field anatomy is label[for] at 13px/600 in primary ink, which is what .lk-field > label draws. What was there:

beforeafter
BiolinkSettingsPanel ×2tiny medium muted mb-1 — 11.5px, 500, --on-surface-2, 4px gap13px, 600, --text, 6px gap
SplashPagesPage ×2small medium muted mb-2 — 13px, 500, --on-surface-2, 8px gap13px, 600, --text, 6px gap

So one pair gets noticeably larger and darker and the other only heavier. That is the point rather than a side effect: these were the only labels in either file written by hand instead of by Field, they were two spellings of the same intent, and neither was the anatomy every other field in the product already uses. Recorded rather than absorbed, on ADR-0032's precedent.

And the frame gains a fill. The .framed box the app drew had no background; the new .lk-code is --surface-2, and its focus state swaps to --surface with the lime border and the 3px glow. That is DESIGN.md's field spec, which every other member of the input family already obeys and this control did not — so a code editor and a textarea beside it now light up identically (ADR-0023's second axis: two components that render against each other agree on the values that make them line up). Recorded rather than absorbed.

Consequences

The app CSS: twenty rules died, and one that looks dead did not

ADR-0021. Counted with ADR-0037's method, re-implemented for this item and validated by reproducing all twenty-five of ADR-0038's published numbers exactly before being used for anything.

classbefore → afterclassbefore → after
framed1 → 0text-link63 → 60
is-disabled1 → 0success-text8 → 7
is-compact1 → 0faint130 → 128
code-editor1 → 0control58 → 57
code-editor-toolbar1 → 0grow54 → 53
code-editor-find1 → 0tiny102 → 99
code-editor-surface1 → 0ic-btn6 → 5
code-editor-gutter1 → 0small444 → 440
code-editor-stack1 → 0medium166 → 161
code-editor-highlight1 → 0muted375 → 370
code-editor-input1 → 0danger-text47 → 46
field4 → 3mb-122 → 20
is-invalid5 → 4mb-241 → 39
row-between32 → 31row-sm24 → 22
caption-md23 → 22

.medium and .muted fall by five rather than four because the label the component rendered for its own label prop was a fifth — and no call site ever passed that prop, so it was never on screen.

.framed, .framed.is-invalid, .is-disabled, the seven .code-editor-* rules, .code-editor-find .control.is-compact and the eleven .tok-* rules are deleted. .framed-head stays, and staying is a finding: it has no emitter anywhere and had none before this item either, so it is pre-existing dead rather than this conversion's to delete. It is also the reason the app and Studio disagreed about whether a code toolbar has a background — the component never used the header rule that sat directly beneath the frame rule it did use, hand-rolling .row-between .code-editor-toolbar instead.

The preview card had to be retired first. preview/current-design.html:498 was the only emitter of code-editor-surface and code-editor-stack outside the component, and that page <link>s the app's real stylesheets, so deleting the rules would have broken its own card. This is ADR-0021 step 5 doing exactly the job it was written for — the third time the preview page has been load-bearing on a deletion, after .checkbox (ADR-0036) and .check-tile (ADR-0038), and the first where retiring the card was enough to let the rules go.

.is-compact keeps one emitter, on current-design.html:226, and it was already inert there: .code-editor-find .control.is-compact was the only rule that name ever had in this app.

Types: CodeEditor is the 37th of Studio's 115 React exports to be typed

Twenty deliberately wrong calls, all rejected: a language outside the union, a numeric value, a DOM-event onChange, a CSS-length minHeight, string lineNumbers and validateJson, rows, the four input-assist attributes the component writes unconditionally, children, defaultValue, the two retired props, an object className, a boolean exitHint, and a ref — which reaches the textarea at runtime and is deliberately not declared, the house gap ADR-0033 records. The probe was mutation-checked — one illegal call made legal, its @ts-expect-error correctly reported unused — and deleted before commit.

Unlike ADR-0038's finding, no hyphenated ARIA attribute is at stake here: the four this component must own are not ARIA at all but spellCheck, autoCapitalize, autoCorrect and autoComplete, which are valid identifiers and can be omitted. The runtime guarantee is still the rest-first spread, and a test pins it.

The tests: 57 assertions, 39 mutations, 39 killed

apps/web/src/components/CodeEditor.test.tsx. The component had zero coverage before, direct or indirect.

Four mutations survived the first pass, and every one was a defect in the tests rather than in the component — which is the pass working:

  • Asserting toContain('1 match') is satisfied by "1 matches", so a component with no singular at all passed. Substring checks lie, exactly as the Method says.
  • Asserting the invalid rule by toContain on its selector stayed true after the rule was deleted, because that selector is a prefix of the :focus-within rule beneath it. Both now assert the declaration.
  • Nothing asserted that language="javascript" — the string both JS call sites actually pass — tokenizes. The existing test used js directly and passed happily against a component that had forgotten the alias.
  • Escape was only ever pressed in the find row's own input, which has its own handler, so the textarea's close-find branch was untested.

The clipboard mutation is worth its own line because it found a defect in the component, during authoring rather than after: written as await navigator.clipboard?.writeText(v), the optional chain evaluates to undefined, which awaits happily and falls straight through to the success path — so a browser with no clipboard API said "Copied" having copied nothing. The absence needs its own branch, and now has one.

What is not fixed here

  • .tok-* is still outside the component layer for a non-.lk- consumer. A host that imports css/components gets the seven rules only inside a .lk-code. That is correct for this component and does not help a host that wants to paint a static <pre>; the answer, when one appears, is a CodeBlock that carries its own restatement, not moving 80-code.css into the allowlist, which its :root subjects forbid.
  • The ADR-0023 completeness gate still does not exist. This component emits twenty-four class names — fifteen .lk-code*, the borrowed .lk-input and .lk-icon, and the seven syntax classes — and nothing in the repository checks that the layer defines them. The enumeration was done by hand, then re-run as a script against the built layer, then pinned by tests; all twenty-four resolve. It caught one: .lk-code__hint was named in the markup before it had a rule at all, which is the ADR-0023 defect committed and found inside a single sitting. The mechanical form ADR-0023 proposed — collect every className literal in packages/react/src/*.jsx, subtract what the standalone layers define, fail on the remainder — would have caught it for free, and is still a gate change rather than a component change.
  • The find affordance still only counts. There is no way to jump between matches, which the control it replaces also lacked. Recorded as an open item rather than widened into here.
  • Every token in the toolbar is a tab stop ahead of the textarea, so reaching the code takes three presses. Same shape as the MultiSelect item ADR-0038 recorded; decided once for both, not differently here.

Source: docs/decisions/0039-the-component-whose-stylesheet-half-studio-already-had.md

Library