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

@andystevenson/theme-toggle

v0.1.5

Published

theme toggle for light/dark mode and prefers-color-scheme

Downloads

17

Readme

a simple javascript/css solution for toggling light/dark themes on websites


Highlights

  • Totally inspired by Adam Argyle based on his GUI Challenges over at web.dev (theme switching component)
  • Simple library
  • Links to user prefers-color-scheme OS settings
  • Writes to browser localStorage to remember user preferences

Install

npm install @andystevenson/theme-toggle

Usage

import toggle from '@andystevenson/theme-toggle'

.... that's it!

Styling it with CSS
[data-theme='light'] {
  color-scheme: light;
  background-color: antiquewhite;
  color: darkslategray;
}

[data-theme='dark'] {
  color-scheme: dark;
  background-color: darkslategray;
  color: antiquewhite;
}

html[data-theme='dark'] [data-theme='light'].theme-toggle,
html[data-theme='light'] [data-theme='dark'].theme-toggle {
  display: none;
}
Linking it into your HTML
<head>
  <script type="module" src="./theme-toggle.js"></script>
  <!-- assuming theme-toggler.js would do the following -->
  <!-- import toggle from '@andystevenson/theme-toggle' -->
  ...
</head>

The rationale for doing it like this

Quoting from Adam Argyle... To achieve this, a plain <script> tag in the document <head> is loaded first, before any CSS or <body> markup. When the browser encounters an unmarked script like this, it runs the code and executes it before the rest of the HTML. Using this blocking moment sparingly, it's possible to set the HTML attribute before the main CSS paints the page, thus preventing a flash or colors.

Under the covers

To make the toggle work the HTML page must contain a clickable element with id="theme-toggler" (can be anything... even a div).

What happens when user clicks/selects the theme-toggler is an attribute data-theme' is attached to the <html> document tag. What the theme-toggle library is doing is switching between data-theme='dark' and data-theme='light'. How you then style these is up to you. See the trivial CSS above as an example. Have a look at the git hub repo for a full example with a working index.html example. The package.json uses as the bundler.

Credits

All credit to Adam Argyle for the inspiration.

I used a couple of SVG icons from the very useful Bootstrap Icons Library.

License

MIT

Maintainer