Skip to content

Commit b7b2e89

Browse files
committed
fix!: persisted is now persisted-store and queryParam is now query-param-store which is a breaking change if you use deep imports for either - filenames were changed to make work with Kitbook updates
1 parent d89ae09 commit b7b2e89

11 files changed

+85
-94
lines changed

src/lib/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ export { default as DataList } from './ui/DataList.svelte';
4242
export { default as MultiSelect } from './ui/MultiSelect.svelte';
4343

4444
export { default as QueryParam } from './stores/QueryParam.svelte';
45-
export { createPersistedStore } from './stores/persisted.js';
46-
export { createQueryParamStore, type QueryParamStore } from './stores/queryParam.js';
45+
export { createPersistedStore } from './stores/persisted-store.js';
46+
export { createQueryParamStore, type QueryParamStore } from './stores/query-param-store.js';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script lang="ts">
2+
import QueryParam from '$lib/stores/QueryParam.svelte';
3+
import Button from '$lib/ui/Button.svelte';
4+
const persist: 'localStorage' | 'sessionStorage' = 'localStorage';
5+
</script>
6+
7+
<QueryParam key="first" replaceState startWith="hello" {persist} let:value let:set let:remove on:value={(e) => console.log(e.detail)}>
8+
<pre class="pl-3">{JSON.stringify(value, null, 2)}</pre>
9+
<Button href="/stores/1-query-param-component?first=123">nav to 123</Button>
10+
<Button onclick={() => set('hey')}>Set to 'hey'</Button>
11+
<Button onclick={remove}>Remove</Button>
12+
</QueryParam>
13+
14+
<QueryParam key="text" startWith="hey" persist="localStorage" let:value let:set on:value={(e) => console.log(e.detail)}>
15+
<input type="text" {value} on:input={(e) => set(e.currentTarget.value)} placeholder="Type here" />
16+
<input type="text" {value} on:input={(e) => set(e.currentTarget.value)} placeholder="Type here" />
17+
<pre class="pl-3">{JSON.stringify(value, null, 2)}</pre>
18+
</QueryParam>
19+

src/lib/stores/QueryParam.md

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/lib/stores/QueryParam.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import { createEventDispatcher, onDestroy } from 'svelte';
33
import type { Unsubscriber } from 'svelte/store';
4-
import { createQueryParamStore, type QueryParamStoreOptions } from './queryParam';
4+
import { createQueryParamStore, type QueryParamStoreOptions } from './query-param-store';
55
66
export let key: string;
77
export let replaceState = true;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script lang="ts">
2+
import { createPersistedStore } from '$lib';
3+
import Button from '$lib/ui/Button.svelte';
4+
5+
const showSomething = createPersistedStore<boolean>('myCustomKeyName', true, true);
6+
const showSomethingElse = createPersistedStore<boolean>('myCustomKeyName2', true, true);
7+
</script>
8+
9+
{$showSomething}
10+
<Button onclick={() => ($showSomething = !$showSomething)}>Toggle showSomething</Button>,
11+
{$showSomethingElse}
12+
<Button onclick={() => ($showSomethingElse = !$showSomethingElse)}>Toggle showSomethingElse</Button>

src/lib/stores/persisted-store.md

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
1-
<script lang="ts">
2-
import { createPersistedStore } from '$lib';
3-
import Button from '$lib/ui/Button.svelte';
4-
import { Story } from 'kitbook';
5-
6-
const showSomething = createPersistedStore<boolean>('myCustomKeyName', true, true);
7-
const showSomethingElse = createPersistedStore<boolean>('myCustomKeyName2', true, true);
8-
</script>
9-
10-
<!-- prettier-ignore -->
111
# Persisted Store
122

13-
Useful for user settings that don't need saved in a database. First argument is the key, second is the value, and add an optional true third argument to sync across tabs.
3+
Useful for user settings that don't need saved in a database, but just in `localStorage`. First argument is the key, second is the value, and add an optional true third argument to sync across tabs.
144

155
`const showSomething = createPersistedStore<boolean>('myCustomKeyName', true, true);`
166

177
The type definition will be automatically inferred from the value passed in, though you can explicitly type it as seen in the example here to enforce a desired type.
188

19-
<Story name="tab synced">
20-
{$showSomething}
21-
<Button onclick={() => ($showSomething = !$showSomething)}>Toggle showSomething</Button>,
22-
{$showSomethingElse}
23-
<Button onclick={() => ($showSomethingElse = !$showSomethingElse)}>Toggle showSomethingElse</Button>
24-
</Story>
25-
26-
Tab syncing can be seen if you open this page in two tabs and toggle the value.
9+
Tab syncing can be seen if you open this page in two tabs and toggle the value (you can observe the two iframes in sync below, but tab persistence is what you're really after).
File renamed without changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script lang="ts">
2+
import { createPersistedStore } from '$lib';
3+
import Button from '$lib/ui/Button.svelte';
4+
5+
const showSomething = createPersistedStore<boolean>('myCustomKeyName', true, true);
6+
const showSomethingElse = createPersistedStore<boolean>('myCustomKeyName2', true, true);
7+
</script>
8+
9+
{$showSomething}
10+
<Button onclick={() => ($showSomething = !$showSomething)}>Toggle showSomething</Button>,
11+
{$showSomethingElse}
12+
<Button onclick={() => ($showSomethingElse = !$showSomethingElse)}>Toggle showSomethingElse</Button>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<script lang="ts">
2+
import Button from '$lib/ui/Button.svelte';
3+
4+
import { createQueryParamStore } from './query-param-store';
5+
const item = createQueryParamStore<string | number | boolean | any[] | typeof object>({ key: 'item', log: true, replaceState: true, persist: 'localStorage', 'startWith': 'foo' });
6+
7+
const object = {
8+
foo: 'hi there',
9+
bar: {
10+
blah: 123,
11+
quux: [1, 2, 3],
12+
},
13+
};
14+
</script>
15+
16+
<!-- # Query Param Store -->
17+
18+
<!-- Update by changing URL: -->
19+
<Button href="?compositionName=default">No query param</Button>
20+
<Button href="?compositionName=default&item=">no value</Button>
21+
<Button href="?compositionName=default&item=123">nav to 123</Button>
22+
<Button href="?compositionName=default&item=1234">nav to 1234</Button>
23+
24+
<!-- Update by setting store value: -->
25+
<Button onclick={() => ($item = '')}>Set to empty string</Button>
26+
<Button onclick={() => ($item = 12345)}>Set to 12345</Button>
27+
<Button onclick={() => ($item = 123456)}>Set to 123456</Button>
28+
<Button onclick={() => ($item = 0)}>Set to 0</Button>
29+
<Button onclick={() => ($item = object)}>Set to object</Button>
30+
<Button onclick={() => ($item = [1, 'hi', object])}>Set to array w/ object</Button>
31+
<Button onclick={() => ($item = 'Hello world? & some = that')}>Set to String with characters needing escaped</Button>
32+
<Button onclick={() => ($item = null)}>Set to null</Button>
33+
<Button onclick={() => ($item = undefined)}>Set to undefined</Button>
34+
<Button onclick={() => ($item = false)}>Set to false</Button>
35+
<Button onclick={() => item.remove()}>Remove function</Button>
36+
37+
<pre>{JSON.stringify($item, null, 2)}</pre>

src/lib/stores/query-param-store.md

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,5 @@
1-
<script lang="ts">
2-
import Button from '$lib/ui/Button.svelte';
3-
4-
import { createQueryParamStore } from '$lib/stores/queryParam';
5-
const item = createQueryParamStore({ key: 'item', log: true, replaceState: true, persist: 'localStorage', 'startWith': 'foo' });
6-
7-
const object = {
8-
foo: 'hi there',
9-
bar: {
10-
blah: 123,
11-
quux: [1, 2, 3],
12-
},
13-
};
14-
</script>
15-
161
# Query Param Store
172

18-
Update by changing URL:
19-
<Button href="/stores/0-query-param">No query param</Button>
20-
<Button href="/stores/0-query-param?item=">no value</Button>
21-
<Button href="/stores/0-query-param?item=123">nav to 123</Button>
22-
<Button href="/stores/0-query-param?item=1234">nav to 1234</Button>
23-
24-
Update by setting store value:
25-
<Button onclick={() => ($item = '')}>Set to empty string</Button>
26-
<Button onclick={() => ($item = 12345)}>Set to 12345</Button>
27-
<Button onclick={() => ($item = 123456)}>Set to 123456</Button>
28-
<Button onclick={() => ($item = 0)}>Set to 0</Button>
29-
<Button onclick={() => ($item = object)}>Set to object</Button>
30-
<Button onclick={() => ($item = [1, 'hi', object])}>Set to array w/ object</Button>
31-
<Button onclick={() => ($item = 'Hello world? & some = that')}>Set to String with characters needing escaped</Button>
32-
<Button onclick={() => ($item = null)}>Set to null</Button>
33-
<Button onclick={() => ($item = undefined)}>Set to undefined</Button>
34-
<Button onclick={() => ($item = false)}>Set to false</Button>
35-
<Button onclick={() => item.remove()}>Remove function</Button>
36-
37-
<pre>{JSON.stringify($item, null, 2)}</pre>
38-
39-
<!-- prettier-ignore -->
403
- server/client side query params based store
414
- Must be initialized from within a `.svelte` file and not a `.ts` file (see https://github.com/sveltejs/kit/issues/2381) because only components can access the `$page` store. Listening to the URL won't work because we want this to work server-side as well.
425
- Only works with SvelteKit as it relies on kit imports. Feel to copy the code and create your own that works with other Svelte frameworks. If needed, refer to the inspiration repos for this tool: [svelte-pathfinder](https://github.com/sveltetools/svelte-pathfinder) and [svelte-store-router](https://github.com/zyxd/svelte-store-router)

0 commit comments

Comments
 (0)