JSPM

tailwind-dynamic-theme

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q9682F
  • License MIT

Runtime Tailwind theme manager for React โ€“ switch dark/light/custom themes dynamically with no rebuilds

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

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 ๐ŸŽจ