Theme Switching
Stride’s brand system supports seamless runtime switching between brands with smooth transitions, automatic persistence, and React hooks for integration.Core Function: applyBrandTheme
TheapplyBrandTheme() function applies a brand theme at runtime, handling both static and dynamic brands automatically.
Function Signature (from src/lib/brands.ts:547)
Basic Usage
- Detects if the brand is static or dynamic
- Removes previous brand classes
- Applies new brand classes
- Updates CSS variables for dynamic brands
- Persists choice to localStorage
- Triggers smooth transitions
How It Works
Fromsrc/lib/brands.ts:547-590, here’s what happens when you call applyBrandTheme:
React Hook: useBrand
TheuseBrand hook provides a React-friendly interface for brand management.
Hook Signature (from src/lib/useBrand.ts:21-38)
Basic React Example
Advanced React Example
Fromsrc/stories/DynamicBrandSystem.stories.tsx:187-197:
Getting Current Brand
Retrieve the currently active brand programmatically. Function Signature (fromsrc/lib/brands.ts:593)
localStorage Persistence
Brands are automatically persisted to localStorage and restored on page load.How Persistence Works
Fromsrc/lib/brands.ts:276-283 and src/lib/brands.ts:578-580:
Restoring Brands on Load
Fromsrc/lib/brands.ts:504-544:
Initialization in App
Fromsrc/lib/brands.ts:615-622:
Disabling Persistence
You can disable localStorage persistence if needed:Transition Configuration
Control how brand switches are animated.Global Transition Configuration
Config Interface (fromsrc/lib/brands.ts:173-179)
src/lib/brands.ts:232-237):
Configuring Transitions
Function Signature (fromsrc/lib/brands.ts:243)
How Transitions Work
Fromsrc/app/brands.css:744-756:
- Adds
.brand-switchingclass to<html>before switching - Disables all transitions temporarily
- Changes brand classes and CSS variables
- Removes
.brand-switchingafter configured duration - Transitions resume, animating to new colors
Available Brands Utilities
Get lists of available brands.Get All Brands
Function Signatures (fromsrc/lib/brands.ts)
Advanced: Dynamic Brand Switching
For dynamic brands, the switching process includes generating CSS on the fly. Fromsrc/lib/brands.ts:330-377:
- Applies fallback brand class first (e.g.,
.brand-stride) - Applies dynamic brand class (e.g.,
.brand-dynamic-client-abc) - Generates CSS with defined tokens
- Undefined tokens inherit from fallback brand
Unregistering Brands
Remove dynamic brands when no longer needed. Function Signature (fromsrc/lib/brands.ts:304)
Complete Example: Brand Switcher Component
Here’s a production-ready brand switcher:Best Practices
-
Initialize on app load
-
Use the useBrand hook in React
-
Validate brand IDs before switching
-
Configure transitions once at app start
-
Handle brand switching in loading states
Next Steps
- Creating Brands - Learn to create custom brands
- Design Tokens - Understand the token system