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

mode-watcher

v0.5.0

Published

SSR-friendly light and dark mode for SvelteKit

Downloads

110,232

Readme

Mode Watcher

Simple utilities to manage light & dark mode in your SvelteKit app.

npm version npm downloads license

Installation

npm install mode-watcher

Usage

Add the ModeWatcher component to your root +layout.svelte file.

<script lang="ts">
	import { ModeWatcher } from "mode-watcher";
</script>

<ModeWatcher />
<slot />

The ModeWatcher component will automatically detect the user's preferences and apply/remove the "dark" class, along with the corresponding color-scheme style attribute to the html element.

ModeWatcher will automatically track operating system preferences and apply these if no user preference is set. If you wish to disable this behavior, set the track prop to false:

<ModeWatcher track={false} />

ModeWatcher can also be configured with a default mode instead of automatically detecting the user's preference.

To set a default mode, use the defaultMode prop:

<ModeWatcher defaultMode={"dark"}>

ModeWatcher can manage the theme-color meta tag for you.

To enable this, set the themeColors prop to your preferred colors:

<ModeWatcher themeColors={{ dark: "black", light: "white" }}>

API

toggleMode

A function that toggles the current mode.

<script lang="ts">
	import { toggleMode } from "mode-watcher";
</script>

<button on:click={toggleMode}>Toggle Mode</button>

setMode

A function that sets the current mode. It accepts a string with the value "light", "dark" or "system".

<script lang="ts">
	import { setMode } from "mode-watcher";
</script>

<button on:click={() => setMode("light")}>Set Light Mode</button>
<button on:click={() => setMode("dark")}>Set Dark Mode</button>

resetMode

A function that resets the mode to system preferences.

<script lang="ts">
	import { resetMode } from "mode-watcher";
</script>

<button on:click={() => resetMode()}>System</button>

mode

A readable store that contains the current mode. It can be "light" or "dark" or undefined if evaluated on the server.

<script lang="ts">
	import { setMode, mode } from "mode-watcher";

	function handleModeChange() {
		if ($mode === "light") {
			setMode("dark");
		} else {
			setMode("light");
		}
	}
</script>

<button on:click={handleModeChange}>{$mode}</button>

userPrefersMode

A writeable store that represents the user's mode preference. It can be "light", "dark" or "system".

systemPrefersMode

A readable store that represents the operating system's mode preference. It can be "light", "dark" or undefined if evaluated on the server. Will automatically track changes to the operating system's mode preference unless this is disabled with the tracking() method which takes a boolean. Normally this is disabled by setting the track prop to false in the <ModeWatcher> component.

Demo / Reproduction Template

Open in StackBlitz

Sponsors

This project is supported by the following beautiful people/organizations:

License

Published under the MIT license. Made by @huntabyte, @ollema, and community 💛