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

@wcj/dark-mode

v1.0.16

Published

Web Component that toggles dark mode πŸŒ’

Downloads

1,027

Readme

dark-mode

Buy me a coffee CI jsDelivr CDN NPM Downloads npm version Open in unpkg

A custom element that allows you to easily put a Dark Mode πŸŒ’ toggle. so you can initially adhere to your users' preferences according to prefers-color-scheme, but also allow them to (optionally permanently) override their system setting for just your site.

Installation

Install from npm:

npm install --save @wcj/dark-mode

Or, alternatively, use a <script defer> tag (served from unpkg's CDN):

CDN: UNPKG | jsDelivr | Githack | Statically

<script src="https://unpkg.com/@wcj/dark-mode"></script>

Usage

There are two ways how you can use <dark-mode>:

<dark-mode></dark-mode>
<dark-mode light="Dark" dark="Light"></dark-mode>
<dark-mode dark="Dark" light="Light" style="border: 1px solid red; font-size: 12px;"></dark-mode>

Use in React:

import React from 'react';
import '@wcj/dark-mode';

function Demo() {
  return (
    <div>
      <dark-mode permanent light="Light" dark="Dark"></dark-mode>
    </div>
  );
}

Toggle in JavaScript:

const toggle = document.querySelector('dark-mode');
const button = document.createElement('button');
button.textContent = 'Change Theme';
button.onclick = () => {
  const theme = document.documentElement.dataset.colorMode;
  // or => const theme = toggle.mode
  document.documentElement.setAttribute('data-color-mode', theme === 'light' ? 'dark' : 'light');
}
document.body.appendChild(button);
// Listen for toggle changes
// and toggle the `dark` class accordingly.
document.addEventListener('colorschemechange', (e) => {
  console.log(`Color scheme changed to "${e.detail.colorScheme}" or "${toggle.mode}".`);
  button.textContent = toggle.mode === 'dark' ? 'Change Theme 🌞' : 'Change Theme πŸŒ’';
});

Interacting with the custom element:

const darkMode = document.querySelector('dark-mode');

// Set the mode to dark
darkMode.mode = 'dark';
// Set the mode to light
darkMode.mode = 'light';
// Set the mode to dark
document.documentElement.setAttribute('data-color-mode', 'dark');
// Set the mode to light
document.documentElement.setAttribute('data-color-mode', 'light');

// Set the light label to "off"
darkMode.light = 'off';
// Set the dark label to "on"
darkMode.dark = 'on';

// Set a "remember the last selected mode" label
darkMode.permanent = true;

// Remember the user's last color scheme choice
darkModeToggle.setAttribute('permanent', false);
// Forget the user's last color scheme choice
darkModeToggle.removeAttribute('permanent');

Reacting on color scheme changes:

/* On the page */
document.addEventListener('colorschemechange', (e) => {
  console.log(`Color scheme changed to ${e.detail.colorScheme}.`);
});

Reacting on "remember the last selected mode" functionality changes:

/* On the page */
document.addEventListener('permanentcolorscheme', (e) => {
  console.log(`${e.detail.permanent ? 'R' : 'Not r'}emembering the last selected mode.`);
});

Properties

Properties can be set directly on the custom element at creation time, or dynamically via JavaScript.

export type ColorScheme = 'light' | 'dark';
export class DarkMode extends HTMLElement {
  mode?: ColorScheme;
  /**
   * Defaults to not remember the last choice.
   * If present remembers the last selected mode (`dark` or `light`),
   * which allows the user to permanently override their usual preferred color scheme.
   */
  permanent?: boolean;
  /**
   * Any string value that represents the label for the "dark" mode.
   */
  dark?: string;
  /**
   * Any string value that represents the label for the "light" mode.
   */
  light?: string;
  style?: React.CSSProperties;
}

Events

  • colorschemechange: Fired when the color scheme gets changed.
  • permanentcolorscheme: Fired when the color scheme should be permanently remembered or not.

Alternatives

  • dark-mode-toggle A custom element that allows you to easily put a Dark Mode πŸŒ’ toggle or switch on your site
  • Darkmode.js Add a dark-mode / night-mode to your website in a few seconds
  • darken Dark mode made easy
  • use-dark-mode A custom React Hook to help you implement a "dark mode" component.
  • Dark Mode Switch Add a dark-mode theme toggle with a Bootstrap Custom Switch

Contributors

As always, thanks to our amazing contributors!

Made with github-action-contributors.

License

Licensed under the MIT License.