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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@burzomir/color-mode-detector

v2.0.0

Published

A lightweight JavaScript/TypeScript library for detecting and managing color modes (light/dark) in web applications. Simple to use, with an intuitive API for subscribing to color mode changes and customizing user interfaces accordingly.

Downloads

172

Readme

Color Mode Detector

Welcome to Color Mode Detector – a straightforward, no-frills library for detecting and managing color modes in your web applications. If you need a simple, reliable solution for handling light and dark modes without any unnecessary overhead, this library is for you.

Overview

Color Mode Detector provides a minimalistic API to help you detect the current color mode (light or dark) and react to changes. It's designed to be easy to use and integrate into any web project, giving you just what you need without any extra fluff.

Installation

You can install the library via npm or yarn:

npm install color-mode-detector

or

yarn add color-mode-detector

Getting Started

Step 1: Import and Initialize

To get started, import the library and initialize the detector. You can specify the default color mode (light or dark) when initializing:

import { init, ColorMode } from "color-mode-detector";

const colorModeDetector = init();

Step 2: Subscribe to Color Mode Changes

Subscribe to color mode changes so you can update your application whenever the mode changes:

colorModeDetector.subscribe((colorMode: ColorMode) => {
  switch (colorMode) {
    case ColorMode.Dark:
      console.log("Color mode changed to dark");
      break;
    case ColorMode.Light:
      console.log("Color mode changed to light");
      break;
  }
});

Step 3: Unsubscribe When Done

Unsubscribe from the detector when you no longer need to listen to changes:

colorModeDetector.unsubscribe(yourCallbackFunction);

Step 4: Cleanup

When your component or application is being unmounted, clean up the detector to avoid memory leaks:

colorModeDetector.cleanup();

Example Usage

Here’s a basic example of how you might use Color Mode Detector in a React component:

import { init, ColorMode } from "@burzomir/color-mode-detector";

// Initialize the detector
const colorModeDetector = init();

// Define a callback function to handle color mode changes
const handleColorModeChange = (colorMode) => {
  console.log(`Color mode is now: ${colorMode}`);
  document.body.style.backgroundColor = colorMode === "dark" ? "#333" : "#FFF";
  document.getElementById("title").style.color =
    colorMode === "dark" ? "#FFF" : "#000";
};

// Subscribe to color mode changes
colorModeDetector.subscribe(handleColorModeChange);

// Cleanup when not needed anymore
window.addEventListener("beforeunload", () => {
  colorModeDetector.cleanup();
});

Contributing

If you run into any issues or have ideas for improvements, feel free to open an issue or submit a pull request on the GitHub repository.

This library is designed to be simple and efficient – just like your code. If you need a reliable way to manage color modes, give it a try.

Change log

  • 2.0.0

    • Removed cleanup method

      Cleanup is handled automatically when all subscribers are removed

  • 1.0.1

    • Project reorganization
  • 1.0.0

    • Initial release