Skip to content

ui-kit: Toggle hand-rolls a span switch instead of a native checkbox #2585

Open

Description

@schlessera

Context

Part of #2576.

(Toggle, packages/ui-kit/src/primitives/Toggle.tsx:64-120) draws an operable switch as <span role="switch" aria-checked tabIndex> with a hand-written Space handler (onKeyDown, Toggle.tsx:98-102). Disabled is aria-disabled plus pointerEvents: none plus tabIndex -1 (:84, :112-113). The knob moves by animating left (:89) through inline transition declarations on the track (:83) and knob (:94). Its 44px reach is .bk-switch::before (D34, documented at Toggle.tsx:13-22). It has 9 operable call sites in ui-react, plus a decorative use in (ListRow, packages/ui-kit/src/rows/ListRow.tsx:196).

A native <input type="checkbox" role="switch"> provides focus, Space, disabled, form participation and the checked state natively. The HTML switch attribute is Safari-only and its polyfill is not allowed, so role="switch" with appearance: none is the cross-engine form. Guide forms/switch: "Use the HTML switch attribute on <input type="checkbox" switch> … to build a binary on/off toggle that preserves native form submission, keyboard navigation, and screen reader semantics". Its fallback section names role="switch" and appearance: none as what the polyfill applies.

Reduced-motion defect (absorbed from #2166, closed as a duplicate of this issue)

Those inline transitions override the reduced-motion rule: tokens.css declares .bk-switch, .bk-switch * { transition: none; } inside @media (prefers-reduced-motion: reduce) (packages/ui-kit/src/tokens.css:1436, rule at :1463-1466), but a normal stylesheet declaration does not beat an inline one. Observed failure (recorded on #2166, found while verifying #2059): pinned offline Chromium with reducedMotion: "reduce" returned matchMedia("(prefers-reduced-motion: reduce)").matches === true and a computed track transition duration of 0.18s; the assertion expected 0s and failed on that value, not during module loading. Fixing it is owned scope of this issue, not a side effect.

Scope

  • When onClick is passed, render a native <input type="checkbox" role="switch"> with appearance: none, painted as today's 38×22 track. Use checked={on}, onChange calling onClick, disabled, and aria-label/aria-labelledby as today.
  • Draw the knob without children (an input has none): either a sibling element that :checked + positions, or a background layer. Move it with translate in the stylesheet. Put the track and knob transitions in tokens.css, not inline, so the existing reduced-motion block removes them. Audit the touched primitive for any other inline transition that would override that block.
  • An <input> does not reliably render ::before across engines (Firefox ignores it), so move the 44px reach (.bk-switch::before) onto a wrapping element. Keep D34's geometry: reach of 11px vertically and 3px horizontally.
  • When no onClick is passed, render the decorative switch as today (non-interactive span, no role).

Out of scope

Acceptance criteria

  • An operable Toggle is a native checkbox input with role="switch". It has no tabIndex and no key handler of its own. Space toggles it once and Enter does nothing (the design's key table). Disabled is native disabled.
  • The Toggle.stories.tsx elementFromPoint edge assertions for adjacent switches pass, and track and knob pixels match the current baselines in both themes and in both states.
  • Under prefers-reduced-motion: reduce, toggling runs no transition on track or knob: their computed transition duration is 0s. Under no preference both keep 0.18s ease. Asserted with real computed styles and state changes in the pinned offline Chromium harness, in both motion modes.
  • Reduced-motion mutation: reintroduce an inline transition on the track (or knob) and watch the reduced-motion assertion fail on its 0s expectation (not on module load), then restore. Record both runs in the PR.
  • Keyboard operation, accessible name, checked state and the 44px hit area remain intact in both motion modes.
  • A real-browser test toggles each operable call site in packages/ui-react by keyboard and pointer. The native-element assertion fails on the current tree. Record that run in the PR.
  • Changeset for @schlessera/brain-ui-kit (patch).

Verification

node scripts/visual.mjs --project=storybook --project=storybook-light --filter=Toggle.stories
node scripts/visual.mjs --project=visual --project=module-settings
bun run test packages/ui-kit packages/ui-react
bun run typecheck
bun run lint

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: commit prefix, and the doc updated in the same commit.
  • This changes a published package's behaviour, so it needs a changeset.

ToggleProps is unchanged. Only the rendered element changes.

2026-10-24: merged #2166 into this issue: its reduced-motion criteria, Chromium failure evidence and guard-removal mutation are now owned scope here, and #2166 is closed as a duplicate. Refreshed the Toggle and tokens.css citations.

Activity

  1. added
    type: refactorStructure changes; behaviour does not. No changeset-visible effect.
    area: ui-kitThe presentational design kit: components, tokens, Storybook, visual baselines.
    priority: p3Someday. Needs a driving use case before it starts.
    agent-readyContext and acceptance criteria are complete enough for an agent to take this unattended.
    on Oct 22, 2026
  2. added
    priority: p2Wanted, not scheduled. Pulled in when a milestone has room.
    and removed
    priority: p3Someday. Needs a driving use case before it starts.
    on Oct 23, 2026
  3. schlessera commented on Oct 23, 2026

    @schlessera
    OwnerAuthor

    2026-10-24 restructuring: raised to p2 because this issue now also owns #2166's p2 reduced-motion fix, which is being merged into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyContext and acceptance criteria are complete enough for an agent to take this unattended.area: ui-kitThe presentational design kit: components, tokens, Storybook, visual baselines.priority: p2Wanted, not scheduled. Pulled in when a milestone has room.type: refactorStructure changes; behaviour does not. No changeset-visible effect.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions