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

@pshah-lab/themeswitcher

v0.1.4

Published

Light, dark, and system theme manager with SSR-safe no-flash support

Readme

@pshah-lab/themeswitcher

A lightweight, framework-agnostic theme manager for light, dark, and system modes — with SSR-safe no-flash support.

Designed to be:

  • Minimal
  • Predictable
  • Copy-paste friendly
  • Framework independent

Features

  • Light / Dark / System theme support
  • Prevents flash of incorrect theme (FOUC)
  • SSR-safe (works before JS bundle loads)
  • No dependencies
  • Fully typed (TypeScript)
  • Works with plain JS, React, Vue, or any framework

Installation

npm install @pshah-lab/themeswitcher

Core Usage (Framework-agnostic)

import { createThemeManager } from "@pshah-lab/themeswitcher";

const theme = createThemeManager();

theme.set("dark");        // "light" | "dark" | "system"
theme.toggle();           // toggle light ↔ dark
theme.get();              // resolved theme: "light" | "dark"
theme.getMode();          // selected mode

The theme is applied to the document using a data attribute:

<html data-theme="dark">

Style your app using this attribute:

[data-theme="dark"] {
  background: #000;
  color: #fff;
}

React Usage

Create the hook once at module scope.

// theme.ts
import { createUseTheme } from "@pshah-lab/themeswitcher";

export const useTheme = createUseTheme();

Use it anywhere in your app:

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

  return (
    <button onClick={toggleTheme}>
      Current theme: {theme}
    </button>
  );
}

⚠️ Do not call createUseTheme() inside components.
It must be created once and reused.


Prevent Theme Flash (Recommended)

When using light/dark themes, browsers may briefly render the page in the default theme before JavaScript loads. This causes a visible flash of incorrect theme.

To prevent this, add the following script inline in your HTML <head>, before any CSS is loaded.

Why this is needed

  • Runs before first paint
  • Applies the correct theme synchronously
  • Works before your JavaScript bundle loads
  • Required for SSR
  • Strongly recommended for all setups

Add this to <head>

<script>
(function () {
  try {
    var key = "theme-mode";
    var attribute = "data-theme";

    var stored = localStorage.getItem(key);
    var mode =
      stored === "light" || stored === "dark" || stored === "system"
        ? stored
        : "system";

    var isDark =
      window.matchMedia &&
      window.matchMedia("(prefers-color-scheme: dark)").matches;

    var theme =
      mode === "system"
        ? isDark
          ? "dark"
          : "light"
        : mode;

    document.documentElement.setAttribute(attribute, theme);
  } catch (e) {
    // fail silently
  }
})();
</script>

Place it before your CSS:

<head>
  <!-- Prevent theme flash -->
  <script>/* pasted theme script */</script>

  <link rel="stylesheet" href="styles.css" />
</head>

Important Notes

  • Do not import this script from the package
  • Do not bundle it
  • Copy-paste is intentional and required for correct timing
  • The script logic matches the internal theme manager

Configuration

createThemeManager({
  defaultMode: "system",        // default
  storageKey: "theme-mode",     // localStorage key
  attribute: "data-theme",      // HTML attribute
});

API Reference

set(mode)

Set the theme mode.

theme.set("dark");

toggle()

Toggle between light and dark.

theme.toggle();

get()

Get the resolved theme.

theme.get(); // "light" | "dark"

getMode()

Get the selected mode.

theme.getMode(); // "light" | "dark" | "system"

Design Philosophy

  • Explicit over magical
  • Copy-paste over runtime hacks
  • No global side effects
  • No framework lock-in
  • Predictable behavior

License

MIT