JSPM

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

A lightweight, framework-agnostic JavaScript library for seamless theme switching between light and dark modes

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

npm version npm downloads npm bundle size License

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 demo video

๐Ÿ’ก Live Test: View Live Portfolio

๐ŸŽฌ 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-switcher

2. 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 test

Build Package

npm run build

Check Bundle Size

npm run size

๐Ÿ“ฆ Installation

npm

npm install website-theme-switcher

CDN

<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

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

๐Ÿ“ž Contact


โญ Star this repository if you find it helpful!