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

react-tailwind4-theme

v1.3.1

Published

๐ŸŒ’ A React Hook to handle the dark/light mode theme using Tailwind4 CSS and localStorage.

Downloads

2

Readme

React
Tailwind CSS v4
Version
License

React Tailwind4 Theme ๐Ÿš€

Installation ๐Ÿ’ป

npm install react-tailwind4-theme

React Tailwind4 Theme is a small library (or hook) designed for projects using Tailwind CSS v4 and React 18+. This hook, useHandleTheme, facilitates managing dark and light modes in your application by detecting system preferences and persistently storing the userโ€™s choice. ๐ŸŽจ

Features โœจ

  • System Theme Detection:
    Checks the userโ€™s preference using window.matchMedia("(prefers-color-scheme: dark)"). ๐Ÿ”

  • Theme Persistence:
    Stores the userโ€™s choice in localStorage to maintain consistency during the session and future visits. ๐Ÿ’พ

  • Separation of Concerns:
    The hook updates two attributes on the <html> element:

    • data-theme: Indicates the applied theme, either "light" or "dark".๐ŸŒ’
    • origin-theme: Specifies the source of the theme ("user-defined" or "system"). ๐Ÿ› ๏ธ
  • Dynamic Update:
    Subscribes to changes in the system preference, updating the theme in real time (if the user has not manually set one). โšก

Setting Up the Entry File

For the hook to work correctly and for you to observe the theme origin right from the start, add the attributes data-theme and origin-theme to the <html> element of your entry file (for example, in index.html). Hereโ€™s an example:

<!doctype html>
<html lang="en" data-theme="light" origin-theme="system">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Your description here" />
    <title>React Tailwind4 Theme Example</title>
  </head>
  <body>
    <div id="root"></div>
    <!-- Scripts and other resources -->
  </body>
</html>

[!NOTE]
With this initial configuration, the useHandleTheme hook will dynamically update the attributes on the <html> element according to the user or system preference. โœ…

Usage Example

Below is an example of how to integrate useHandleTheme in a React component:

import { useHandleTheme } from 'react-tailwind4-theme';

export const Header = () => {
  const { onChangeTheme, origin, theme } = useHandleTheme();
  console.log({ origin }); // user-defined - system
  return (
    <header className="flex items-center justify-between p-4 bg-gray-100 dark:bg-gray-900">
      {/* ... */}
      <div className="flex items-center gap-4">
        <button
          onClick={onChangeTheme}
          className="px-4 py-2 rounded bg-blue-600 text-white hover:bg-blue-700"
        >
          {theme === 'dark' ? (
            <DarkSvg className="dark:text-gray-200 w-6 md:w-8" />
          ) : (
            <LightSvg className="dark:text-gray-200 w-6 md:w-8" />
          )}
        </button>
      </div>
    </header>
  );
};

In this example, the hook:

  • Detects and persists the applied theme. ๐Ÿ”„
  • Updates the data-theme and origin-theme attributes on the <html> element. ๐ŸŒ
  • Allows switching the theme manually via onChangeTheme. ๐Ÿ”ง

How to Debug and Test Dark Mode ๐Ÿž

To facilitate debugging and testing related to prefers-color-scheme, Chrome DevTools allows you to emulate the user's color scheme preference without affecting the system-wide configuration. This is especially useful for validating the behavior of the useHandleTheme hook. ๐Ÿ‘