Utility

Dark Mode

Learn how to use Tailwind's dark mode within your Skeleton application. Skeleton provides two methods for handling dark mode. Choose a method that meets your application's requirements.

Page Source

Via Selector

Allows users to toggle between light and dark mode with an interactive Lightswitch component.

typescript
// tailwind.config.[ts|js|cjs]

module.exports = {
  darkMode: 'selector', // <--
  // ...
}

Then implement the Skeleton Lightswitch component.

Via Media

Skeleton provides a utility method to adjust the mode based on the user's operating system preference.

typescript
// tailwind.config.[ts|js|cjs]

module.exports = {
	darkMode: 'media', // <--
	// ...
}

First, import the following in /src/routes/+layout.svelte.

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

In the same file, add the following at the top of the HTML markup.

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

Custom Component

If you wish to build a custom Lightswitch component, Skeleton exposes all required utility methods.

Initilize

Import and add the following within your component. This will set the .dark class on the root HTML element in a highly performant manner. Please note that the CLI installer inserts class="dark" statically in the html element of app.html and you should remove it when going this route.

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

Utilities

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.