A lightweight Svelte store that automatically persists its value to localStorage. Perfect for maintaining state across browser sessions with minimal setup.
- π Auto-persistence: Automatically syncs store values with
localStorage - π― Type-safe: Full TypeScript support with generic types
- π οΈ Customizable: Custom serialization/deserialization functions
- π§Ή Clean API: Simple, intuitive interface extending Svelte's writable store
npm install @madkarma/svelte-storablebun add @madkarma/svelte-storablepnpm add @madkarma/svelte-storableyarn add @madkarma/svelte-storableimport storable from '@madkarma/svelte-storable';
// Create a persisted store
const count = storable('count', 0);
// Use it like a regular Svelte store
count.set(5);
count.update((n) => n + 1);
// The value is automatically saved to localStorage
// and restored on page reloadIn your Svelte component:
<script>
import storable from '@madkarma/svelte-storable';
const count = storable('count', 0);
</script>
<h1>Count: {$count}</h1>
<button on:click={() => count.update((n) => n + 1)}> Increment </button>Creates a persistent Svelte store.
key(string): The localStorage key to store the value underinitial(T): The initial value of the storeoptions(optional): Configuration objectserialize((value: T) => string): Custom serialization function (defaults toJSON.stringify)deserialize((value: string) => T): Custom deserialization function (defaults toJSON.parse)saveInitial(boolean): Whether to save the initial value to localStorage if no stored value exists (defaults totrue)
A store object with the following methods:
subscribe(callback): Subscribe to store changes (standard Svelte store method)set(value): Set a new valueupdate(updater): Update the value using an updater functionreset(): Reset the store to its initial valueremove(reset?): Remove the value from localStoragereset(boolean): Whether to also reset the store value to initial (defaults totrue)
import storable from '@madkarma/svelte-storable';
const count = storable('count', 0);
count.update((n) => n + 1); // Automatically saved to localStorageconst lastVisit = storable('lastVisit', new Date(), {
serialize: (date) => date.toISOString(),
deserialize: (str) => new Date(str)
});Useful when you only want to persist values that the user has explicitly set:
const preferences = storable('prefs', { theme: 'dark' }, { saveInitial: false });
// Initial value won't be saved to localStorage until user changes ittype UserPreferences = {
theme: 'light' | 'dark';
language: string;
notifications: boolean;
};
const preferences = storable<UserPreferences>('user-prefs', {
theme: 'dark',
language: 'en',
notifications: true
});const settings = storable('settings', { volume: 50 });
settings.set({ volume: 80 });
settings.reset(); // Back to { volume: 50 }const token = storable('auth-token', null);
// Remove from localStorage and reset to initial value
token.remove();
// Remove from localStorage but keep current value in store
token.remove(false);Full TypeScript support is included. The store is generic and will infer types from your initial value:
// Type is inferred as Writable<number>
const count = storable('count', 0);
// Type is inferred as Writable<string>
const name = storable('name', 'John');
// Explicit type annotation
const data = storable<{ id: number; name: string }>('data', {
id: 1,
name: 'Example'
});- Svelte 5.0.0 or higher
Contributions are welcome! Please feel free to submit a Pull Request.