Repository navigation
CSS custom properties migration for all Backpack components #4792
Description
Problem Statement
Backpack components currently use static SASS tokens (e.g. `tokens.$bpk-text-primary-day`) that are hardcoded to light-mode values. When a consuming product switches to the dark theme — or overrides tokens via `BpkThemeProvider` — the components do not respond: they remain locked to light-mode colours, spacings, and radii. This breaks dark mode support and makes per-product theming fragile.
Solution
Migrate every component's SCSS module to reference CSS custom properties (defined in the token-sync pipeline) with the SASS token as a fallback. Because CSS vars cascade and respond to `:root[data-theme="dark"]` overrides at runtime, components automatically flip to the correct values for each theme without any JavaScript changes.
Each component is migrated in its own self-contained PR so changes are reviewable in isolation and can be merged or reverted independently.
User Stories
- As a Backpack consumer, I want Backpack components to automatically render in dark mode when I apply the dark theme, so that my product supports dark mode without per-component workarounds.
- As a Backpack consumer, I want to override a component's private colour tokens via `BpkThemeProvider` using stable camelCase keys, so that I can white-label Backpack components for my brand.
- As a Backpack contributor, I want each component migration to land in its own PR, so that I can review and revert individual components without touching unrelated code.
- As a Backpack contributor, I want every migrated component to document which SASS tokens had no CSS var equivalent, so that I can prioritise those gaps in the token-sync pipeline.
- As a Backpack contributor, I want SASS tokens kept as fallbacks in all `var()` declarations, so that the migration is backward-compatible and components still render correctly in environments that do not load the CSS var stylesheet.
- As a Backpack contributor, I want components with component-scoped private vars (`--bpk-private-{component}-*`) to expose those as camelCase keys in `themeAttributes.tsx`, so that `BpkThemeProvider` can inject targeted overrides via inline styles.
- As a Backpack contributor, I want the migration to never invent new CSS custom property names, so that only tokens that exist in the token-sync pipeline are used as primary values.
- As a Backpack consumer, I want spacing, radius, and border values in components to also use CSS primitive vars (`--bpk-spacing-`, `--bpk-radius-`, `--bpk-border-*`), so that layout tokens are also themeable.
- As a Backpack contributor, I want migrations to replace legacy `bpk-themeable-property()` mixin calls with plain `var()` declarations, so that the mixin is eventually retired.
- As a Backpack contributor, I want RTL-specific `bpk-rtl()` mixin blocks to be replaced with CSS logical properties during migration, so that the RTL mixin is also progressively retired.
- As a Backpack contributor, I want unused SCSS `@use` imports removed after migration, so that component SCSS stays lean.
- As a Backpack contributor, I want the gap analysis document kept up to date as token-sync adds new CSS vars, so that blocked migrations can be unblocked incrementally.
Implementation Decisions
- Pattern: Every migrated property uses
var(--css-var-name, tokens.$sass-fallback). No nested fallbacks. No cascade chains. - Gap handling: Where no CSS var exists for a token, the bare SASS token is kept as-is with no `var()` wrapper. The gap is noted in the PR description. See
docs/css-vars-migration/gap-analysis.mdfor the full gap inventory. - Semantic vs component vars: Use `--bpk-private-{component}-` vars for component-specific colours/dimensions. Use global semantic vars (`--bpk-text-`, `--bpk-core-`, `--bpk-status-`, `--bpk-surface-`) for text, icon, and status colours. Spacing/radius/border use primitive vars (`--bpk-spacing-`, `--bpk-radius-`, `--bpk-border-`).
- themeAttributes.tsx: Only components that have `--bpk-private-{component}-*` vars need a `themeAttributes.tsx`. Only private component-scoped vars are listed there — global semantic vars are not themeable at the component level. Components with no private vars do not need this file.
- themeAttributes key naming: Strip `--bpk-` prefix, convert remaining kebab-case to camelCase. E.g. `--bpk-private-badge-colour-bg-default` → `privateBadgeColourBgDefault`.
- One PR per component: Each PR touches only the SCSS module(s) and optionally `themeAttributes.tsx` + stories for that single component.
- Shared token mapping: The shared mapping table in
docs/css-vars-migration/gap-analysis.mdis the canonical reference for which SASS token maps to which CSS var. Agents should not deviate from it. - Naming mismatches: Some SASS private tokens have differently-named CSS var equivalents (e.g. `$bpk-private-segmented-control-canvas-default-day` → `--bpk-private-segmented-control-colour-bg-on-canvas-default-default`). These are flagged in the gap analysis and should be resolved to the CSS var name, not the SASS name.
- Precedence of component-specific dimension vars: When a `--bpk-private-{component}-dimension-` var exists for a spacing/padding/radius value, prefer it over the generic `--bpk-spacing-` primitive.
Recommended PR order (by complexity)
Wave 1 — zero gaps:
accordion, ai-blurb, graphic-promotion, section-header, fieldset, nudger, table, infinite-scroll, mobile-scroll-container, spinner, panel, section-list, card-list, split-input, barchart
Wave 2 — ≤1 gap:
breadcrumb, collapsible, rating, grid-toggle, journey-arrow
Wave 3 — small gap sets:
tooltip, skip-link, checkbox, radio, progress, price-range, slider, select, input
Wave 4 — moderate:
datatable, horizontal-nav, chat-notification, content-cards, segmented-control, switch
Wave 5 — several gaps:
info-banner, navigation-tab-group, skeleton, page-indicator, star-rating, autosuggest, card
Wave 6 — most gaps:
bottom-sheet, dialog, drawer, modal, flare, floating-notification, map, inset-banner, overlay, popover, form-validation, label
Testing Decisions
- Good tests verify observable output — that the compiled CSS contains the correct `var(--css-var, fallback)` declaration for a given property — not the internal structure of the SCSS.
- The primary test surface is visual regression: Storybook stories render each component variant, and snapshots capture the rendered HTML/CSS. A migrated component should produce identical visual output to the pre-migration baseline.
- themeAttributes tests (`themeAttributes-test.tsx`) assert the exported arrays contain the exact expected camelCase key strings. Prior art: `packages/backpack-web/src/bpk-component-badge/src/themeAttributes-test.tsx`.
- Dark mode stories should be added or updated to render the component under `:root[data-theme="dark"]` to verify the CSS var cascade is working.
- Existing unit tests (React Testing Library) should pass unchanged — they do not test CSS output.
Out of Scope
- Adding new CSS custom properties to the token-sync pipeline — gaps are documented only; filling them requires a separate design token work item.
- Migrating already-migrated components (button, badge, text, chip, calendar, banner-alert, bubble, chatbot-input, checkbox-card, comparison-table, pagination).
- Changing component behaviour, props, or TypeScript interfaces.
- Migrating SCSS in `bpk-mixins/` shared mixin files — this PRD covers per-component `*.module.scss` files only.
- Typography mixin replacement (`@include typography.bpk-footnote` etc.) — only tokens directly used in module SCSS are in scope.
Further Notes
- Full gap inventory (which tokens have no CSS var, which components are affected) is maintained in
docs/css-vars-migration/gap-analysis.md. - The `backpack-css-vars-migration` skill at
.claude/skills/backpack-css-vars-migration/SKILL.md\contains the step-by-step migration instructions and should be invoked for each component PR. - Some private SASS token names differ from their CSS var equivalents (e.g. navigation-tab vs navigation-tabs, segmented-control old vs new names). These are called out in the private component token gaps section of the gap analysis.
- The `--bpk-test` var visible in the light theme CSS is not a real token — ignore it.
Activity