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 seamlessly integrates with any website to provide smooth light/dark theme switching. Perfect for React, Vue, Angular, Svelte, and vanilla JavaScript projects.
๐ฌ Demo Video
๐ฌ Demo Video: Click the image above to view the theme switcher in action. The demo showcases instant theme switching, smooth transitions, and responsive design.
โจ 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 | v1.2.6 | v1.2.8 |
|---|---|---|---|---|---|---|
| Basic Theme Switching | โ | โ | โ | โ | โ | โ |
| TypeScript Support | โ | โ | โ | โ | โ | โ |
| Multiple Build Formats | โ | โ | โ | โ | โ | โ |
| localStorage Persistence | โ | โ | โ | โ | โ | โ |
| System Preference Detection | โ | โ | โ | โ | โ | โ |
| Touch Gestures | โ | โ | โ | โ | โ | โ |
| Working Demo | โ | โ | โ | โ | โ | โ |
| Toggle Button Fix | โ | โ | โ | โ | โ | โ |
| Self-Contained Demo | โ | โ | โ | โ | โ | โ |
| Enhanced Demo Page | โ | โ | โ | โ | โ | โ |
| GitHub Pages Deployment | โ | โ | โ | โ | โ | โ |
| Video Demo | โ | โ | โ | โ | โ | โ |
| Simple Working 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.8
- โ Video Demo Restored - Auto-playing video that loops continuously to showcase theme switching
- โ Simple Working Demo - Reverted the working demo with beautiful UI and smooth animations
- โ Enhanced Styles - Comprehensive CSS with responsive design and theme-specific styling
- โ Updated Version Table - Complete feature comparison from v1.0.0 to v1.2.8
- โ Cleaner Package - Removed unused demo files while keeping essential working demos
- โ Video Controls - Added video controls for better user experience
๐ Demo
Experience the theme switcher in action with our demo video and live portfolio! The demos showcase:
- ๐ฌ Instant Theme Switching: See how themes change instantly between light and dark modes
- โจ Smooth Transitions: Watch the beautiful animations and transitions
- ๐พ localStorage Persistence: Observe how theme preferences are saved
- ๐ฑ Responsive Design: See the theme switcher working across different screen sizes
- ๐ Live Portfolio: Test the theme switcher on a real website
๐ฌ Demo Video - Click to view the theme switcher in action
๐ Live Portfolio - Test the theme switcher on a live website
๐ฌ Demo Experience: Click the demo video link above to see the theme switcher in action, or visit the live portfolio to test it on a real website.
๐ค 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!