Package Exports
- website-theme-switcher
- website-theme-switcher/dist/index.esm.js
- website-theme-switcher/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (website-theme-switcher) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
๐ Website Theme Switcher
A lightweight, framework-agnostic theme switcher that works seamlessly with Tailwind CSS, Bootstrap, or any CSS framework. Switch between light and dark themes with a simple toggle button.
โจ Features
- ๐ฏ Simple Toggle Button - One button to switch between light and dark themes
- ๐ Framework Agnostic - Works with React, Vue, Angular, or vanilla JavaScript
- ๐จ CSS Framework Compatible - Integrates with Tailwind CSS, Bootstrap, or custom CSS
- ๐พ Automatic Persistence - Saves theme preference to localStorage
- ๐ฑ Mobile Friendly - Touch gestures and responsive design
- โก Lightweight - Only 16.6 kB minified
- ๐ง Easy to Use - Simple HTML attributes, no complex setup
๐ฏ Quick Start (5 minutes)
1. Install the Package
npm install website-theme-switcher2. Add Toggle Button to Your Navbar
<button class="theme-toggle" data-toggle-theme="dark,light">
๐ Toggle Theme
</button>3. Add CSS Variables
:root {
/* Light theme (your existing website - unchanged) */
--bg-color: #ffffff; /* White background */
--text-color: #333333; /* Dark text */
}
[data-theme="dark"] {
/* Dark theme (minimal changes) */
--bg-color: #000000; /* Black background */
--text-color: #ffffff; /* White text for readability */
}4. Replace Hardcoded Colors
body {
background-color: var(--bg-color); /* Only this changes */
color: var(--text-color); /* Only this changes */
}5. Initialize Theme Switcher
<script src="https://unpkg.com/website-theme-switcher@latest/dist/index.js"></script>
<script>
WebsiteThemeSwitcher.init();
</script>๐งช Test It
Open demo/simple-working-demo.html in your browser to see the working toggle button!
๐จ HTML Attributes
The package automatically detects these HTML attributes:
Toggle Between Themes
<button data-toggle-theme="dark,light">๐ Toggle</button>Set Specific Theme
<button data-set-theme="dark">๐ Dark</button>
<button data-set-theme="light">โ๏ธ Light</button>Select Dropdown
<select data-choose-theme>
<option value="light">โ๏ธ Light</option>
<option value="dark">๐ Dark</option>
</select>Custom Storage Key
<button data-toggle-theme="dark,light" data-key="my-theme">Toggle</button>Active Class
<button data-set-theme="dark" data-act-class="active">Dark</button>๐ Framework Examples
React
import { useEffect } from 'react';
import { WebsiteThemeSwitcher } from 'website-theme-switcher';
function App() {
useEffect(() => {
WebsiteThemeSwitcher.init();
}, []);
return (
<nav>
<button data-toggle-theme="dark,light">๐ Toggle</button>
</nav>
);
}Vue 3
<template>
<nav>
<button data-toggle-theme="dark,light">๐ Toggle</button>
</nav>
</template>
<script setup>
import { onMounted } from 'vue';
import { WebsiteThemeSwitcher } from 'website-theme-switcher';
onMounted(() => {
WebsiteThemeSwitcher.init();
});
</script>Angular
import { Component, OnInit } from '@angular/core';
import { WebsiteThemeSwitcher } from 'website-theme-switcher';
@Component({
selector: 'app-navbar',
template: `
<nav>
<button data-toggle-theme="dark,light">๐ Toggle</button>
</nav>
`
})
export class NavbarComponent implements OnInit {
ngOnInit() {
WebsiteThemeSwitcher.init();
}
}โ๏ธ Configuration
WebsiteThemeSwitcher.init({
defaultTheme: 'light', // Default theme
storageKey: 'theme', // localStorage key
enableSystemPreference: false, // Use system preference
transitionDuration: 300, // CSS transition duration
themes: ['light', 'dark'], // Available themes
onThemeChange: (theme) => {}, // Theme change callback
debug: false, // Enable debug logging
enableTouchGestures: false, // Enable touch gestures
touchThreshold: 50 // Touch gesture threshold
});๐จ Custom Themes
Load Custom Theme
const themeSwitcher = WebsiteThemeSwitcher.getInstance();
themeSwitcher.loadTheme('custom', {
'--bg-color': '#1a1a2e',
'--text-color': '#ffffff',
'--accent-color': '#0f3460'
});Use Custom Theme
<button data-set-theme="custom">๐จ Custom</button>๐ฑ Mobile Support
Touch Gestures
WebsiteThemeSwitcher.init({
enableTouchGestures: true,
touchThreshold: 50
});- Swipe Right: Previous theme
- Swipe Left: Next theme
๐ง API Methods
Get Instance
const themeSwitcher = WebsiteThemeSwitcher.getInstance();Set Theme
themeSwitcher.setTheme('dark');Toggle Theme
themeSwitcher.toggleTheme(['light', 'dark']);Get Current Theme
const currentTheme = themeSwitcher.getCurrentTheme();Check Dark Mode
const isDark = themeSwitcher.isDarkMode();Remove Theme
themeSwitcher.removeTheme();๐ Version Comparison
| Feature | v1.0.0 | v1.1.0 | v1.2.0 | v1.2.3 |
|---|---|---|---|---|
| Basic Theme Switching | โ | โ | โ | โ |
| TypeScript Support | โ | โ | โ | โ |
| Multiple Build Formats | โ | โ | โ | โ |
| localStorage Persistence | โ | โ | โ | โ |
| System Preference Detection | โ | โ | โ | โ |
| Touch Gestures | โ | โ | โ | โ |
| Working Demo | โ | โ | โ | โ |
| Toggle Button Fix | โ | โ | โ | โ |
| Self-Contained Demo | โ | โ | โ | โ |
๐งช Testing
Run Tests
npm testBuild Package
npm run buildCheck Bundle Size
npm run size๐ฆ Installation
npm
npm install website-theme-switcherCDN
<script src="https://unpkg.com/website-theme-switcher@latest/dist/index.js"></script>Download
๐ What's New in v1.2.3
- โ
Working Theme Switcher Demo -
simple-working-demo.htmlwith self-contained, fully functional theme switcher - โ
Improved Toggle Functionality - Enhanced
data-toggle-themeattribute detection and theme switching - โ Self-Contained Demo - Created demo that works without external dependencies
- โ Fixed Toggle Button - Resolved issue where toggle buttons were not switching themes
- โ Enhanced Package - Better toggle button detection and theme switching logic
๐ฏ Demo
Test the working theme switcher:
- Local Demo:
demo/simple-working-demo.html(fully functional) - Package Demo:
demo/package-demo.html(uses npm package)
๐ค Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Creator: Muhammad Nazmul Ahsan
- Company: Triotrix Tech Solutions
- Live Demo: Portfolio
๐ Contact
- GitHub: mnahsanofficial/website-theme-switcher
- Company: Triotrix Tech Solutions
โญ Star this repository if you find it helpful!