Skip to content

Instantly share code, notes, and snippets.

@jonshipman
jonshipman / persistent-writable.ts
Created April 4, 2024 18:25 — forked from kevinsschmidt/persistent-writable.ts
Svelte Persistent Writable
import { writable, type Updater, type Writable } from 'svelte/store';
/**
* Create a writable store that persists to sessionStorage or localStorage.
*
* Usage example:
* export const colorScheme = createPersistentWritable<'system' | 'dark' | 'light'>({ storageId: 'color-scheme', useLocalStorage: true }, 'dark');
*/
export function createPersistentWritable<T>(
{ storageId, useLocalStorage }: { storageId: string; useLocalStorage?: boolean },