Skip to content

Commit 1d2af1c

Browse files
committed
docs: Automated examples generation + hooks count.
1 parent 20d1d1a commit 1d2af1c

File tree

38 files changed

+953
-326
lines changed

38 files changed

+953
-326
lines changed
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
```tsx
22
import { useClickOutside } from 'bagon-hooks';
33

4-
export function App() {
4+
import { createSignal, Show } from 'solid-js';
5+
6+
export function UseClickOutsideExample() {
57
const ref = useClickOutside(() => {
6-
console.log('Clicked outside!');
8+
setClicked(true);
9+
10+
setTimeout(() => {
11+
setClicked(false);
12+
}, 500);
713
});
14+
const [clicked, setClicked] = createSignal(false);
815

9-
return <div ref={ref}>Click outside of me.</div>;
16+
return (
17+
<div class="flex h-full w-full flex-1 items-center justify-center rounded-md border p-3 py-10">
18+
<div
19+
class={`select-none rounded-full border bg-neutral-50 p-2 text-xs transition ${clicked() ? 'scale-95' : ''}`}
20+
ref={ref}
21+
>
22+
<Show when={clicked()} fallback={'No detections'}>
23+
You clicked outside!
24+
</Show>
25+
</div>
26+
</div>
27+
);
1028
}
1129
```
Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
```tsx
2-
const { copied, copy, reset } = useClipboard();
2+
import { useClipboard } from 'bagon-hooks';
33

4-
return (
5-
<div>
6-
<button onClick={() => copy('Bagon is awesome!')}>
7-
<Show
8-
when={copied()}
9-
fallback={<div>Copy</div>}
10-
children={<div>Copied!</div>}
11-
/>
12-
</button>
4+
import { Show } from 'solid-js';
5+
6+
export function UseClipboardExample() {
7+
const { copied, copy, reset } = useClipboard();
8+
9+
return (
10+
<div class="flex h-full w-full flex-col items-center justify-center gap-3 rounded-md border p-3 py-10 text-center text-sm">
11+
<span class="text-center">Bagon is awesome!</span>
12+
<button class="transition active:scale-90" onClick={() => copy('Bagon is awesome!')}>
13+
<Show
14+
when={copied()}
15+
fallback={
16+
<div class="flex items-center gap-x-1">
17+
<IconCopy class="h-8 w-8" />
18+
Copy
19+
</div>
20+
}
21+
children={
22+
<div class="flex items-center gap-x-1 text-green-500">
23+
<IconCheck class="h-8 w-8" /> Copied!
24+
</div>
25+
}
26+
/>
27+
</button>
1328
</div>
14-
);
29+
);
30+
}
1531
```
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
```tsx
2-
const colorScheme = useColorScheme();
2+
import { useColorScheme } from 'bagon-hooks';
33

4-
return <>Your system color scheme is: {colorScheme()}</>;
4+
export function UseColorSchemeExample() {
5+
const colorScheme = useColorScheme();
6+
7+
return (
8+
<div class="flex h-full w-full items-center justify-center gap-x-1 rounded-md border p-3 py-10 text-center text-sm">
9+
<div
10+
class="rounded-md border px-4 py-2 text-sm"
11+
style={{
12+
background: colorScheme() === 'dark' ? '#000' : '#fff',
13+
color: colorScheme() ? '#fff' : '#000',
14+
}}
15+
>
16+
Your system color scheme is: {colorScheme()}
17+
</div>
18+
</div>
19+
);
20+
}
521
```

dev/components/examples/use-counter/use-counter.code.mdx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
```tsx
22
import { useCounter } from 'bagon-hooks';
33

4-
export function App() {
4+
export function UseCounterExample() {
55
const [count, { decrement, increment, reset, set }] = useCounter(5, { min: 1, max: 10 });
6+
67
return (
7-
<div>
8-
{count()}
9-
<button onClick={decrement}>-</button>
10-
<button onClick={increment}>+</button>
11-
<button onClick={reset}>Reset</button>
12-
<button onClick={() => set(Math.floor(Math.random() * 10))}>Set (To Random)</button>
8+
<div class="flex h-full w-full flex-col items-center justify-center gap-5 gap-x-1 rounded-md border p-3 py-10 text-center">
9+
<span>{count()}</span>
10+
11+
<div class="flex gap-x-2 text-sm">
12+
<button class="rounded-md border p-1 px-1.5 transition active:scale-90" onClick={decrement}>
13+
-
14+
</button>
15+
<button class="rounded-md border p-1 px-1.5 transition active:scale-90" onClick={increment}>
16+
+
17+
</button>
18+
<button class="rounded-md border p-1 px-1.5 transition active:scale-90" onClick={reset}>
19+
Reset
20+
</button>
21+
<button
22+
class="rounded-md border p-1 px-1.5 transition active:scale-90"
23+
onClick={() => set(Math.floor(Math.random() * 10))}
24+
>
25+
Set (To Random)
26+
</button>
27+
</div>
1328
</div>
1429
);
1530
}
Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,55 @@
11
```tsx
2-
const [search, setSearch] = createSignal('');
3-
const [searchResults, setSearchResults] = createSignal<{ id: number; title: string }[]>([]);
4-
const [loading, setLoading] = createSignal(false);
2+
import { useDebouncedCallback } from 'bagon-hooks';
53

6-
const debouncedSearch = useDebouncedCallback(async (query: string) => {
4+
import { createSignal, For, JSX, Show } from 'solid-js';
5+
6+
function getSearchResults(query: string): Promise<{ id: number; title: string }[]> {
7+
return new Promise(resolve => {
8+
setTimeout(() => {
9+
resolve(
10+
query.trim() === ''
11+
? []
12+
: Array(5)
13+
.fill(0)
14+
.map((_, index) => ({ id: index, title: `${query} ${index + 1}` })),
15+
);
16+
}, 1000);
17+
});
18+
}
19+
20+
export function UseDebouncedCallbackExample() {
21+
const [search, setSearch] = createSignal('');
22+
const [searchResults, setSearchResults] = createSignal<{ id: number; title: string }[]>([]);
23+
const [loading, setLoading] = createSignal(false);
24+
25+
const debouncedSearch = useDebouncedCallback(async (query: string) => {
726
setLoading(true);
827
setSearchResults(await getSearchResults(query));
928
setLoading(false);
10-
}, 500);
29+
}, 500);
1130

12-
const handleInput: JSX.EventHandler<HTMLInputElement, InputEvent> = event => {
31+
const handleInput: JSX.EventHandler<HTMLInputElement, InputEvent> = event => {
1332
setSearch(event.currentTarget.value);
1433
debouncedSearch(event.currentTarget.value);
15-
};
34+
};
35+
36+
return (
37+
<div class="flex h-full w-full flex-col items-center justify-center gap-x-1 gap-y-2 rounded-md border p-3 py-10 text-center text-sm">
38+
<input
39+
value={search()}
40+
onInput={handleInput}
41+
class="rounded-md border p-2"
42+
placeholder="Search..."
43+
/>
1644

17-
return (
18-
<div>
19-
<input type="text" value={search} onInput={handleInput} />
20-
<Show when={loading()}
21-
children={<div>Loading...</div>}
22-
fallback={<For each={searchResults()}>{result => <div>{result.title}</div>}</For>}
23-
/>
24-
</div>;
25-
)
45+
<Show
46+
when={loading()}
47+
children={<>Loading...</>}
48+
fallback={
49+
<For each={searchResults()}>{result => <div class="text-xs">{result.title}</div>}</For>
50+
}
51+
/>
52+
</div>
53+
);
54+
}
2655
```
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
```tsx
2-
const [signal, setSignal] = useDebouncedSignal('', 500);
3-
return (
4-
<>
5-
<input value={signal()} onInput={e => setSignal(e.currentTarget.value)} />
6-
<span>State: {JSON.stringify(signal())}</span>
7-
</>
8-
);
2+
import { useDebouncedSignal } from 'bagon-hooks';
3+
4+
export function UseDebouncedSignalExample() {
5+
const [signal, setSignal] = useDebouncedSignal('', 500);
6+
7+
return (
8+
<div class="flex h-full w-full flex-col items-center justify-center gap-x-1 gap-y-2 rounded-md border p-3 py-10 text-center text-sm">
9+
<input
10+
value={signal()}
11+
onInput={e => setSignal(e.currentTarget.value)}
12+
class="rounded-md border p-2"
13+
/>
14+
<span>State: {JSON.stringify(signal())}</span>
15+
</div>
16+
);
17+
}
918
```
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
```tsx
2-
const id = useId();
2+
import { useDebouncedValue } from 'bagon-hooks';
33

4-
return (
5-
<div>
6-
Random ID: {id()}
7-
</div>;
8-
)
4+
import { createSignal } from 'solid-js';
5+
6+
export function UseDebouncedValueExample() {
7+
const [signal, setSignal] = createSignal('');
8+
const [value, cancel] = useDebouncedValue(signal, 500);
9+
10+
return (
11+
<div class="flex h-full w-full flex-col items-center justify-center gap-x-1 gap-y-2 rounded-md border p-3 py-10 text-center text-sm">
12+
<input
13+
value={signal()}
14+
onInput={e => setSignal(e.currentTarget.value)}
15+
class="rounded-md border p-2"
16+
/>
17+
<div class="flex items-center gap-x-2">
18+
<span>State: {JSON.stringify(signal())}</span>
19+
<span>|</span>
20+
<span>Value: {JSON.stringify(value())}</span>
21+
</div>
22+
</div>
23+
);
24+
}
925
```
Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
```tsx
2-
const [signal, setSignal] = createSignal(0);
2+
import { useDidUpdate } from 'bagon-hooks';
33

4-
useDidUpdate(() => {
4+
import { createSignal } from 'solid-js';
5+
6+
export function UseDidUpdateExample() {
7+
const [signal, setSignal] = createSignal(0);
8+
9+
useDidUpdate(() => {
510
console.log('Did Update', signal());
6-
}, signal);
11+
}, signal);
712

8-
return (
9-
<div>
10-
<button onClick={setSignal(prev => prev + 1)}>Simulate Update {signal()}</button>
11-
</div>;
12-
)
13+
return (
14+
<div class="flex h-full w-full flex-col items-center justify-center gap-x-1 gap-y-2 rounded-md border p-3 py-10 text-center text-sm">
15+
<p class="max-w-xs text-center text-xs">
16+
This logs "Did Update {signal() === 0 ? 'X' : signal()}" to the console. Notice that it
17+
doesn't log "Did Update 0" since it happens on mount.
18+
</p>
19+
<button
20+
class="rounded-md bg-primary p-2 text-white transition active:scale-95"
21+
onClick={() => {
22+
setSignal(signal() + 1);
23+
}}
24+
>
25+
Simulate an Update {signal()}
26+
</button>
27+
</div>
28+
);
29+
}
1330
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```tsx
2+
export function UseDisclosureExample() {
3+
return (
4+
<div class="flex h-full w-full items-center justify-center gap-x-1 rounded-md border p-3 py-10 text-center text-sm"></div>
5+
);
6+
}
7+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ExampleBase } from '../example-base';
2+
import Code from './use-disclosure.code.mdx';
3+
4+
import { useMDXComponents } from 'solid-jsx';
5+
6+
export function UseDisclosureExample() {
7+
// @ts-ignore
8+
const components: any = useMDXComponents();
9+
10+
return (
11+
<ExampleBase
12+
title="useDisclosure"
13+
description="A hook to manage state for dialogs, modals, etc."
14+
code={<Code components={components} />}
15+
>
16+
<div class="flex h-full w-full items-center justify-center gap-x-1 rounded-md border p-3 py-10 text-center text-sm"></div>
17+
</ExampleBase>
18+
);
19+
}

0 commit comments

Comments
 (0)