A lightweight, modern JavaScript library for toggling light and dark themes with blazing-fast performance, system preference detection, and persistent user settings. Perfect for any web project!
- 🌗 Toggle between light, dark, or custom themes
- 🖥️ Detects system theme via
prefers-color-scheme
- 💾 Persists user preference in
localStorage
- 🎨 Supports Tailwind CSS and smooth CSS transitions
- ⚡ Simple, event-driven API
npm install daynitejs
import DayniteJs from 'daynitejs';
const daynite = new DayniteJs({
themes: ['light', 'dark'],
defaultTheme: 'light',
customStyles: {
light: { '--bg-color': '#ffffff', '--text-color': '#1f2937' },
dark: { '--bg-color': '#1f2937', '--text-color': '#f3f4f6' }
}
});
daynite.toggle();
daynite.onThemeChange(theme => console.log(`Theme changed to: ${theme}`));
Define theme-specific styles using [data-theme]
:
[data-theme="light"] {
--bg-color: #ffffff;
--text-color: #1f2937;
}
[data-theme="dark"] {
--bg-color: #1f2937;
--text-color: #f3f4f6;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
}
/* Smooth transitions */
.DayniteJs-transition,
.DayniteJs-transition * {
transition: background-color 0.3s ease, color 0.3s ease;
}
<button id="toggle-btn" aria-label="Toggle theme">Toggle Theme</button>
<script type="module">
import DayniteJs from 'daynitejs';
const daynite = new DayniteJs();
document.getElementById('toggle-btn').addEventListener('click', () => daynite.toggle());
</script>
new DayniteJs(options)
— Initialize with themes, default theme, and custom stylesdaynite.init()
— Initializes the theme based on system or local preference (called automatically)daynite.toggle()
— Manually toggles themedaynite.setTheme('dark' | 'light')
— Explicitly set the themedaynite.getTheme()
— Returns the current themedaynite.reset()
— Resets to system/default themedaynite.onThemeChange(callback)
— Subscribes to theme change events
themes
: Array of theme names (default:["light", "dark"]
)defaultTheme
: Fallback theme (default:'light'
)storageKey
: localStorage key (default:'DayniteJs-theme'
)customStyles
: CSS variables per theme
Build minified bundle:
npm run build
Run tests:
npm test
To test the theme toggle demo in your browser:
- Build the library:
npm run build
- Serve the project root with a local HTTP server:
npm run serve
- Open your browser and go to: http://localhost:3000/examples/
Note: Opening
examples/index.html
directly from the filesystem will not work due to browser security restrictions. Always use a local server.
DayniteJs is developed and maintained by Suhaib Muhammad Babangida.
If you like this project, please ⭐ it on GitHub and share it with your friends!
Licensed by MIT