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

@colinaut/theme-multi-switch

v0.8.2

Published

I created this three way switch for setting theme as I couldn't find what I was looking for — most are two way switches and I wanted to allow for auto. So I built a three way switch which defaults to "auto" (for browser prefers-color-scheme) and allows "l

Downloads

24

Readme

Theme Multi Switch Web Component

I created this three way switch for setting theme as I couldn't find what I was looking for — most are two way switches and I wanted to allow for auto. So I built a three way switch which defaults to "auto" (for browser prefers-color-scheme) and allows "light" or "dark" for overriding prefers-color-scheme. I quickly realized that I could just as easily make it work with multiple themes!

Check out the Demo Page

When switch is triggered it:

  • Adds current theme to data-theme attribute on the html element.
  • Stores the current theme in localStorage under "theme" — component checks for this on reload to set current theme state.
  • Triggers a "theme-switch" custom event with the current theme as detail. This will automatically update any other theme-switch components on the page.
  • Optionally, updates meta theme-color using the "meta-color" prop. This requires that the meta-colors prop is set and that the meta theme-color is included in your html.

You can also trigger the switch externally by either changing the "theme" prop or dispatching a "theme-switch" event with the theme in the detail.

If desired the theme names can be modified from their defaults via props.

Installation

Add the theme-multi-switch.js script to your project. Then add the component <theme-switch></theme-switch> to your html.

CDN

<script src="https://unpkg.com/@colinaut/theme-multi-switch/dist/theme-multi-switch.js"></script>

For best results, add this to your <head> without defer so that it is render blocking. This avoids Flash of inAccurate coloR Theme (FART).

NPM/PNPM/YARN

npm i @colinaut/theme-multi-switch

pnpm i @colinaut/theme-multi-switch

yarn add @colinaut/theme-multi-switch

Eleventy static site

If you are using Eleventy, and want to install locally rather than rely on the CDN, you can install via NPM/PNPM/YARN and then pass through the js file so that it is included in the output. Then you would just need to add it to the head.

eleventyConfig.addPassthroughCopy({
    "node_modules/@colinaut/theme-multi-switch/dist/theme-multi-switch.js": "js/theme-multi-switch.js",
})
<script src="/js/theme-multi-switch.js"></script>

Modifying

Sizing uses em so it can be resized by changing the css font-size. Base color for text, knob, and track border uses currentColor so it will update if you change the body text color.

Further restyling is possible via slots, parts, and css variables. Properties allows changing defaults and values for changing meta theme-color.

Default Slots

  • light
  • auto
  • dark

Slots are dynamically named after the theme names.

Default Parts

  • light
  • auto
  • dark
  • track
  • knob

Label Parts are dynamically named after the theme names.

CSS Variables

  • --theme-switch-knob background: var(--theme-switch-knob, currentColor);
  • --theme-switch-track background: var(--theme-switch-track, #88888822);
  • --theme-switch-track-border border: 0.1em solid var(--theme-switch-border, currentColor);
  • --theme-switch-highlight color: var(--theme-switch-highlight, inherit);
  • --theme-switch-knob-width width: calc(var(--theme-switch-knob-width, 1) * 1em);

Properties

  • themes = "light,auto,dark"
  • theme = defaults to themes[1]
  • meta-colors = ""
  • layout = "around top"

themes and meta-colors accepts either a comma separated string or a stringified JSON array

layout can be either "around top", "around bottom", or just "top" or "bottom"

Example Auto/Light/Dark CSS

/\* Values for light and dark \*/

:root {
    --header-light: rgb(95, 0, 0);
    --body-light: rgb(45, 45, 65);
    --bg-light: rgb(225, 225, 225);
    --track-light: rgb(205 195 165 /0.8);
    --highlight-light: rgb(155, 50, 0);

    --header-dark: rgb(250, 180, 120);
    --body-dark: rgb(225, 225, 225);
    --bg-dark: rgb(45, 45, 65);
    --track-dark: rgb(105 85 55 /0.6);
    --highlight-dark: rgb(225, 120, 100);
}

/\* Automatic Light Mode; Light Mode Override \*/

:root,
:root\[data-theme="light"\] {
    --header: var(--header-light);
    --body: var(--body-light);
    --bg: var(--bg-light);
    --track: var(--track-light);
    --highlight: var(--highlight-light);
}

/\* Dark Mode Override \*/

:root\[data-theme="dark"\] {
    --header: var(--header-dark);
    --body: var(--body-dark);
    --bg: var(--bg-dark);
    --track: var(--track-dark);
    --highlight: var(--highlight-dark);
}

/\* Automatic Dark Mode \*/

@media (prefers-color-scheme: dark) {
    :root {
        --header: var(--header-dark);
        --body: var(--body-dark);
        --bg: var(--bg-dark);
        --track: var(--track-dark);
        --highlight: var(--highlight-dark);
    }
}