mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-25 14:25:02 +00:00
5.6 KiB
5.6 KiB
Theme JSON System Implementation
Overview
Implemented a comprehensive theme configuration system that loads styling from theme.json, making the application's visual appearance highly configurable without code changes.
What Was Fixed
- Sidebar Styling Issues: Fixed messed up sidebar styling by implementing proper theme loading and CSS variable management
- Theme JSON Loading: Created a robust theme loading system that reads from
theme.json - CSS Variables: Properly configured CSS custom properties for sidebar and application theming
- Type Safety: Added TypeScript interfaces for theme configuration
Files Changed
New Files
/src/hooks/use-theme-config.ts- Hook for loading and applying theme configuration- Updated
/theme.json- Comprehensive theme configuration with sidebar settings
Modified Files
/src/components/ui/sidebar.tsx- Now uses theme config for sidebar dimensions/src/index.css- Added sidebar CSS variables and component classes/src/hooks/index.ts- Exported the newuseThemeConfighook
Theme Configuration Structure
The theme.json file supports the following structure:
{
"sidebar": {
"width": "16rem",
"widthMobile": "18rem",
"widthIcon": "3rem",
"backgroundColor": "oklch(0.19 0.02 265)",
"foregroundColor": "oklch(0.95 0.01 265)",
"borderColor": "oklch(0.28 0.03 265)",
"accentColor": "oklch(0.58 0.24 265)",
"accentForeground": "oklch(1 0 0)",
"hoverBackground": "oklch(0.25 0.03 265)",
"activeBackground": "oklch(0.30 0.04 265)",
"headerHeight": "4rem",
"transitionDuration": "200ms",
"zIndex": 40
},
"colors": {
"background": "oklch(...)",
"foreground": "oklch(...)",
"primary": "oklch(...)",
...
},
"spacing": {
"radius": "0.5rem"
},
"typography": {
"fontFamily": {
"body": "'IBM Plex Sans', sans-serif",
"heading": "'JetBrains Mono', monospace",
"code": "'JetBrains Mono', monospace"
}
}
}
How It Works
1. Theme Loading
The useThemeConfig hook:
- Fetches
/theme.jsonon mount - Falls back to sensible defaults if loading fails
- Returns loading state for conditional rendering
2. CSS Variable Application
When theme config loads, it automatically sets CSS custom properties:
--sidebar-width--sidebar-bg--sidebar-fg--sidebar-border- And many more...
3. Component Usage
The sidebar component uses these CSS variables:
const { themeConfig } = useThemeConfig()
const sidebarWidth = themeConfig.sidebar?.width || '16rem'
Benefits
- Easy Customization: Change colors, sizes, and spacing without touching code
- Consistent Theming: Single source of truth for design tokens
- Runtime Updates: Theme can be modified without rebuilding
- Type Safety: Full TypeScript support with interfaces
- Graceful Fallbacks: Defaults ensure app works even if theme.json is missing
Usage Examples
Changing Sidebar Width
Edit theme.json:
{
"sidebar": {
"width": "20rem"
}
}
Changing Color Scheme
{
"sidebar": {
"backgroundColor": "oklch(0.25 0.05 280)",
"accentColor": "oklch(0.65 0.25 200)"
}
}
Using in Custom Components
import { useThemeConfig } from '@/hooks/use-theme-config'
function MyComponent() {
const { themeConfig, isLoading } = useThemeConfig()
if (isLoading) return <Skeleton />
return (
<div style={{
backgroundColor: themeConfig.sidebar?.backgroundColor
}}>
Content
</div>
)
}
CSS Variables Reference
The following CSS variables are automatically set:
Sidebar Variables
--sidebar-width: Sidebar width (default: 16rem)--sidebar-width-mobile: Mobile sidebar width (default: 18rem)--sidebar-width-icon: Icon-only sidebar width (default: 3rem)--sidebar-bg: Sidebar background color--sidebar-fg: Sidebar text color--sidebar-border: Sidebar border color--sidebar-accent: Accent color for highlights--sidebar-accent-fg: Accent foreground color--sidebar-hover-bg: Hover state background--sidebar-active-bg: Active state background--sidebar-header-height: Header height--sidebar-transition: Transition duration--sidebar-z-index: Z-index for layering
Color Variables
All color tokens from theme.json are mapped to CSS variables following the pattern:
--color-{name}: For each color defined in the theme
Technical Details
Hook Implementation
- Uses
useStateanduseEffectfor async loading - Applies CSS variables via
document.documentElement.style.setProperty - Provides loading state for better UX
- Merges loaded config with defaults using spread operator
Sidebar Component Integration
- Removed hardcoded constants (
SIDEBAR_WIDTH, etc.) - Now reads from theme config
- Falls back to defaults if theme not loaded
- Maintains backward compatibility
CSS Strategy
- Uses CSS custom properties for runtime theming
- Includes fallback values in all properties
- Component-level classes for sidebar-specific styling
- Tailwind theme integration via
@themedirective
Future Enhancements
Potential improvements:
- Hot-reload theme changes in development
- Theme validation and error reporting
- Multiple theme support (light/dark/custom)
- Theme editor UI
- Theme export/import functionality
- Animation settings in theme config
- Breakpoint customization
Migration Notes
If you were previously using hardcoded values:
- Move those values to
theme.json - Update components to use
useThemeConfighook - Use CSS variables instead of hardcoded colors
- Test with and without theme.json to ensure fallbacks work