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

use-theme-mode

v1.0.2

Published

A lightweight React hook for managing light and dark theme with persistence.

Downloads

213

Readme

use-theme-mode 🌗

npm version npm downloads license Live Demo

A lightweight, framework-agnostic React hook for managing light and dark theme modes with persistence and system preference detection.

use-theme-mode focuses purely on theme state management, leaving styling completely in your control—making it compatible with any CSS solution or UI framework.


✨ Improvements

  • Added live demo deployed on Vercel
  • Added homepage link to npm package
  • Improved README with usage examples and styling guides

🛠 Maintenance

  • Cleaned up package metadata
  • Improved documentation structure

🔗 Live Demo

Try the live demo here:

👉 https://use-theme-mode-demo.vercel.app/

The demo showcases:

  • Real light/dark theme switching
  • Persistent theme state
  • Code example synced with live behavior

✨ Key Features

  • Toggle Modes: Simple light/dark theme switching.
  • System Preference: Automatically detects prefers-color-scheme.
  • Persistent: Saves user choice via localStorage.
  • Cross-Tab Sync: Updates theme seamlessly across all open browser tabs.
  • SSR Safe: Works reliably in Next.js and other server-rendered environments.
  • Framework Agnostic: Use with Plain CSS, Tailwind, MUI, Chakra UI, etc.
  • Zero Dependencies: Keep your bundle size minimal.

📦 Installation

Get started by adding the package to your project:

# npm
npm install use-theme-mode

# yarn
yarn add use-theme-mode

🚀 Basic Usage

Integrating the hook is straightforward. It automatically updates the data-theme attribute on your <html> element.

import { useTheme } from "use-theme-mode";

function App() {
  const { theme, toggleTheme } = useTheme();

  return (
    <button onClick={toggleTheme}>
      Current theme: {theme === 'light' ? '☀️ Light' : '🌙 Dark'}
    </button>
  );
}

🎨 Applying Theme Styles

This library does not inject styles. Instead, it acts as a controller that manages a data-theme attribute on the <html> element.

1. Plain CSS Example

/* Define your tokens */
:root[data-theme="light"] {
  --bg-color: #ffffff;
  --text-color: #000000;
}

:root[data-theme="dark"] {
  --bg-color: #121212;
  --text-color: #ffffff;
}

/* Apply transitions for smooth switching */
body {
  background-color: var(--bg-color);
  color: var(--text-color);
  transition: background-color 0.3s ease, color 0.3s ease;
}

2. Tailwind CSS Example

Configure your tailwind.config.js to recognize the data attribute:

// tailwind.config.js
module.exports = {
  darkMode: ["attribute", "data-theme"],
};

You can then use standard Tailwind dark-mode classes:

<div className="bg-white text-black dark:bg-black dark:text-white">
  Hello World
</div>

🧩 API Reference

The useTheme() hook returns the following:

| Property | Type | Description | | :--- | :--- | :--- | | theme | "light" \| "dark" | The current active theme. | | toggleTheme | () => void | Toggles between light and dark. | | setLight | () => void | Forces light mode. | | setDark | () => void | Forces dark mode. |


🧠 How It Works

  1. Initialize: Checks localStorage for a saved preference.
  2. Fallback: If no local data exists, it reads the system's prefers-color-scheme.
  3. Persist: All manual updates are stored back to localStorage.
  4. Sync: Updates the <html data-theme="..."> attribute globally and synchronizes across tabs via the storage event.

📋 Compatibility

  • React: 16.8+
  • Environments: CRA, Vite, Next.js, and general SSR.

🛣️ Roadmap

  • 🚀 Optional ThemeProvider context wrapper for easier state sharing.
  • 🛡️ Improved TypeScript support with more robust type definitions.
  • 📦 ESM build support to modernize package compatibility.
  • 🔑 Custom storage key support to allow overriding the default localStorage key.

📄 License

MIT © Saad Ahmad


🔗 GitHub Repository:

https://github.com/saadahmad888/use-theme-mode