Skip to content

Separate themes into their own package (@xds/themes) #139

Closed

Description

@cixzhang

Summary

XDS components should work out of the box with no theme installed. Themes are optional overlays — consumers can use @xds/core standalone and get a functional UI with proper light/dark mode support using the base token defaults from defineVars. Themes are distributed as separate packages that consumers opt into.

This separation also opens the door for designers and other system teams to craft and publish their own theme bundles for XDS, without touching core.

Current State

  • packages/core/src/theme/ contains both infrastructure and themes:
    • Infrastructure — tokens.stylex.ts (defineVars), ThemeContext.ts, XDSTheme.tsx, types.ts
    • Themes — defaultTheme.stylex.ts, neutralTheme.stylex.ts (use createTheme to override token vars)
  • ⚠️ Base tokens use light-mode-only values — tokens.stylex.ts defaults are single hex values (e.g. #0064E0), while defaultTheme.stylex.ts uses light-dark() (e.g. light-dark(#0064E0, #2694FE)). This means components without a theme applied don't support dark mode.
  • Components import token vars (colorVars, spacingVars, etc.) directly — they never import a theme
  • @xds/core/theme re-exports everything together: tokens, provider, and theme objects
  • CLI theme command scans packages/core/src/theme/*Theme.stylex.ts to discover themes

Design Principles

  1. Components work without themes — @xds/core ships with base token defaults via defineVars, including proper light-dark() support. Install core, use components, done.
  2. Themes are additive — Wrapping your app in <XDSTheme theme={someTheme}> applies createTheme overrides on top of the defaults. No theme = base defaults.
  3. Themes are independently distributable — Anyone (XDS team, partner design systems, community) can author and publish a theme package that targets XDS tokens.
  4. Component style overrides belong in themes — Themes can include component-level style overrides (e.g., buttonVariants, headingStyles, proseElementStyles). This gives theme authors full control over component rendering, not just token values.

Proposed Structure

@xds/core (infrastructure + base tokens)

  • tokens.stylex.ts — defineVars with light-dark() defaults (components depend on these)
  • ThemeContext.ts — React context for runtime theme access
  • XDSTheme.tsx — Provider component (<XDSTheme theme={...}>)
  • types.ts — Theme, ThemeRaw, ComponentStyles, etc. (stays in core — components augment ComponentStyles via declare module)

Components continue to import colorVars, spacingVars, etc. from core. No change needed.

@xds/theme/default and @xds/theme/neutral (new package with subpath exports)

  • default/ — the XDS default theme (token overrides + component style overrides)
  • neutral/ — the neutral grayscale theme
  • Future: additional brand themes, community themes

Each theme uses createTheme() against @xds/core's token vars and can include component-level style overrides — clean peer dependency.

Usage

// Minimal — no theme, base defaults with light-dark() support
import { XDSButton } from '@xds/core/Button';
// Just works ✓ (light and dark mode)

// With a theme
import { XDSTheme } from '@xds/core/theme';
import { defaultTheme } from '@xds/theme/default';

<XDSTheme theme={defaultTheme}>
  <XDSButton>Themed</XDSButton>
</XDSTheme>

// Infrastructure imports stay the same
import { colorVars, spacingVars } from '@xds/core/theme';

Theme authoring (third-party)

// @my-org/xds-theme-brand/index.ts
import * as stylex from '@stylexjs/stylex';
import { colorVars, spacingVars, ... } from '@xds/core/theme/tokens.stylex';
import type { Theme } from '@xds/core/theme';

const colors = stylex.createTheme(colorVars, {
  '--color-accent': 'light-dark(#E91E63, #F48FB1)',
  // ... all color tokens inline
});

// Component-level overrides
const headingStyles = stylex.create({ ... });
const buttonVariants = stylex.create({ ... });

export const myBrandTheme: Theme = {
  name: 'my-brand',
  styles: { colors, ... },
  raw: { ... },
  components: {
    button: { variants: buttonVariants },
    heading: { styles: headingStyles },
  },
};

Challenges & Spike Results

1. Theme type lives in core (module augmentation)

Components augment the ComponentStyles interface via declare module '../theme/types' — e.g., XDSButton adds button?: { variants?: ... } to ComponentStyles. This means the Theme type grows as you import components — which is the desired behavior (theme type is as concise as the components you're using). The Theme type definition must stay in @xds/core.

2. Cross-package createTheme — SPIKED ✅

Result: it works, with constraints.

createTheme across package boundaries works when:

  • Themes import directly from the .stylex.ts source file (e.g., @xds/core/theme/tokens.stylex), NOT through a re-exporting index.ts
  • The StyleX Babel plugin has aliases configured to resolve @xds/core/* to source

What doesn't work:

  • Importing via @xds/core/theme (resolves to index.ts → re-export chain breaks static analysis)
  • Using as unknown as type casts on override objects (StyleX requires inline object literals)

Approach for @xds/theme:

// Theme files import tokens directly from the .stylex.ts source
import { colorVars } from '@xds/core/theme/tokens.stylex';

export const defaultTheme = stylex.createTheme(colorVars, {
  '--color-accent': 'light-dark(#0064E0, #2694FE)',
  // ... inline literal values only
});

@xds/core needs a new subpath export pointing to source:

"./theme/tokens.stylex": {
  "types": "./src/theme/tokens.stylex.ts",
  "default": "./src/theme/tokens.stylex.ts"
}

Fallback for third-party themes without source access: Use stylex.create() with raw CSS variable names instead of createTheme(). Works anywhere but loses type safety.

Full spike details: packages/theme/spike/FINDINGS.md

Testing Strategy

Storybook (visual verification)

Storybook is the primary tool for visual testing. The current setup already has theme and color mode switchers in the toolbar.

  • ✅ "No Theme" option added to the Storybook theme toolbar — renders components without the <XDSTheme> wrapper to verify they look correct using only base defineVars defaults
  • Test all three states for each component: no theme, default theme, neutral theme — in both light and dark mode
  • After package separation, update apps/storybook/.storybook/preview.tsx to import themes from @xds/theme/default and @xds/theme/neutral

Unit tests (vitest)

  • Existing component tests already render without a theme provider — these implicitly test the "no theme" path. Verify they still pass after tokens.stylex.ts gets light-dark() defaults.
  • Add a test that verifies the Theme type from @xds/core accepts theme objects from @xds/theme/* (type-level check)

Sandbox app

  • apps/sandbox (Next.js) serves as an integration test — update it to import from @xds/theme/* and verify the full build pipeline works with the separated packages

Work Items

Prerequisites

  • Spike cross-package createTheme — Works with direct .stylex.ts imports + aliases config. See findings above.
  • Add "No Theme" to Storybook toolbar — Done in preview.tsx
  • Update tokens.stylex.ts to use light-dark() values — Branch: navi/feat/tokens-light-dark (ready, needs PR)
  • Verify components render correctly in both light and dark mode with no theme applied (via Storybook "No Theme" mode)

Package separation

  • Create packages/theme with its own package.json (@xds/theme), peer dep on @xds/core
  • Add ./theme/tokens.stylex subpath export to @xds/core/package.json (points to source)
  • Set up subpath exports: @xds/theme/default, @xds/theme/neutral
  • Move defaultTheme.stylex.ts and neutralTheme.stylex.ts into packages/theme, update imports to use @xds/core/theme/tokens.stylex
  • Remove theme object re-exports from @xds/core/theme/index.ts (keep infrastructure exports)
  • Configure StyleX aliases in theme package build config

Downstream updates

  • Update Storybook preview to import themes from @xds/theme/*
  • Update sandbox app to import themes from @xds/theme/*
  • Update CLI theme command to discover themes from @xds/theme package
    -Update agent docs (xds.md, CLI component output) to reflect new import paths
  • Document theme authoring guide — how third parties create and publish XDS themes

Open Questions

  1. Theme authoring helpers — Should we export a createXDSTheme() utility that handles the boilerplate of calling createTheme() for each token group? Would lower the bar for theme authors.
  2. Base tokens vs default theme divergence — Once base tokens have light-dark() values, the default theme may initially be identical to the base. Over time they could diverge (e.g., default theme gets opinionated component overrides). Is that the intent?
  3. Versioning — Should @xds/theme version in lockstep with @xds/core, or independently?

Related

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions