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

dark-mode-toggle-component

v1.1.2

Published

A somewhat opinionated plug-and-play dark mode web component.

Downloads

16

Readme

Dark Mode Toggle Component

The Dark Mode Toggle Component is a lightweight, easy-to-integrate custom element that allows users to switch between dark and light themes on your website. It's designed to be incredibly simple to use, requiring just a single line of HTML to add to your project. This component automatically adds a .dark class to the document's root element, enabling seamless theme transitions. It's perfect for projects that aim to provide a user-friendly theme toggling experience with minimal effort.

Light mode Dark mode

Features

  • Automatic Theme Detection: Initializes based on the user's system preference for dark or light mode. Stores their preference in local storage.
  • Easy to Use: Simply add the component to your project, and it handles the rest.
  • Customizable: Integrates with your project's styles.
  • Lightweight: Less than 2kB gzipped, making it an incredibly efficient choice for any project.
  • Toggling images: Images can be toggled with this component as well. Just add dark-toggle to their id and data-alt-src="path/to/dark/mode.png and it just works!

Getting Started

To use the Dark Mode Toggle Component in your project, you only need to include the script from jsDelivr CDN. Here's how you can do it:

Include the Script

Add the following <script> tag to your HTML document, ideally within the <head> section to ensure it loads early:

<script src="https://cdn.jsdelivr.net/npm/dark-mode-toggle-component@latest/dist/index.js"></script>

You can also install the package through npm and add it to your project that way, the choice is yours.

Note: It's important you import it at the top of <head> as a regular, non-deferred script in order to ensure there is no flashing of unstyled content. The reason is that if a user sets their preference on your website to dark mode but their system preference is light they would first see a flash of light mode followed by dark mode.

Usage

Once included, you can use the <dark-mode-toggle> element anywhere in your HTML document:

<dark-mode-toggle></dark-mode-toggle>

That's it! The component will automatically render a toggle switch that allows users to switch between dark and light modes.

Example

The component modifies the document by adding or removing the .dark class on the root element. You can define your theme styles using CSS variables in the :root and .dark selectors.

Here's an example of how you can define your CSS variables for light and dark themes:

:root {
  --background-color: #ffffff;
  --text-color: #000000;
}

.dark {
  --background-color: #1a1a1a;
  --text-color: #ffffff;
}

body {
  background-color: var(--background-color);
  color: var(--text-color);
}

These examples demonstrate how the component looks in different themes. You can style the component and the themed elements of your site using CSS as shown above.

For images the following works:

<img
  src="/images/github.png"
  data-alt-src="project/images/github-dark.png"
  slot="image"
  class="dark-toggle"
/>

If you're using a bundler like rollup you may need to change data-alt-src to project/images/image as shown above because it may not be rewritten.