Skip to content

Commit 219380a

Browse files
committed
feat: Finished many examples for each hooks + code examples.
1 parent fee1df3 commit 219380a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+841
-4119
lines changed

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"tabWidth": 2,
44
"printWidth": 100,
55
"semi": true,
6+
"plugins": ["prettier-plugin-tailwindcss"],
67
"singleQuote": true,
78
"useTabs": false,
89
"arrowParens": "avoid",

bun.lockb

73.5 KB
Binary file not shown.

dev/app.css

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { IconCode } from 'dev/icons';
2+
import { createSignal, FlowProps, JSX, Show } from 'solid-js';
3+
4+
type ExampleBaseProps = {
5+
title: JSX.Element;
6+
description: string;
7+
class?: string;
8+
code?: JSX.Element;
9+
};
10+
11+
export function ExampleBase(props: FlowProps<ExampleBaseProps>) {
12+
const [viewing, setViewing] = createSignal<'code' | 'result'>('result');
13+
14+
return (
15+
<div class="flex h-full max-h-[500px] w-full flex-col items-start gap-y-5 overflow-hidden rounded-xl border bg-white p-5">
16+
<div class="flex w-full items-center justify-between">
17+
<h2 class="text-2xl font-bold">{props.title}</h2>
18+
<button
19+
class="rounded-md border p-2 transition active:scale-95"
20+
onClick={() => {
21+
setViewing(viewing() === 'code' ? 'result' : 'code');
22+
}}
23+
>
24+
<IconCode class="h-5 w-5" />
25+
</button>
26+
</div>
27+
<p>{props.description}</p>
28+
29+
<Show when={viewing() === 'result'}>
30+
<div class="w-full flex-1 rounded-md">{props.children}</div>
31+
</Show>
32+
33+
<div
34+
style={{ display: viewing() === 'code' ? 'block' : 'none' }}
35+
class="w-full flex-1 overflow-hidden rounded-md border bg-[#1c1e28] p-3 text-white"
36+
>
37+
{props.code}
38+
</div>
39+
</div>
40+
);
41+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
```tsx
2+
const ref = useClickOutside(() => {
3+
console.log('Clicked outside!');
4+
});
5+
6+
return (
7+
<div ref={ref}>
8+
</div>;
9+
)
10+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { useClickOutside } from 'src';
2+
import { ExampleBase } from '../example-base';
3+
import Code from './use-click-outside.code.mdx';
4+
5+
import { useMDXComponents } from 'solid-jsx';
6+
import { createSignal, Show } from 'solid-js';
7+
8+
export function UseClickOutsideExample() {
9+
const ref = useClickOutside(() => {
10+
setClicked(true);
11+
12+
setTimeout(() => {
13+
setClicked(false);
14+
}, 500);
15+
});
16+
const [clicked, setClicked] = createSignal(false);
17+
18+
// @ts-ignore
19+
const components: any = useMDXComponents();
20+
21+
return (
22+
<ExampleBase
23+
title="useClickOutside"
24+
description="Detects clicks outside a ref"
25+
code={<Code components={components} />}
26+
>
27+
<div class="flex h-full w-full flex-1 items-center justify-center rounded-md border p-3 py-10">
28+
<div
29+
class={`select-none rounded-full border bg-neutral-50 p-2 text-xs transition ${clicked() ? 'scale-95' : ''}`}
30+
ref={ref}
31+
>
32+
<Show when={clicked()} fallback={'No detections'}>
33+
You clicked outside!
34+
</Show>
35+
</div>
36+
</div>
37+
</ExampleBase>
38+
);
39+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```tsx
2+
const [ref, rectStore] = useElementSize();
3+
4+
return (
5+
<textarea ref={ref}>
6+
{JSON.stringify(rectStore, null, 2)}
7+
</textarea>;
8+
)
9+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { useElementSize, useIdle, useNetwork, useOs, useResizeObserver } from 'src';
2+
import { ExampleBase } from '../example-base';
3+
import Code from './use-element-size.code.mdx';
4+
5+
import { useMDXComponents } from 'solid-jsx';
6+
7+
export function UseElementSizeExample() {
8+
const { ref, height, width } = useElementSize();
9+
10+
// @ts-ignore
11+
const components: any = useMDXComponents();
12+
13+
return (
14+
<ExampleBase
15+
title="useElementSize"
16+
description="Returns width and height of a resizable element. An abstraction over useResizeObserver."
17+
code={<Code components={components} />}
18+
>
19+
<div class="flex h-full w-full flex-col items-center justify-center gap-3 rounded-md border p-3 py-10 text-start">
20+
<div class="flex flex-col items-center">
21+
<span>Width: {width().toFixed(2)}</span>
22+
<span>Height: {height().toFixed(2)}</span>
23+
</div>
24+
25+
<div class="relative grid flex-1 place-items-center overflow-hidden">
26+
<textarea ref={ref} class="h-20 w-20 resize rounded-md border"></textarea>
27+
<div class="pointer-events-none absolute inset-0 grid place-items-center truncate text-center text-xs text-neutral-400">
28+
Resize Me
29+
</div>
30+
</div>
31+
</div>
32+
</ExampleBase>
33+
);
34+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```tsx
2+
useHotkeys([
3+
['mod+a', () => alert('Ctrl+Shift+A pressed')],
4+
['mod+Enter', () => alert('Alt+Shift+A pressed')],
5+
['shift+g', () => alert('Alt+A pressed')],
6+
]);
7+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { useHotkeys, useOs } from 'src';
2+
import { ExampleBase } from '../example-base';
3+
import Code from './use-hotkeys.code.mdx';
4+
5+
import { useMDXComponents } from 'solid-jsx';
6+
import { createSignal, FlowProps, VoidProps } from 'solid-js';
7+
8+
export function UseHotkeysExample() {
9+
const [activatedHotkey, setActivatedHotkey] = createSignal<-1 | 0 | 1 | 2>(-1);
10+
11+
let timeout: ReturnType<typeof window.setTimeout>;
12+
13+
function handleKeyPress(index: ReturnType<typeof activatedHotkey>) {
14+
if (timeout) clearTimeout(timeout);
15+
16+
setActivatedHotkey(index);
17+
18+
timeout = setTimeout(() => {
19+
setActivatedHotkey(-1);
20+
}, 400);
21+
}
22+
23+
useHotkeys([
24+
['mod+a', () => handleKeyPress(0)],
25+
['mod+Enter', () => handleKeyPress(1)],
26+
['shift+g', () => handleKeyPress(2)],
27+
]);
28+
29+
const os = useOs();
30+
31+
// @ts-ignore
32+
const components: any = useMDXComponents();
33+
34+
return (
35+
<ExampleBase
36+
title="useHotkeys"
37+
description="Handle hotkeys easily"
38+
code={<Code components={components} />}
39+
>
40+
<div class="flex h-full w-full flex-wrap items-center justify-center gap-3 rounded-md border p-3 py-10 text-center">
41+
<Key activated={activatedHotkey() === 0}>{os() === 'macos' ? 'cmd' : 'ctrl'} + a</Key>
42+
<Key activated={activatedHotkey() === 1}>{os() === 'macos' ? 'cmd' : 'ctrl'} + Enter</Key>
43+
<Key activated={activatedHotkey() === 2}>shift + g</Key>
44+
</div>
45+
</ExampleBase>
46+
);
47+
}
48+
49+
function Key(props: FlowProps<{ activated: boolean }>) {
50+
return (
51+
<div class="relative text-xs">
52+
<div class="absolute inset-0 rounded-md bg-neutral-200 transition"></div>
53+
<div
54+
class="relative rounded-md border bg-neutral-50 px-2 py-1.5"
55+
style={{
56+
translate: props.activated ? '0px 0px' : '0px -5px',
57+
}}
58+
>
59+
{props.children}
60+
</div>
61+
</div>
62+
);
63+
}

0 commit comments

Comments
 (0)