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

themur

v0.3.1

Published

A vanilla JavaScript theme switcher

Downloads

20

Readme

Themur.js

A lightweight vanilla JS theme switcher with support for localStorage and the prefers-color-scheme media query.

Demo

Getting started

The easiest way to use Themur is to include it in a <script> tag on your site via the jsDelivr CDN:

<script src="https://cdn.jsdelivr.net/npm/themur@latest/dist/themur.min.js"></script>

Then add a button element to you page that will be used to control toggling themes.

🚨 It's important to note the hidden attribute here. Themur will remove this attribute so that the button is visible as long as JavaScript is available. In the event that JavaScript isn't available the button will be hidden since Themur would no longer function anyway.

<button id="my-theme-toggle" hidden>

Using as a module

If you are using a module bundling system like Webpack, Themur is also available as an ES6 module you can include in your project. After you've added Themur as a dependency to your project using npm you can then import it into your project like any other module:

import Themur from 'themur';

const themeSwitcher = new Themur({
  /* options */
});

Options

The Themur instance takes one argument, an Object with the following options:

  • toggleElement: HTMLButtonElement | required
  • containerElement: HTMLElement | default: document.body,
  • themeClass: String | default: my-theme,
  • storageKey: String | default: 'themeEnabled',
  • useLocalStorage: Boolean | default: false

API Methods

There are a couple of methods available on the Themur instance that you can use programmatically in your own scripts if needed.

  • enableTheme() - Enables the theme. This will add the themeClass to the containerElement and set the storageKey to "true" if have the useLocalStorage set to true.
  • disableTheme() - disables the theme. This will remove the themeClass from the containerElement and set the storageKey to "false" if have the useLocalStorage set to true.

API usage

// Create a new Themur instance.
const switcher = new Themur({
  // Options...
});

// Later in your script programmatically activate your theme.
switcher.enableTheme(); 

// Later in your script deactivate your theme.
switcher.disableTheme(); 

User preferred theme settings

Themur will first check for a users theme OS preferences by using window.matchMedia(). If the useLocalStorage Themur option is set to true it will store the user's preference in localStorage. This means that if the user has their OS dark theme enabled it will set the storage key to "true".

About using localStorage

Themur comes with an configuration option that allows you to use the browser's localStorage API to persist theme selection. When the useLocalStorage option is set to true Themur will save users' theme choice in localStorage persisting their theme choice for your site from page view to page view, and even visit to visit.

Example

The Themur instance requires only one option, toggleElement. The toggleElement should be an button element that will be used to switch themes. The rest of the options default to the ones listed above.

1. Add the HTML to your page:

<button id="theme-switcher">Select theme</button>

2. Create a new instance of Themur with your options:

const theme = new Themur({
  toggleElement: document.getElementById('theme-switcher'),
  themeClass: 'dark-theme',
  useLocalStorage: true
});

By default Themur will use document.body to add/remove a CSS class that you can use as a hook to specify styles for your alternate theme. In this demo, I'm using CSS Custom Properties to easily update the styles for the "Dark theme" specified in the configuration above.

:root {
  --color-bg: #ffffff;
  --color-text: #2c2e3d;
}

.dark-theme {
  --color-bg: #2c2e3d;
  --color-text: #ffffff;
}

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