A library for managing light and dark mode for a sveltekit site.
When you create a SchemeState instance ( provided in a svelte context ) that instance uses a svelte MediaQuery, the browser's localStorage, and svelte reactivity to track both the visitor's system scheme preference and their preference on your site to update the current scheme.
.current property to update. createSchemeState({ default: 'dark' }) and using a custom svelte:head with a fn to
prevent a Flash Of Unwanted Content that updates the classlist or dataset to dark will
mean that your site defaults to dark mode but the user can override it to be light or
follow their system.
.current property is updated is
entirely up to you. Put it in an $effect or {} and your ui will change accordingly. npm install @lineus/svelte-color-scheme
pnpm install @lineus/svelte-color-scheme
createSchemeState() - a fn that creates the context and instantiates SchemeState.
getSchemeState() - a fn that retrieves the SchemeState instance from the context.
a <SchemeDatasetHeader /> - an optional component to add an iife to <svelte:head> on
first load that will set a data attribute called scheme on the html element.
This is to prevent a FOUC.
a <SchemeClassHeader /> - an optional component to add an iiffe to <svelte:head> on
first load that will add a dark class to the html element. This is to prevent
a FOUC.
in the script of +layout.svelte (presumably the root level):
createSchemeState()in a scheme selecting component of your own making:
const scheme = getSchemeState()npm install or pnpm installnpm run dev --open or pnpm run dev --open