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

@georapbox/theme-toggle-element

v4.0.2

Published

A custom element that allows you to toggle between dark and light theme.

Downloads

24

Readme

npm version npm license

<theme-toggle>

A custom element that allows you to toggle between light, dark and system theme.

How it works

By default, the component determines the theme from user's system preferences using the prefers-color-scheme media query. When the theme is changed, by clicking the toggle button, the selected theme is saved in localStorage if noStorage is false (default). A data-theme attibute is added to the root element of the document, with the appropriate value (light, dark, system). The default value is system, but if the user has previously selected a theme and the noStorage property is not set to true, the saved theme is used instead.

API documentationDemo

Install

$ npm install --save @georapbox/theme-toggle-element

Usage

Script

import { ThemeToggle } from './node_modules/@georapbox/theme-toggle-element/dist/theme-toggle.js';

// Manually define the element.
ThemeToggle.defineCustomElement();

Alternatively, you can import the automatically defined custom element.

import './node_modules/@georapbox/theme-toggle-element/dist/theme-toggle-defined.js';

Markup

<theme-toggle storage-key="theme-preference"></theme-toggle>

Style

The component comes with a bare minimum style, but you can override it by using the CSS Parts provided. See the usage example for more details.

API

Properties

| Name | Reflects | Type | Required | Description | | ---- | -------- | ---- | -------- |------------ | | noStorageno-storage | ✓ | Boolean | - | Indicates whether the theme state should be persisted to local storage. Any previously saved preference is ignored, but is not removed from local storage. | | storageKeystorage-key | ✓ | String | - | The key to be used in local storage to save the theme preference. If omitted, the default value is theme-toggle/theme-preference. If noStorage is true, this property is ignored. | | noIconno-icon | ✓ | Boolean | - | Indicates whether the icon should be displayed or not. | | noLabelno-label | ✓ | Boolean | - | Indicates whether the label should be displayed or not. For accessibility reasons, the label is only visually hidden, but still available to screen readers. |

Slots

| Name | Description | | ---- | ----------- | | light | The content for the light theme (icon and label). | | icon-light | The content for the light theme icon. | | label-light | The content for the light theme label. | | dark | The content for the dark theme (icon and label). | | icon-dark | The content for the dark theme icon. | | label-dark | The content for the dark theme label. | | system | The content for the system theme (icon and label). | | icon-system | The content for the system theme icon. | | label-system | The content for the system theme label. |

CSS Parts

| Name | Description | | ---- | ----------- | | base | The componen'ts base wrapper. In this case this is a native <button> element. | | icon | The icon's wrapper element (including the light, dark and system theme icons). | | icon-light | The light theme icon's wrapper element. | | icon-dark | The dark theme icon's wrapper element. | | icon-system | The system theme icon's wrapper element. | | label | The label's wrapper element (including the light, dark and system theme labels). | | label-light | The light theme label's wrapper element. | | label-dark | The dark theme label's wrapper element. | | label-system | The system theme label's wrapper element. |

Methods

| Name | Type | Description | Arguments | | ---- | ---- | ----------- | --------- | | defineCustomElement | Static | Defines/registers the custom element with the name provided. If no name is provided, the default name is used. The method checks if the element is already defined, hence will skip trying to redefine it. | elementName='theme-toggle' |

Events

| Name | Description | Event Detail | | ---- | ----------- | ------------ | | tt-theme-change | Emitted when theme changes by user's interaction. | {theme: 'light' \| 'dark' \| 'system'} |

Usage example

HTML

<theme-toggle></theme-toggle>

CSS

Customize the component's appearance by using CSS custom properties and CSS parts.

:root {
  --body-color: #000;
  --body-bg-color: #fff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --body-color: #fff;
    --body-bg-color: #000;
  }
}

:root[data-theme="light"] {
  --body-color: #000;
  --body-bg-color: #fff;
}

:root[data-theme="dark"] {
  --body-color: #fff;
  --body-bg-color: #000;
}

theme-toggle:not(:defined) {
  display: none;
}

theme-toggle::part(base) {
  border: 1px solid var(--body-color);
}

JavaScript

import { ThemeToggle } from './node_modules/@georapbox/theme-toggle-element/dist/theme-toggle.js';

ThemeToggle.defineCustomElement();

documemt.addEventListener('tt-theme-change', evt => {
  console.log('tt-theme-change ->', evt.detail);
});

Changelog

For API updates and breaking changes, check the CHANGELOG.

Development setup

Prerequisites

The project requires Node.js and npm to be installed on your environment. Preferrably, use nvm Node Version Manager and use the version of Node.js specified in the .nvmrc file by running nvm use.

Install dependencies

Install the project dependencies by running the following command.

npm install

Build for development

Watch for changes and start a development server by running the following command.

npm start

Linting

Lint the code by running the following command.

npm run lint

Testing

Run the tests by running any of the following commands.

npm test
npm run test:watch # watch mode

Build for production

Create a production build by running the following command.

npm run build

License

The MIT License (MIT)