@svelterun/store

Persisted version of Svelte's writable store.

Installation

pnpm add @svelterun/store
yarn add @svelterun/store
npm i -D @svelterun/store

Usage

./stores.js

import { writable } from '@svelterun/store'

/**
 * @param {string} key - localStorage key 
 * @param {*} value - the store's initial value
 * @returns {import('svelte/store').Writable}
 */
export const preferences = writable('preferences', {
  theme: 'dark',
  pane: '50%',
  // ...
})

./App.svelte

import { get } from 'svelte/store'
import { preferences } from './stores'

// subscribe to changes
preferences.subscribe(value => console.log('preferences:\n', value))

// update value
preferences.update(current => ({...current, theme: 'light'}))

// set value
preferences.set(value)

// read value
get(preferences)

// read value with auto subscription
$preferences

License

MIT © Svelte.run, Nicholas Berlette

Top categories

Loading Svelte Themes