Component-Level Theme Variables
You'll learn how to create component-specific custom properties that reference global theme tokens, making your components both flexible and maintainable.
The Core Idea
While global theme variables define your design system's foundation (colors, spacing, typography), individual components often need their own "vocabulary" of variables that reference those global tokens. This creates a translation layer between your universal design system and specific component needs.
Think of it like a recipe: global variables are your pantry (flour, sugar, eggs), while component-level variables are the recipe itself ("cake flour," "sweetener," "binding agent"). The recipe can use different pantry items without changing its structure.
How It Works
Define component-specific custom properties within the component's scope, pulling values from your global tokens:
/* Global tokens already defined */
:root {
--color-primary: #3498db;
--color-danger: #e74c3c;
--spacing-sm: 0.5rem;
}
/* Component-level variables */
.button {
--button-bg: var(--color-primary);
--button-padding: var(--spacing-sm);
background: var(--button-bg);
padding: var(--button-padding) calc(var(--button-padding) * 2);
}
.button--danger {
--button-bg: var(--color-danger);
}
Why This Matters
This approach gives you:
- Semantic naming within the component context
- Easy theming by overriding component variables
- Maintainability – change global tokens and components update automatically
- Flexibility – swap token mappings without touching component logic
Instead of scattering var(--color-primary) throughout button styles, you use var(--button-bg) everywhere, then change one mapping to retheme the entire component.
Key Takeaway: Component-level theme variables act as a semantic translation layer between global design tokens and component implementation, enabling flexible, maintainable theming without tight coupling.