Core theming concepts and strategies for @nexcraft/forge component library.
- Core Concept: CSS Custom Properties as API
- Manual Theming Approach
- Tailwind Plugin Integration
- Design Token Bridge
- Choosing Your Approach
The fundamental principle is that Forge UI does not dictate themes; it provides a "Theming API" that applications can use to implement any design system.
This API is a documented set of CSS Custom Properties (e.g., --forge-color-primary, --forge-spacing-md) that our components use for styling. The consuming application is responsible for defining the values of these properties.
This approach respects our core architecture:
- Shadow DOM Compatible: CSS Custom Properties are the standard and most performant way to pierce Shadow DOM boundaries for styling.
- Framework Agnostic: It's a browser-native feature and requires no JavaScript framework.
A component's internal stylesheet will use our variables with sensible fallbacks:
/* Simplified example from inside forge-button.ts */
.button {
background-color: var(--forge-color-primary, #2196f3);
font-family: var(--forge-font-family, sans-serif);
border-radius: var(--forge-border-radius-md, 8px);
}Any project can theme Forge UI by creating a standard CSS file that defines the values for our Theming API. This file is then included in their application.
Example my-app-theme.css:
/* This file defines the application's brand and applies it to Forge components */
:root {
/* Color Palette */
--forge-color-primary: #c42a38; /* A custom brand red */
--forge-color-secondary: #4a5568;
--forge-color-surface: #ffffff;
--forge-color-on-surface: #1a202c;
/* Typography */
--forge-font-family: 'Georgia', serif;
/* Shape */
--forge-border-radius-md: 2px; /* Sharp corners */
}Pros: Universal, simple, no dependencies.
Cons: Requires manual maintenance.
To provide a best-in-class developer experience for the vast number of projects using Tailwind CSS, we will create and distribute an official Forge UI Tailwind Plugin (@forge/tailwind-plugin).
This plugin bridges the gap between a project's tailwind.config.js and our components.
- The user installs the plugin:
npm install -D @forge/tailwind-plugin. - They add it to their
tailwind.config.js. - The plugin reads the user's defined theme values (colors, spacing, etc.) from the config.
- It then injects the corresponding
--forge-*CSS variables into Tailwind's base layer.
Example tailwind.config.js:
const forgePlugin = require('@forge/tailwind-plugin');
module.exports = {
theme: {
// User's existing Tailwind theme configuration
extend: {
colors: {
brand: '#c42a38',
neutral: {
900: '#1a202c',
100: '#ffffff',
},
},
fontFamily: {
serif: ['Georgia', 'serif'],
},
borderRadius: {
DEFAULT: '2px',
},
},
},
plugins: [
// The user maps their Tailwind theme keys to the Forge Theming API
forgePlugin({
themeMap: {
primary: 'theme.colors.brand',
surface: 'theme.colors.neutral.100',
'on-surface': 'theme.colors.neutral.900',
'font-family': 'theme.fontFamily.serif',
'border-radius-md': 'theme.borderRadius.DEFAULT',
},
}),
],
};This configuration would automatically generate the same CSS as the manual example above, but it would always be in sync with the project's tailwind.config.js.
The most powerful approach is using the Token Bridge system - the ONLY component library with built-in design token conversion from any design system.
Import design tokens from Figma, Tailwind CSS, Material Design, and more with zero configuration:
import { TokenBridge } from '@nexcraft/forge/utils';
// Import from any design system
const bridge = TokenBridge.fromFigma(figmaTokens);
const css = bridge.toCSSProperties();
// Automatic CSS generation - always in sync!- Figma Design Tokens - Export directly from Figma
- Tailwind CSS - Import from your tailwind.config.js
- Material Design 3 - Google's design system tokens
- Custom Tokens - Build your own token system
// From Figma
const figmaTokens = {
'Brand/Primary/Blue/500': {
value: '#3b82f6',
type: 'color',
description: 'Primary brand color'
}
};
const bridge = TokenBridge.fromFigma(figmaTokens);
const css = bridge.toCSSProperties();
// Inject into your app
const styleSheet = new CSSStyleSheet();
styleSheet.replaceSync(css);
document.adoptedStyleSheets = [styleSheet];- Multiple Sources: Combine tokens from different design systems
- Custom Tokens: Create and add your own tokens
- Output Formats: CSS, Sass, JavaScript, JSON
- Performance: Built-in caching for fast regeneration
- Type Safety: Full TypeScript support
📖 Complete Token Bridge Guide - Full documentation with examples and API reference
- You have a simple, stable design system
- You prefer direct control over CSS
- You don't use a design system tool
- You want zero dependencies
- You're already using Tailwind CSS
- You want automatic sync with your Tailwind config
- You prefer configuration over manual CSS
- You want type-safe theme values
- You use Figma, Material Design, or other design systems
- You want automatic design-to-code sync
- You need to combine multiple design systems
- You want the most powerful and flexible approach
- You're building a complex application with evolving design
- Ultimate Flexibility: We do not impose any design decisions. The consumer has full control.
- Seamless Integration: The Tailwind plugin and Token Bridge make using Forge UI feel native in any ecosystem.
- High Performance: Relies entirely on the browser's native CSS engine with zero JavaScript runtime for theming.
- Maintainable: Keeps a clean separation of concerns. Our components handle logic and structure; the consumer handles the look and feel.
- Future-Proof: As the consumer's design system evolves, our components will adapt automatically.
- Token Bridge Reference - Complete guide and API reference
- Migration Guide - Migrate from existing theming systems
- Figma Integration - Pull tokens from Figma via CLI/MCP
- AI Styling Guide - Complete styling reference for AI assistants