Skip to content

Commit 1be2c7e

Browse files
committed
feat: Added examples for all hooks so far.
1 parent 57f568a commit 1be2c7e

File tree

7 files changed

+100
-12
lines changed

7 files changed

+100
-12
lines changed

dev/components/examples/example-base.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function ExampleBase(props: FlowProps<ExampleBaseProps>) {
1616
<div class="flex w-full items-center justify-between">
1717
<h2 class="text-2xl font-bold">{props.title}</h2>
1818
<button
19-
class="rounded-md border p-2 transition active:scale-95"
19+
class={`rounded-md border p-2 transition active:scale-95 ${viewing() === 'code' ? 'bg-background text-white' : ''}`}
2020
onClick={() => {
2121
setViewing(viewing() === 'code' ? 'result' : 'code');
2222
}}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
```tsx
2-
const [ref, rectStore] = useElementSize();
2+
const { ref, width, height } = useElementSize();
33

44
return (
5-
<textarea ref={ref}>
6-
{JSON.stringify(rectStore, null, 2)}
7-
</textarea>;
8-
)
5+
<div>
6+
<textarea ref={ref}></textarea>
7+
Width: {width().toFixed(2)}
8+
Height: {height().toFixed(2)}
9+
</div>
10+
);
911
```

dev/components/examples/use-hotkeys/use-hotkeys.example.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ function Key(props: FlowProps<{ activated: boolean }>) {
5151
<div class="relative text-xs">
5252
<div class="absolute inset-0 rounded-md bg-neutral-200 transition"></div>
5353
<div
54-
class="relative rounded-md border bg-neutral-50 px-2 py-1.5"
54+
class="relative transform rounded-md border bg-neutral-50 px-2 py-1.5 transition-transform"
5555
style={{
56-
translate: props.activated ? '0px 0px' : '0px -5px',
56+
transform: props.activated ? 'translateY(0px)' : 'translateY(-5px)',
5757
}}
5858
>
5959
{props.children}

dev/components/examples/use-resize-observer/use-resize-observer.code.mdx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
const [ref, rectStore] = useResizeObserver();
33

44
return (
5-
<textarea ref={ref}>
6-
{JSON.stringify(rectStore, null, 2)}
7-
</textarea>;
8-
)
5+
<div>
6+
<textarea ref={ref}></textarea>
7+
{JSON.stringify(rectStore, null, 2)}
8+
</div>
9+
);
910
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
```tsx
2+
const [value, toggle] = useToggle(['apple', 'orange', 'grape', 'kiwi'] as const);
3+
4+
return <div>{value()}</div>;
5+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { useToggle } from 'src';
2+
import { ExampleBase } from '../example-base';
3+
import Code from './use-toggle.code.mdx';
4+
5+
import { useMDXComponents } from 'solid-jsx';
6+
import { createMemo, createSignal, FlowProps, VoidProps } from 'solid-js';
7+
8+
export function UseToggleExample() {
9+
const [value, toggle] = useToggle(['apple', 'orange', 'grape', 'kiwi'] as const);
10+
11+
// @ts-ignore
12+
const components: any = useMDXComponents();
13+
14+
const color = createMemo(() => {
15+
if (value() === 'apple') {
16+
return '#e5312f';
17+
}
18+
19+
if (value() === 'orange') {
20+
return '#fc8627';
21+
}
22+
23+
if (value() === 'grape') {
24+
return '#bc3d73';
25+
}
26+
27+
if (value() === 'kiwi') {
28+
return '#acc144';
29+
}
30+
31+
return undefined;
32+
});
33+
34+
return (
35+
<ExampleBase
36+
title="useToggle"
37+
description="A hook that toggles between two or multiple values (by implementing a common state pattern). Dev Note: Personally this can be called `useCycle` instead since it cycles through the options."
38+
code={<Code components={components} />}
39+
>
40+
<div
41+
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"
42+
style={{
43+
'background-color': color(),
44+
}}
45+
>
46+
<div class="flex flex-wrap gap-3">
47+
<Key activated={value() === 'apple'}>🍎 apple</Key>
48+
<Key activated={value() === 'orange'}>🍊 orange</Key>
49+
<Key activated={value() === 'grape'}>🍇 grape</Key>
50+
<Key activated={value() === 'kiwi'}>🥝 kiwi </Key>
51+
</div>
52+
53+
<button onClick={() => toggle()} class="text-white transition active:scale-95">
54+
Click me to Toggle
55+
</button>
56+
</div>
57+
</ExampleBase>
58+
);
59+
}
60+
61+
function Key(props: FlowProps<{ activated: boolean }>) {
62+
return (
63+
<div class="relative text-xs">
64+
<div class="absolute inset-0 rounded-md bg-neutral-200 transition"></div>
65+
<div
66+
class="relative transform rounded-md border bg-neutral-50 px-2 py-1.5 transition-transform"
67+
style={{
68+
transform: props.activated ? 'translateY(0px)' : 'translateY(-5px)',
69+
}}
70+
>
71+
{props.children}
72+
</div>
73+
</div>
74+
);
75+
}

dev/pages/index/+Page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { UseMountedExample } from 'dev/components/examples/use-mounted/use-mount
1111
import { UseNetworkExample } from 'dev/components/examples/use-network/use-network.example';
1212
import { UseOsExample } from 'dev/components/examples/use-os/use-os.example';
1313
import { UseResizeObserverExample } from 'dev/components/examples/use-resize-observer/use-resize-observer.example';
14+
import { UseToggleExample } from 'dev/components/examples/use-toggle/use-toggle.example';
1415
import { MarkdownContextProvider } from 'dev/components/markdown/markdown.context';
1516
import { IconLogo } from 'dev/icons';
1617
import { title } from 'process';
@@ -86,6 +87,10 @@ export default function HomePage() {
8687
title: 'useElementSize', // Make sure this is under useResizeObserver
8788
example: <UseElementSizeExample />,
8889
},
90+
{
91+
title: 'useToggle',
92+
example: <UseToggleExample />,
93+
},
8994
];
9095

9196
const filteredList = createMemo(() => {

0 commit comments

Comments
 (0)