Theming
Using CSS variables to customize the theme.
Color Tokens
The theme system is built with semantic color tokens that represent specific use cases rather than literal colors. Here are the main color tokens and their use cases:
Base Colors
background
/foreground
: Primary background and text colorscard
/card-foreground
: Used for card components and their contentpopover
/popover-foreground
: For popover, dropdown, dialog, etc.
Interactive Elements
primary
/primary-foreground
: Main brand color, used for primary actionssecondary
/secondary-foreground
: Less prominent actions and elementsmuted
/muted-foreground
: Subdued elements like secondary textaccent
/accent-foreground
: Highlighted or featured elementsdestructive
/destructive-foreground
: Dangerous or destructive actions
Status Colors
danger
/danger-foreground
/danger-border
: Error states and critical alertswarning
/warning-foreground
/warning-border
: Warning messages and alertsinfo
/info-foreground
/info-border
: Informational messagessuccess
/success-foreground
/success-border
: Success states and confirmations
Utility Colors
border
: Default border colorinput
: Form input bordersring
: Focus ring color for interactive elements
Chart Colors
chart-1
throughchart-5
: Predefined colors for data visualizations
Color Format Implementation
Why Hex in CSS Variables?
In globals.css
, colors are defined in hexadecimal format for several reasons:
- Browser Compatibility: Hex colors have universal browser support
- Human Readability: Hex values are widely understood and easier to read
- Tool Integration: Most design tools export colors in hex format
globals.css
:root {
--background: #fafafa;
--foreground: #060606;
/* ... other colors ... */
}
OKLCH Transformation
In tailwind.config.js
, these hex colors are transformed into OKLCH color space:
tailwind.config.js
colors: {
background: "oklch(from var(--background) l c h / <alpha-value>)",
foreground: "oklch(from var(--foreground) l c h / <alpha-value>)",
// ... other colors ...
}
This transformation provides several benefits:
- Wider Color Gamut: OKLCH can represent a broader range of colors
- Better Color Interpolation: Smoother transitions and animations
- Perceptual Uniformity: More natural-looking color variations
Customizing Colors
Adding New Colors
To add new colors to your theme:
Add the CSS variables in globals.css
globals.css
:root {
/* Existing colors */
--custom-color: #7c3aed;
--custom-color-foreground: #ffffff;
}
.dark {
/* Existing dark mode colors */
--custom-color: #9f67ff;
--custom-color-foreground: #ffffff;
}
Add the color to your tailwind.config.js
tailwind.config.js
colors: {
// Existing colors
'custom-color': {
DEFAULT: "oklch(from var(--custom-color) l c h / <alpha-value>)",
foreground: "oklch(from var(--custom-color-foreground) l c h / <alpha-value>)",
}
}
Using Custom Colors
After adding new colors, you can use them with Tailwind's utility classes:
Usage
<div className="bg-custom-color text-custom-color-foreground">
Custom colored content
</div>