magicbyt3 / svelte-local-storage-store

Adds pub/sub to local storage.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

npm version license codecov

svelte-local-storage-store

A store that adds pub/sub to local storage. Supports changes across multiple tabs.

Installation

npm install svelte-local-storage-store

Usage

Define the store:

import { writable } from 'svelte-local-storage-store'

// First param `preferences` is the local storage key.
// Second param is the initial value.
export const preferences = writable('preferences', {
  theme: 'dark',
  pane: '50%',
  ...
})

Then when you want to use the store:

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

preferences.subscribe(...) // subscribe to changes
preferences.update(...) // update value
preferences.set(...) // set value
get(preferences) // read value
$preferences // read value with automatic subscription

License

MIT

About

Adds pub/sub to local storage.

License:MIT License


Languages

Language:TypeScript 94.1%Language:JavaScript 5.9%