Package Exports
- tailwind-dynamic-theme
Readme
๐ tailwind-dynamic-theme
Runtime Tailwind theme manager for React โ switch between dark, light, or custom themes using CSS variables, without rebuilds.
tailwind-dynamic-theme
is a lightweight, production-ready React library that enables dynamic theming in Tailwind CSS apps using runtime CSS variables. It's framework-friendly, zero-config, and supports persistent theme preferences with localStorage.
โจ Features
- ๐จ Dynamic theme switching (light/dark/custom)
- โก๏ธ No rebuilds required โ powered by CSS variables
- ๐ง Smart defaults with system preference detection
- ๐พ Stores preference in localStorage
- ๐ชถ Lightweight: no Tailwind plugins required
- โ Written in TypeScript
๐ฆ Installation
npm install tailwind-dynamic-theme
Or if testing locally:
npm link ../tailwind-dynamic-theme
๐ Quick Start
1. Wrap your app with ThemeProvider
// main.tsx or App.tsx
import { ThemeProvider } from 'tailwind-dynamic-theme';
function App() {
return (
<ThemeProvider>
<YourApp />
</ThemeProvider>
);
}
2. Use the useTheme
hook to toggle themes
import { useTheme } from 'tailwind-dynamic-theme';
const ThemeToggle = () => {
const { theme, setTheme } = useTheme();
return (
<div className="space-x-2">
<button onClick={() => setTheme('light')}>Light</button>
<button onClick={() => setTheme('dark')}>Dark</button>
<p>Current: {theme}</p>
</div>
);
};
๐จ Define Your Themes
Create CSS files with custom properties:
light.css
:root {
--color-bg: #ffffff;
--color-text: #111827;
--color-primary: #3b82f6;
}
dark.css
:root {
--color-bg: #111827;
--color-text: #f9fafb;
--color-primary: #60a5fa;
}
Then serve them via public folder or CDN and reference them inside the lib config (already handled if you use default file structure).
โ๏ธ Tailwind Config Example
// tailwind.config.js
module.exports = {
content: ['./src/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {
colors: {
background: 'var(--color-bg)',
text: 'var(--color-text)',
primary: 'var(--color-primary)'
}
}
},
plugins: []
}
๐ง API
ThemeProvider
Wraps your app and injects context.
useTheme()
Returns:
{
theme: string; // current theme (light | dark | custom)
setTheme: (theme: string) => void;
}
๐พ Persistence
The selected theme is saved to localStorage
under the key:
preferred-theme
๐ File Structure (recommended)
public/
โโโ themes/
โ โโโ light.css
โ โโโ dark.css
src/
โโโ App.tsx
โโโ index.css
tailwind.config.js
๐งช Example Project
Check the /example
directory for a working Vite + Tailwind + React setup using tailwind-dynamic-theme
.
๐ Roadmap
- Add support for dynamic custom themes
- Support SSR + hydration fallback
- Vue composable (optional)
๐ License
MIT ยฉ 2025 [Your Name]
๐ฌ Feedback / Contributions
Pull requests and ideas welcome! Open an issue or fork the repo and start building. Let's make theming fun again ๐จ