Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mode-watcher

Package Overview
Dependencies
Maintainers
0
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mode-watcher

SSR-friendly light and dark mode for SvelteKit

  • 0.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
28K
increased by4.4%
Maintainers
0
Weekly downloads
 
Created
Source

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:

Logos from Sponsors

License

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

FAQs

Package last updated on 19 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc