Skip to content

Commit 50b9743

Browse files
committed
docs: Updated docs to include use-local-storage-store.
1 parent bcb3fc9 commit 50b9743

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
```tsx
2+
const [value, setValue] = useLocalStorageStore<{ id: string; name: string }[]>({
3+
key: 'todos-store',
4+
defaultValue: [],
5+
});
6+
7+
return (
8+
<div>
9+
<For each={value}>
10+
{(todo, index) => {
11+
return (
12+
<div>
13+
<input
14+
value={todo.name}
15+
onInput={event => {
16+
setValue(
17+
produce(_value => {
18+
if (!_value[index()]) return;
19+
_value[index()]!.name = event.target.value ?? '';
20+
}),
21+
);
22+
}}
23+
/>
24+
<button
25+
onClick={() => {
26+
setValue(
27+
produce(_value => {
28+
_value.splice(index(), 1);
29+
}),
30+
);
31+
}}
32+
>
33+
<IconClose width={18} height={18} />
34+
</button>
35+
</div>
36+
);
37+
}}
38+
</For>
39+
40+
<button
41+
onClick={() => {
42+
setValue(
43+
produce(_value => {
44+
_value.push({ id: Math.random().toString(), name: 'New Todo' });
45+
}),
46+
);
47+
}}
48+
>
49+
New Todo
50+
</button>
51+
</div>
52+
);
53+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { useLocalStorageStore } from 'src';
2+
import { ExampleBase } from '../example-base';
3+
import Code from './use-local-storage-store.code.mdx';
4+
5+
import { IconClose } from 'dev/icons';
6+
import { For } from 'solid-js';
7+
import { produce } from 'solid-js/store';
8+
import { useMDXComponents } from 'solid-jsx';
9+
10+
export function UseLocalStorageStoreExample() {
11+
const [value, setValue] = useLocalStorageStore<{ id: string; name: string }[]>({
12+
key: 'todos-store',
13+
defaultValue: [],
14+
});
15+
16+
// @ts-ignore
17+
const components: any = useMDXComponents();
18+
19+
return (
20+
<ExampleBase
21+
title="useLocalStorageStore"
22+
description={
23+
<>
24+
(Improvement) A hook that allows using value from the localStorage as a store. The hook
25+
works exactly the same way as createStore, but also writes the value to the localStorage.
26+
It even works between tabs!
27+
<br />
28+
<br />
29+
To test, try changing the value with two tabs open.
30+
</>
31+
}
32+
code={<Code components={components} />}
33+
>
34+
<div class="flex h-full w-full flex-col items-center justify-center gap-3 rounded-md border p-3 py-10 text-center transition-colors">
35+
<For each={value}>
36+
{(todo, index) => (
37+
<div class="flex flex-wrap items-center gap-1 text-sm">
38+
<input
39+
class="rounded-md border p-1"
40+
value={todo.name}
41+
onInput={event => {
42+
setValue(
43+
produce(_value => {
44+
if (!_value[index()]) return;
45+
_value[index()]!.name = event.target.value ?? '';
46+
}),
47+
);
48+
}}
49+
/>
50+
<button
51+
onClick={() => {
52+
setValue(
53+
produce(_value => {
54+
_value.splice(index(), 1);
55+
}),
56+
);
57+
}}
58+
>
59+
<IconClose width={18} height={18} />
60+
</button>
61+
</div>
62+
)}
63+
</For>
64+
65+
<button
66+
class={`rounded-md bg-primary px-3 py-1.5 text-white transition active:scale-95`}
67+
onClick={() => {
68+
setValue(
69+
produce(_value => {
70+
_value.push({ id: Math.random().toString(), name: 'New Todo' });
71+
}),
72+
);
73+
}}
74+
>
75+
New Todo
76+
</button>
77+
</div>
78+
</ExampleBase>
79+
);
80+
}

dev/icons/close.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { JSX, VoidProps } from 'solid-js';
2+
3+
export default function Code(props: VoidProps<JSX.SvgSVGAttributes<SVGSVGElement>>) {
4+
return (
5+
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" {...props}>
6+
<path
7+
fill="currentColor"
8+
d="m12 13.4l-2.917 2.925q-.277.275-.704.275t-.704-.275q-.275-.275-.275-.7t.275-.7L10.6 12L7.675 9.108Q7.4 8.831 7.4 8.404t.275-.704q.275-.275.7-.275t.7.275L12 10.625L14.892 7.7q.277-.275.704-.275t.704.275q.3.3.3.713t-.3.687L13.375 12l2.925 2.917q.275.277.275.704t-.275.704q-.3.3-.712.3t-.688-.3z"
9+
/>
10+
</svg>
11+
);
12+
}

dev/icons/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
export { default as IconCheck } from './check';
2+
export { default as IconClose } from './close';
23
export { default as IconCode } from './code';
34
export { default as IconCopy } from './copy';
45
export { default as IconEyeDropper } from './eye-dropper';
56
export { default as IconGithub } from './github';
67
export { default as IconLogo } from './logo';
78
export { default as IconSolidJS } from './solidjs';
89
export { default as IconX } from './x';
10+

dev/pages/index/+Page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { UseInViewportExample } from 'dev/components/examples/use-in-viewport/us
2424
import { UseInputStateExample } from 'dev/components/examples/use-input-state/use-input-state.example';
2525
import { UseIntersectionExample } from 'dev/components/examples/use-intersection/use-intersection.example';
2626
import { UseKeyboardExample } from 'dev/components/examples/use-keyboard/use-keyboard.example';
27+
import { UseLocalStorageStoreExample } from 'dev/components/examples/use-local-storage-store/use-local-storage-store.example';
2728
import { UseLocalStorageExample } from 'dev/components/examples/use-local-storage/use-local-storage.example';
2829
import { UseMediaQueryExample } from 'dev/components/examples/use-media-query/use-media-query.example';
2930
import { UseMountedExample } from 'dev/components/examples/use-mounted/use-mounted.example';
@@ -178,6 +179,10 @@ export default function HomePage() {
178179
title: 'useDocumentTitle',
179180
example: <UseDocumentTitleExample />,
180181
},
182+
{
183+
title: 'useLocalStorageStore',
184+
example: <UseLocalStorageStoreExample />,
185+
},
181186
];
182187

183188
const filteredList = createMemo(() => {

0 commit comments

Comments
 (0)