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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@btfcss/color-scheme

v1.0.3

Published

Color Scheme (Light/Dark mode) manager for front-end JavaScript (user preference / system preference)

Readme

color-scheme

This npm package manages color scheme (Light/Dark mode) for front-end application (user preference / system preference). The color scheme setting is done in the following priority order:

  1. local storage (keep track of previous user choice)
  2. user preference
  3. browser settings
  4. system settings

We recommend using the color-scheme CSS property, for example :

:root {
  color-scheme: light dark;
}

Note that this package does not update CSS, but a callback function can be triggered on color scheme change. Update your CSS in this function.

Animation showing the system color scheme manager

Install

npm install btfcss@color-scheme

Usage

Import the package

import colorScheme from "@btfcss/color-scheme";

Get User Choice

The function getUserChoice() returns the current user choice:

  • "light" : light mode
  • "light dark" : system settings
  • "dark" : dark mode

If user choice has never been set, check if a previous choice has been recorded in local storage, otherwise the library set system settings ("light dark") as default.

// Should display "light", "light dark" or "dark"
console.log (colorScheme.getUserChoice()}

Get System Color Scheme

The function getSystem() returns the system settings:

  • "light" : light mode
  • "dark" : dark mode
// Should display "light" or "dark"
console.log (colorScheme.getSystem()}

Note that the system color scheme is not necessary the same as the page. For example, system can be set to dark while the user selected light in the page. User choice overide system settings.

Get Current Color Scheme

The function getCurrent() returns the page current color scheme:

  • "light" : light mode
  • "dark" : dark mode
// Should display "light" or "dark"
console.log (colorScheme.getCurrent()}

Get Current Status

The function get() returns an object containing the current color scheme status:

  • status.user - The user's choice ["light" | "light dark" | " dark"]
  • status.current - The page color scheme ["light" | " dark"]
// Log the current color scheme status
console.log (colorScheme.getCurrent()}
// {user: 'light dark', current: 'dark'}

Set New User Choice

When the user selects a new color scheme from the interface, you have to call the setUserChoice(newChoice) fonction. The function expects a string as parameter. The value of the string must be one of the following:

  • "light" : set light mode
  • "light dark" : set system settings
  • "dark" : set dark mode

This function does not update the CSS, but dispatches an event that will trigger the callback function. It is recommended to update the CSS in the callback function.

Listen for Change

The addEventListenerOnChange(callback) set a callback function that will be called when the page color scheme changes. Changes may occurs

  • because the function setUserChoice() has been called
  • or because the user choice is set to system ("light dark") and system settings has changed.

The callback function receives the new object status as parameter :

  • status.user - The user's choice ["light" | "light dark" | " dark"]
  • status.current - The page color scheme ["light" | " dark"]
colorScheme.addEventListenerOnChange((status) => {
    // On change, set the new CSS color scheme
    document.querySelector(':root').style.setProperty('color-scheme', status.user);

    // Log new status in console
    console.log ('User choice', status.user);
    console.log ('Current scheme', status.current);
}));

Full Example

This full example can be tested online in CodeSandBox.

HTML

<h1>Color Scheme Manager</h1>
Select color scheme :
<select id="color-scheme-selector">
    <option value="light">Light</option>
    <option value="light dark" selected>System</option>
    <option value="dark">Dark</option>
</select>

CSS

:root {
  color-scheme: light dark;
}

* {
  font-family: sans-serif;
  color: light-dark(#000, #fff);
  background-color: light-dark(#fff, #000);
}

JavaSCript

import "./styles.css";
import colorScheme from "@btfcss/color-scheme";

// Get the selector element
const selector = document.getElementById("color-scheme-selector");

// When the selector changes
selector.addEventListener("change", () => {
  colorScheme.setUserChoice(selector.value);
});

// Callback function
function onChange(status) {
  // On change, set the new CSS color scheme
  document
    .querySelector(":root")
    .style.setProperty("color-scheme", status.user);

  // Update the selector according to user choice
  selector.value = status.user;
}

// Listen color scheme changes
colorScheme.addEventListenerOnChange(onChange);

// Call at start up for applying user preferences
onChange(colorScheme.get());