Lightswitch

Utility

Allows users to toggle between light and dark themes using Tailwind's dark mode feature.

Examples

Getting Started

Add the following component. For best results this will be present on page load.

typescript
<LightSwitch />

Using OS Preference

Alternative

To automatically adjust the mode based on operating system preference, import the following in /src/routes/+layout.svelte.

typescript
import { autoModeWatcher } from '@skeletonlabs/skeleton';

Then add the following in your layout markup.

html
<svelte:head>{@html `<script>${autoModeWatcher.toString()} autoModeWatcher();</script>`}</svelte:head>

Note that Mac OS will update immediately, while other operating systems may require the browser page to be refreshed manually.


Custom Lightswitch

Advanced

Skeleton uses Tailwind's dark mode feature to control light and dark mode. However, we expose all tools used if you wish to create your own custom Lightswitch or interface with the current state.

Set Initial Classes

Import and add the following to your component. This will set the .dark class on the root HTML element in a highly performant manner.

typescript
import { setInitialClassState } from '@skeletonlabs/skeleton';
html
<svelte:head>{@html `<script>${setInitialClassState.toString()} setInitialClassState();</script>`}</svelte:head>

Interface Methods

Skeleton provides several options to interface with the lightswitch settings. Please be aware that light mode is represented by true, while dark mode is represented by false.

typescript
import { modeOsPrefers, modeUserPrefers, modeCurrent } from '@skeletonlabs/skeleton';
Store Value Description
$modeOsPrefers true | false The current operating system setting preference.
$modeUserPrefers true | false | undefined The current user's selected preference.
$modeCurrent true | false The current currently active mode setting.