// Full implementation of a multi-theme system in React using CSS variables and Context API.
// Features:
// - Supports 'light', 'dark', and 'ocean' themes.
// - CSS variables for easy theming (bg-color, text-color, primary-color, secondary-color).
// - Theme state managed globally via Context API.
// - Theme persisted in localStorage.
// - Smooth transitions.
// - Toggle cycles through themes.
// File: src/ThemeContext.js
import React, { createContext, useContext, useState, useEffect } from 'react';
const ThemeContext = createContext();
export const useTheme = () => {
const context = useContext(ThemeContext);
if (!context) {
throw new Error('useTheme must be used within ThemeProvider');
}
return context;
};
export const ThemeProvider = ({ children }) => {
const [theme, setTheme] = useState('light');
// Load saved theme on mount
useEffect(() => {
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
setTheme(savedTheme);
}
}, []);
// Save theme and update DOM attribute on change
useEffect(() => {
localStorage.setItem('theme', theme);
document.documentElement.setAttribute('data-theme', theme);
}, [theme]);
const toggleTheme = () => {
const themes = ['light', 'dark', 'ocean'];
const currentIndex = themes.indexOf(theme);
const nextIndex = (currentIndex + 1) % themes.length;
setTheme(themes[nextIndex]);
};
return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
{children}
</ThemeContext.Provider>
);
};
/* File: src/index.css (or public/index.css) */
/* Import this in src/index.js with: import './index.css'; */
:root {
/* Light theme defaults */
--bg-color: #ffffff;
--text-color: #000000;
--primary-color: #1976d2;
--secondary-color: #9c27b0;
}
[data-theme="dark"] {
--bg-color: #121212;
--text-color: #ffffff;
--primary-color: #bb86fc;
--secondary-color: #03dac6;
}
[data-theme="ocean"] {
--bg-color: #0f0f23;
--text-color: #e0e0ff;
--primary-color: #00bfff;
--secondary-color: #20b2aa;
}
/* Global styles using CSS vars */
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
background: var(--bg-color);
color: var(--text-color);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
min-height: 100vh;
transition: background-color 0.3s ease, color 0.3s ease;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 2rem;
}
.btn {
background: var(--primary-color);
color: var(--bg-color);
border: none;
padding: 12px 24px;
border-radius: 8px;
cursor: pointer;
font-size: 1rem;
font-weight: 500;
transition: background-color 0.3s ease, transform 0.1s ease;
}
.btn:hover {
background: color-mix(in srgb, var(--primary-color) 90%, black);
transform: translateY(-1px);
}
.card {
background: color-mix(in srgb, var(--secondary-color) 95%, transparent);
padding: 2rem;
border-radius: 12px;
margin-top: 2rem;
border: 1px solid color-mix(in srgb, var(--secondary-color) 20%, transparent);
transition: all 0.3s ease;
}
h1 {
margin-bottom: 1rem;
}
// File: src/App.js
import React from 'react';
import { ThemeProvider, useTheme } from './ThemeContext';
import './index.css'; // Ensure CSS is imported
const DemoContent = () => {
const { theme, toggleTheme } = useTheme();
return (
<div className="container">
<h1>🧩 Multi-Theme System Demo</h1>
<p>Current theme: <strong>{theme}</strong></p>
<button className="btn" onClick={toggleTheme}>
🔄 Toggle Theme (Cycle: light → dark → ocean → light)
</button>
<div className="card">
<h2>This is a themed card!</h2>
<p>
Uses <code>--secondary-color</code> for background/border and <code>--text-color</code> for text.
</p>
<ul>
<li>Global state via Context API ✅</li>
<li>CSS variables for zero-JS restyling ✅</li>
<li>Persisted in localStorage ✅</li>
<li>Smooth transitions ✅</li>
</ul>
</div>
</div>
);
};
const App = () => {
return (
<ThemeProvider>
<DemoContent />
</ThemeProvider>
);
};
export default App;
// File: src/index.js (standard CRA setup)
import React from 'react';
import { createRoot } from 'react-dom/client'; // React 18+
import './index.css';
import App from './App';
const container = document.getElementById('root');
const root = createRoot(container);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
🚀 Quick Setup Instructions
- Create a new React app:
npx create-react-app theme-system && cd theme-system
- Replace
src/App.js, add src/ThemeContext.js, replace src/index.css and src/index.js.
- Run
npm start – toggle the button to see themes switch instantly!
- Customization: Add more themes by extending the
themes array and CSS [data-theme="new"] {} selectors.
- Usage in other components:
const { theme, toggleTheme } = useTheme();
This is production-ready, performant (no re-renders on theme change outside context consumers), and extensible. CSS vars ensure styles update without JS! 🎨