Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 7cb7ea1

Browse files
authored
docs: add docs for several hooks (#11)
1 parent 2073682 commit 7cb7ea1

19 files changed

+685
-7
lines changed

docs/.vitepress/config.ts

+158
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,164 @@ export default defineConfig({
3939
},
4040
],
4141
},
42+
{
43+
text: "Hooks",
44+
base: "/hooks",
45+
items: [
46+
{
47+
text: "useAsyncEffect",
48+
link: "/use-async-effect",
49+
},
50+
{
51+
text: "useBoolean",
52+
link: "/use-boolean",
53+
},
54+
{
55+
text: "useClickOutside",
56+
link: "/use-click-outside",
57+
},
58+
{
59+
text: "useClipboardText",
60+
link: "/use-clipboard-text",
61+
},
62+
{
63+
text: "useConstFn",
64+
link: "/use-const-fn",
65+
},
66+
{
67+
text: "useConst",
68+
link: "/use-const",
69+
},
70+
{
71+
text: "useCounter",
72+
link: "/use-counter",
73+
},
74+
{
75+
text: "useDebounceEffect",
76+
link: "/use-debounce-effect",
77+
},
78+
{
79+
text: "useDebounce",
80+
link: "/use-debounce",
81+
},
82+
{
83+
text: "useDocumentEventListener",
84+
link: "/use-document-event-listener",
85+
},
86+
{
87+
text: "useDocument",
88+
link: "/use-document",
89+
},
90+
{
91+
text: "useElementSize",
92+
link: "/use-element-size",
93+
},
94+
{
95+
text: "useEventListener",
96+
link: "/use-event-listener",
97+
},
98+
{
99+
text: "useFocusTrap",
100+
link: "/use-focus-trap",
101+
},
102+
{
103+
text: "useHover",
104+
link: "/use-hover",
105+
},
106+
{
107+
text: "useInterval",
108+
link: "/use-interval",
109+
},
110+
{
111+
text: "useIsMounted",
112+
link: "/use-is-mounted",
113+
},
114+
{
115+
text: "useKeydown",
116+
link: "/use-keydown",
117+
},
118+
{
119+
text: "useLatest",
120+
link: "/use-latest",
121+
},
122+
{
123+
text: "useLocalStorage",
124+
link: "/use-local-storage",
125+
},
126+
{
127+
text: "useMediaQuery",
128+
link: "/use-media-query",
129+
},
130+
{
131+
text: "useMount",
132+
link: "/use-mount",
133+
},
134+
{
135+
text: "usePrevious",
136+
link: "/use-previous",
137+
},
138+
{
139+
text: "useRerender",
140+
link: "/use-rerender",
141+
},
142+
{
143+
text: "useSafeLayoutEffect",
144+
link: "/use-safe-layout-effect",
145+
},
146+
{
147+
text: "useSessionStorage",
148+
link: "/use-session-storage",
149+
},
150+
{
151+
text: "useTextSelection",
152+
link: "/use-text-selection",
153+
},
154+
{
155+
text: "useTheme",
156+
link: "/use-theme",
157+
},
158+
{
159+
text: "useThrottleEffect",
160+
link: "/use-throttle-effect",
161+
},
162+
{
163+
text: "useThrottle",
164+
link: "/use-throttle",
165+
},
166+
{
167+
text: "useTimeout",
168+
link: "/use-timeout",
169+
},
170+
{
171+
text: "useTitle",
172+
link: "/use-title",
173+
},
174+
{
175+
text: "useToggle",
176+
link: "/use-toggle",
177+
},
178+
{
179+
text: "useUnmount",
180+
link: "/use-unmount",
181+
},
182+
{
183+
text: "useUnsafeOnceEffect",
184+
link: "/use-unsafe-once-effect",
185+
},
186+
{
187+
text: "useUpdate",
188+
link: "/use-update",
189+
},
190+
{
191+
text: "useWindowSize",
192+
link: "/use-window-size",
193+
},
194+
{
195+
text: "useWindow",
196+
link: "/use-window",
197+
},
198+
],
199+
},
42200
],
43201
socialLinks: [
44202
{

docs/hooks/use-async-effect.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# useAsyncEffect
2+
3+
Same as `useEffect`, but the effect can be asynchronous.
4+
5+
```ts
6+
useAsyncEffect(async () => {
7+
await fetch("https://api.example.com/data");
8+
}, []);
9+
```
10+
11+
## Parameters
12+
13+
### effect
14+
15+
- Type: `() => Promise<void>`
16+
17+
The asynchronous effect to run.
18+
19+
> [!NOTE]
20+
>
21+
> Destructor is not yet supported. The cleanup function returned by the effect will be ignored and discarded.
22+
23+
### deps
24+
25+
- Type: `DependencyList`
26+
- Default: `undefined`
27+
28+
The dependencies of the effect, just like in `useEffect`.

docs/hooks/use-boolean.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# useBoolean
2+
3+
Use a boolean value.
4+
5+
```ts
6+
const { value, set, setTrue, setFalse, toggle, reset } = useBoolean();
7+
```
8+
9+
## Parameters
10+
11+
### initialValue
12+
13+
- Type: `boolean`
14+
- Default: `false`
15+
16+
The initial value of the boolean.
17+
18+
## Returns
19+
20+
### value
21+
22+
- Type: `boolean`
23+
24+
The current value of the boolean.
25+
26+
### set
27+
28+
- Type: `Dispatch<SetStateAction<boolean>>`
29+
30+
Set the value to any boolean.
31+
32+
### setTrue
33+
34+
- Type: `() => void`
35+
36+
Set the value to `true`.
37+
38+
### setFalse
39+
40+
- Type: `() => void`
41+
42+
Set the value to `false`.
43+
44+
### toggle
45+
46+
- Type: `() => void`
47+
48+
Toggle the value, that is, to set the value to `true` if it is currently `false`, and to `false` if it is currently `true`.
49+
50+
### reset
51+
52+
- Type: `() => void`
53+
54+
Reset the value to the initial value.

docs/hooks/use-click-outside.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# useClickOutside
2+
3+
Listen to click events outside of a node.
4+
5+
```ts
6+
const modalRef = useRef<HTMLDivElement>(null);
7+
8+
useClickOutside(modalRef, () => closeModal());
9+
```
10+
11+
## Parameters
12+
13+
### ref
14+
15+
- Type: `RefObject<Node>`
16+
17+
The ref of the node to listen for click events outside of.
18+
19+
### callback
20+
21+
- Type: `(event: MouseEvent, target: Node) => void`
22+
23+
The function to call when a click event occurs outside of the node.
24+
25+
The first argument is the click event, and the second argument is the target node contained inside the `ref`.

docs/hooks/use-clipboard-text.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# useClipboardText
2+
3+
Use the text on the user's clipboard.
4+
5+
```ts
6+
const { text, error, write } = useClipboardText();
7+
```
8+
9+
## Parameters
10+
11+
### options
12+
13+
- Type: `UseClipboardTextOptions`
14+
- Default: `{}`
15+
16+
The options to configure the hook.
17+
18+
## Options
19+
20+
### readOnMount
21+
22+
- Type: `boolean`
23+
- Default: `true`
24+
25+
Whether to immediately read the user's clipboard when the hook is mounted.
26+
27+
If set to `false`, the returned `text` will be its default value, that is, `""`.
28+
29+
When this option is changed from `false` to `true`, the hook will read the user's clipboard.
30+
31+
## Returns
32+
33+
### text
34+
35+
- Type: `string`
36+
- Default: `""`
37+
38+
The text on the user's clipboard.
39+
40+
### error
41+
42+
- Type: `Error | null`
43+
- Default: `null`
44+
45+
The error that occurs while reading from or writing to the user's clipboard.
46+
47+
After any successful read or write operation, `error` will be reset to `null`.
48+
49+
### write
50+
51+
- Type: `(text: string) => Promise<void>`
52+
53+
Write text to the user's clipboard.

docs/hooks/use-const-fn.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# useConstFn
2+
3+
Memoize a function, which stays the same across rerenders.
4+
5+
```ts
6+
const fn = useConstFn(() => {
7+
doSomething();
8+
});
9+
```
10+
11+
## Parameters
12+
13+
### fn
14+
15+
- Type: `Function`
16+
17+
The function to memoize.
18+
19+
## Returns
20+
21+
- Type: `Function`
22+
23+
The memoized function.

docs/hooks/use-const.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# useConst
2+
3+
Memoize a value, which stays the same across rerenders.
4+
5+
```ts
6+
const value = useConst(() => expensiveCompute());
7+
```
8+
9+
## Parameters
10+
11+
### factory
12+
13+
- Type: `() => T`
14+
15+
A function that computes the value to be memoized.
16+
17+
## Returns
18+
19+
- Type: `T`
20+
21+
The memoized value.

0 commit comments

Comments
 (0)