The user wants a plan for a "multi-brand theming integration" using "design tokens," "CSS variables," and "compile-time guardrails."
Deconstruct the Request:
- Multi-brand theming: The system needs to support multiple distinct visual identities (e.g., Brand A vs. Brand B) using the same codebase.
- Design Tokens: The raw material. Platform-agnostic variables (JSON/YAML) defining colors, spacing, typography, etc.
- CSS Variables: The delivery mechanism for the browser. These map the tokens to CSS custom properties (
--brand-color-primary).
- Compile-time guardrails: Constraints enforced during the build process to prevent invalid theme generation (e.g., missing tokens, incorrect types, accessibility violations).
Target Audience: Likely a Frontend Architect, Senior Frontend Engineer, or Design System Lead. The tone should be technical, structured, and actionable.
Key Components of the Plan:
- Architecture/Strategy: How do tokens flow to CSS?
- Token Structure: How are they defined?
- Build Process: How are tokens transformed?
- Runtime vs. Compile-time: Dealing with the "compile-time" constraint.
- Guardrails: What checks are in place?
- Integration: How does the app consume this?
Drafting the Steps:
-
Phase 1: Token Definition (The Source of Truth)
- Need a standard format. Style Dictionary is the industry standard here.
- Structure: Global vs. Semantic vs. Component.
- Branding: How do we switch brands? Namespaces? Or separate files?
- Decision: Use a nested structure.
brands/brand-a/tokens.json, brands/brand-b/tokens.json.
-
Phase 2: The Transformation Pipeline (Style Dictionary)
- How to generate CSS?
- Output format:
:root for default, maybe class-based switching (.brand-a, .brand-b) or attribute-based ([data-brand="a"]).
- Refinement: Since the user mentioned "compile-time guardrails," maybe they want static CSS files per brand rather than dynamic JS switching? Or maybe a "Multi-tenant" approach. Let's assume a flexible approach: CSS Variables attached to a parent selector.
-
Phase 3: Implementation in CSS/Components
- Components shouldn't use hardcoded values. They must use semantic variables (
var(--color-btn-bg)).
- Themed components need to handle different radii, shadows, etc., automatically.
-
Phase 4: Compile-Time Guardrails (The "Special Sauce")
- This is crucial. How do we check things before they reach production?
- Types: TypeScript types generated from tokens (typed-css-modules).
- Validation: JSON schemas for token files.
- Accessibility: Contrast checking during build (WCAG).
- Completeness: Ensuring Brand B has all the tokens Brand A has.
-
Phase 5: Application Architecture
- How to load the theme?
- Strategy: Static bundling (one build per brand) vs. Dynamic loading (runtime switch). The prompt implies "integration," so a dynamic switch or a build switch are both valid. I'll focus on the CSS Architecture that supports both.
Structuring the Output:
I'll organize this into a logical roadmap:
- Architecture Overview (The big picture).
- Token Taxonomy (Structuring data).
- Build Pipeline & Tooling (Style Dictionary configuration).
- CSS Architecture (Variable mapping).
- Compile-Time Guardrails (The specific requested feature).
- Integration Strategy (Implementation).
Refining the Content:
Self-Correction/Review during drafting:
- Initial thought: Just use SASS maps.
- Correction: No, the prompt specifically asks for Design Tokens and Compile-time guardrails. Modern systems use JSON + Style Dictionary (or Theo), not just SASS. SASS is the consumer, not the source of truth.
- Guardrails: Make sure to emphasize compile-time. This means catching errors in CI/CD, not just console warnings in the browser.
Final Polish:
Use code snippets to illustrate the configuration (JSON, config.js, CSS). Ensure clear headings. Add a summary of benefits.
(Self-Correction on Multi-Brand):
Multi-brand usually implies White Labeling or a Portfolio of products. The strategy needs to distinguish between "Themes" (Light/Dark) and "Brands" (Coke/Pepsi).
- Structure:
- Core (Common logic)
- Brand A (Tokens)
- Brand B (Tokens)
- The output generates CSS for the specific brand context.
Let's write the plan.
This plan outlines a robust architecture for managing multiple brands (e.g., White-labeling, B2B2C) using modern frontend tooling. It prioritizes type safety, maintainability, and performance.
Phase 1: Architecture & Taxonomy
Before writing code, we must define the structure of our tokens. We will use a three-tier token architecture to separate logic from visual style.
- Global Tokens (Primitives): Raw values (e.g.,
#FFFFFF, 8px, 1rem).
- Alias Tokens (Semantic): Purpose-driven aliases (e.g.,
color-background-primary, spacing-medium).
- Component Tokens (Specific): Explicit application (e.g.,
button-primary-background).
Directory Structure:
/tokens
/core
global.json # Shared primitives (grids, opacity)
/brands
/brand-a
theme.json # Overwrites for Brand A
assets/ # Logos, icons
/brand-b
theme.json # Overwrites for Brand B
/src
/styles
_variables.css # Generated CSS output
index.css # Root entry point
Phase 2: The Token Pipeline (Style Dictionary)
We will use Style Dictionary to transform JSON definitions into cross-platform variables.
1. Define Token Files (JSON)
tokens/core/global.json
{
"color": {
"white": { "value": "#FFFFFF" },
"black": { "value": "#000000" }
}
}
tokens/brands/brand-a/theme.json
{
"color": {
"background": { "value": "{color.white}" },
"text": { "value": "{color.black}" }
}
}
2. Configuration (W3C Standard)
Use the W3C Design Tokens Community Group (DTCG) format for future compatibility.
Phase 3: CSS Variable Integration
Instead of generating separate CSS files for every brand, we generate a single "Theme Map" CSS file. This allows for runtime switching (if needed) or strict scoping.
Generated Output (_variables.css):
We use :root for defaults and Attribute Selectors for brand scoping.
/* Default / Fallback */
:root {
--color-background: #FFFFFF;
--color-text: #000000;
}
/* Brand A Specifics */
[data-brand="brand-a"] {
--color-background: var(--color-white);
--color-text: var(--color-blue-900);
}
/* Brand B Specifics */
[data-brand="brand-b"] {
--color-background: var(--color-black);
--color-text: var(--color-neon-green);
}
Consumption in Components:
Components should never use hardcoded hex codes. They consume the semantic variables.
/* Button.css */
.button {
background-color: var(--color-background-primary);
color: var(--color-text-action);
}
Phase 4: Compile-Time Guardrails
This is the most critical phase for stability. We need to ensure that Developer Experience (DX) prevents errors before they reach production.
1. Structural Validation (JSON Schema)
Create a schema that all brand theme files must adhere to. This ensures that if Brand A defines button-radius, Brand B must also define it (or have a default fallback).
Guardrail Check: Use a pre-build script (e.g., ajv JSON schema validator) to compare brand tokens against the master interface.
2. Type Safety (TypeScript/CSS Modules)
Generate TypeScript definitions from the tokens.
types/tokens.d.ts (Generated)
export interface DesignTokens {
color: {
background: string;
text: string;
};
// ...
}
CSS-Modules Integration:
If using CSS Modules or CSS-in-JS, enforce type safety so developers cannot typo a variable name.
// vite.config.ts / webpack.config.js
// Use 'typed-css-modules' to generate .d.ts files for CSS
// This ensures var(--colour-text) throws an error if it doesn't exist in tokens
3. Visual Regression Testing
Themes change visual density. A button in Brand A might fit, but in Brand B (with a massive border) it might break layout.
- Tool: Playwright or Percy.
- Strategy: Run visual diff tests specifically against
[data-brand="brand-a"] and [data-brand="brand-b"] states.
4. Linting (Stylelint)
Configure Stylelint to ban raw color values.
.stylelintrc
{
"rules": {
"color-no-hex": true,
"color-named": "never",
"declaration-property-value-disallowed-list": {
"/.*/": ["/^#/", "/^rgb/", "/^hsl/"]
}
}
}
This ensures developers use var(--brand-primary) instead of #00FF00.
Phase 5: Implementation Strategy
Option A: Compile-Time Switching (Best Performance)
If you know the brand at build time (e.g., compiling a standalone mobile app or a static site for a specific client), use CSS Variables with postcss-custom-properties to perform dead-code elimination.
- Define
process.env.BRAND = 'brand-a'.
- PostCSS plugin scans the CSS. It sees
[data-brand="brand-a"].
- It inlines the values and removes the other brand blocks.
Option B: Runtime Switching (Web Apps)
Load the CSS variable manifest dynamically or scope the data-brand attribute to the <html> or <body> tag.
<!-- Index.html -->
<html data-brand="brand-a">
React Integration Example:
import { useEffect } from 'react';
const ThemeProvider = ({ brand }) => {
useEffect(() => {
document.documentElement.setAttribute('data-brand', brand);
}, [brand]);
return null;
};
Summary Checklist for Success
- [ ] Single Source of Truth: All colors/spacing live in JSON, not CSS/JS.
- [ ] Semantic Naming: Variables represent purpose (
danger) not value (red).
- [ ] Validation Pipeline: CI fails if a brand token is missing or invalid JSON.
- [ ] Strict Linting: No hardcoded colors allowed in component CSS.
- [ ] Scoped Variables: CSS variables are defined on
[data-brand] to avoid global pollution.