npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

light-dark-auto

v0.0.2

Published

[中文](./README-zh_CN.md) | English

Readme

🌗 light-dark-auto

中文 | English

A lightweight library for managing light/dark theme switching with automatic system preference detection. It provides both programmatic API and a ready-to-use Web Component.

🌟 Features

  • 🎯 Automatic detection of system theme preference
  • 💡 Manual theme switching (light/dark mode)
  • 💾 Theme persistence using localStorage
  • 🧩 Web Component (<theme-switch>) for easy UI integration
  • 🚀 Zero dependencies
  • 📝 Written in TypeScript with full type support
  • ⚡ Lightweight and fast

📦 Installation

npm install light-dark-auto

🚀 Usage

📘 Basic Usage

Import and initialize the theme manager:

import { useTheme } from 'light-dark-auto';

const themeStore = useTheme((theme, details) => {
  console.log('Theme changed to:', theme, 'Details:', details);
});

// Get current theme
console.log(themeStore.theme); // 'auto', 'light', or 'dark'

// Change theme
themeStore.light();  // Switch to light mode
themeStore.dark();   // Switch to dark mode
themeStore.auto();   // Follow system preference

🔌 Using the Web Component

Include the component in your HTML:

<script src="https://cdn.jsdelivr.net/npm/light-dark-auto/lib/index.umd.js"></script>

<theme-switch></theme-switch>

Customize icons with slots:

<theme-switch>
  <div slot="light">☀️ Light</div>
  <div slot="dark">🌙 Dark</div>
  <div slot="system-light">_AUTO(Light)_</div>
  <div slot="system-dark">_AUTO(Dark)_</div>
</theme-switch>

Web Component Attributes

The <theme-switch> element supports the following attributes:

  • theme - Set the initial theme. Possible values: auto (default), light, or dark
  • theme-key - Set the localStorage key to save theme preference. Default: theme
  • body-theme-attr - Set the attribute name added to the body element. Default: theme
  • immediate - Apply theme immediately on component initialization. Default: true
<!-- Set initial theme to dark -->
<theme-switch theme="dark"></theme-switch>

Web Component Methods

The <theme-switch> element provides the following methods for programmatic control:

  • addThemeChangeEventListener(listener) - Add a listener for theme change events
  • removeThemeChangeEventListener(listener) - Remove a previously added listener

Example usage:

const themeSwitch = document.querySelector('theme-switch');

// Add theme change listener
const listener = (theme, details) => {
  console.log('Theme changed to:', theme, 'Details:', details);
};
themeSwitch.addThemeChangeEventListener(listener);

// Remove theme change listener
themeSwitch.removeThemeChangeEventListener(listener);

To get or set the current theme programmatically:

// Get current theme
console.log(themeSwitch.theme); // 'auto', 'light', or 'dark'

// Set theme
themeSwitch.theme = 'dark'; // or 'light' or 'auto'

⚙️ Advanced Configuration

import { useTheme } from 'light-dark-auto';

const themeStore = useTheme(
  (theme, details) => {
    // Theme change callback
    console.log('Theme changed to:', theme);
  },
  {
    themeKey: 'my-app-theme',     // localStorage key (default: 'theme')
    bodyThemeAttr: 'data-theme',  // body attribute (default: 'theme')
    immediate: true,              // Apply theme immediately (default: false)
    initialTheme: 'dark'          // Initial theme (default: 'auto')
  }
);

📚 API

useTheme(onThemeChange?, options?)

Creates a theme store instance.

  • onThemeChange: Callback function triggered when theme changes
  • options: Configuration object

Theme Store Methods

  • themeStore.theme - Get or set current theme
  • themeStore.light() - Switch to light mode
  • themeStore.dark() - Switch to dark mode
  • themeStore.auto() - Follow system preference
  • themeStore.getThemeDetail() - Get actual theme ('light' or 'dark')

🧠 How It Works

The library adds a theme attribute to the <body> element with values:

  • theme="dark" for dark mode
  • No attribute (or removed attribute) for light mode

For automatic mode, it listens to the prefers-color-scheme media query and updates the body attribute accordingly.

🌐 Browser Support

  • Modern browsers with ES6+ support
  • Custom Elements API for Web Component usage

📄 License

MIT