@gild-ui/hooks 0.0.0-stage → 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +173 -2
- package/dist/index.cjs +15 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.mjs +7 -0
- package/dist/useControlled.cjs +13 -0
- package/dist/useControlled.d.cts +12 -0
- package/dist/useControlled.d.mts +12 -0
- package/dist/useControlled.mjs +12 -0
- package/dist/useCopyToClipboard.cjs +34 -0
- package/dist/useCopyToClipboard.d.cts +13 -0
- package/dist/useCopyToClipboard.d.mts +13 -0
- package/dist/useCopyToClipboard.mjs +32 -0
- package/dist/useDebounce.cjs +20 -0
- package/dist/useDebounce.d.cts +8 -0
- package/dist/useDebounce.d.mts +8 -0
- package/dist/useDebounce.mjs +18 -0
- package/dist/useDebouncedCallback.cjs +20 -0
- package/dist/useDebouncedCallback.d.cts +6 -0
- package/dist/useDebouncedCallback.d.mts +6 -0
- package/dist/useDebouncedCallback.mjs +19 -0
- package/dist/useEventCallback.cjs +18 -0
- package/dist/useEventCallback.d.cts +8 -0
- package/dist/useEventCallback.d.mts +8 -0
- package/dist/useEventCallback.mjs +17 -0
- package/dist/useMergedRefs.cjs +22 -0
- package/dist/useMergedRefs.d.cts +7 -0
- package/dist/useMergedRefs.d.mts +7 -0
- package/dist/useMergedRefs.mjs +21 -0
- package/package.json +134 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Masoud Soroush <https://soroush.tech>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,174 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @gild-ui/hooks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@gild-ui/hooks)
|
|
4
|
+
[](https://app.codecov.io/gh/soroush-tech/design-system?flags%5B0%5D=hooks)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
7
|
+
React hooks that belong to no component and no theme: a stable event callback, merged refs, a
|
|
8
|
+
value that is controlled or not, a debounced value or callback, and copy to the clipboard. They
|
|
9
|
+
are useful in an app with none of our components, and the components are built on them.
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm i @gild-ui/hooks
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Each hook has an entry point of its own, `@gild-ui/hooks/useDebounce`, so an app loads only
|
|
16
|
+
what it imports, and the root exports them all. The package depends on nothing of ours and has one peer, `react`.
|
|
17
|
+
|
|
18
|
+
Every hook below says whether it needs a browser. A hook that does not touches no DOM API, so it
|
|
19
|
+
renders on the server and can be shared with a React Native app; one that does reads a browser
|
|
20
|
+
API only when it is called, never when it is imported.
|
|
21
|
+
|
|
22
|
+
## useControlled
|
|
23
|
+
|
|
24
|
+
A value a control owns until its owner takes it over: the controlled value when there is one,
|
|
25
|
+
otherwise its own state. The setter keeps that state moving either way, so a control that stays
|
|
26
|
+
uncontrolled needs no branch. `undefined` leaves the control uncontrolled; `null` is a value.
|
|
27
|
+
|
|
28
|
+
Needs a browser: no.
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { useControlled } from '@gild-ui/hooks/useControlled'
|
|
32
|
+
|
|
33
|
+
function useControlled<T>(params: {
|
|
34
|
+
/** The controlled value; `undefined` leaves the control uncontrolled. */
|
|
35
|
+
controlled: T | undefined
|
|
36
|
+
/** The value at first, when uncontrolled. */
|
|
37
|
+
default: T
|
|
38
|
+
}): [value: T, setValue: (value: T) => void]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
function Counter({ value, defaultValue = 0, onChange }: CounterProps) {
|
|
43
|
+
const [count, setCount] = useControlled({ controlled: value, default: defaultValue })
|
|
44
|
+
return (
|
|
45
|
+
<button
|
|
46
|
+
onClick={() => {
|
|
47
|
+
setCount(count + 1)
|
|
48
|
+
onChange?.(count + 1)
|
|
49
|
+
}}
|
|
50
|
+
>
|
|
51
|
+
{count}
|
|
52
|
+
</button>
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## useCopyToClipboard
|
|
58
|
+
|
|
59
|
+
Copy text and show a confirmation: `copied` is `true` for `resetMs` after a successful copy, then
|
|
60
|
+
`false` again. Copying again restarts the window, and a reset still pending when the component
|
|
61
|
+
unmounts is cancelled.
|
|
62
|
+
|
|
63
|
+
Needs a browser: yes, the Clipboard API. It reads `navigator` only when `copy` is called, so
|
|
64
|
+
importing it and rendering on the server are safe, and `copy` does nothing where the API is
|
|
65
|
+
missing (an insecure page) or the write is rejected.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { useCopyToClipboard } from '@gild-ui/hooks/useCopyToClipboard'
|
|
69
|
+
|
|
70
|
+
function useCopyToClipboard(resetMs?: number): {
|
|
71
|
+
copied: boolean
|
|
72
|
+
copy: (text: string) => void
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`resetMs` defaults to 2000, exported as `COPIED_RESET_MS`.
|
|
77
|
+
|
|
78
|
+
```tsx
|
|
79
|
+
const { copied, copy } = useCopyToClipboard()
|
|
80
|
+
return <button onClick={() => copy(code)}>{copied ? 'Copied' : 'Copy'}</button>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## useDebounce
|
|
84
|
+
|
|
85
|
+
A value that follows the one you give it once that has stayed unchanged for `delayMs`. The first
|
|
86
|
+
render returns the value as it is, and a change during the delay restarts it. Use it to query on
|
|
87
|
+
the pause of a search box.
|
|
88
|
+
|
|
89
|
+
Needs a browser: no. It uses timers only, and renders on the server as the value it is given.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { useDebounce } from '@gild-ui/hooks/useDebounce'
|
|
93
|
+
|
|
94
|
+
function useDebounce<T>(value: T, delayMs?: number): T
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`delayMs` defaults to 500, exported as `DEBOUNCE_DELAY_MS`. The timer is cleared when the value
|
|
98
|
+
changes and when the component unmounts.
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
const [query, setQuery] = useState('')
|
|
102
|
+
const settled = useDebounce(query, 300)
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
search(settled)
|
|
105
|
+
}, [settled])
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## useDebouncedCallback
|
|
109
|
+
|
|
110
|
+
A function that calls your callback with the arguments of its last call, once `delayMs` has passed
|
|
111
|
+
with no other call. It always reaches the latest callback, its identity changes only with
|
|
112
|
+
`delayMs`, and a call still waiting when the component unmounts never runs. Use it where the
|
|
113
|
+
change is an event rather than a value: saving on the pause of typing.
|
|
114
|
+
|
|
115
|
+
Needs a browser: no. It uses timers only.
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { useDebouncedCallback } from '@gild-ui/hooks/useDebouncedCallback'
|
|
119
|
+
|
|
120
|
+
function useDebouncedCallback<Args extends unknown[]>(
|
|
121
|
+
callback: (...args: Args) => void,
|
|
122
|
+
delayMs?: number
|
|
123
|
+
): (...args: Args) => void
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
const save = useDebouncedCallback((text: string) => api.save(text), 800)
|
|
128
|
+
return <textarea onChange={(event) => save(event.target.value)} />
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## useEventCallback
|
|
132
|
+
|
|
133
|
+
A stable function identity that always calls the latest `fn`. An effect or a memoized child can
|
|
134
|
+
depend on it without running again when the handler changes. The latest `fn` is read from an
|
|
135
|
+
effect, so it is safe to render on the server; it suits handlers that a user triggers, which run
|
|
136
|
+
after the effect.
|
|
137
|
+
|
|
138
|
+
Needs a browser: no.
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { useEventCallback } from '@gild-ui/hooks/useEventCallback'
|
|
142
|
+
|
|
143
|
+
function useEventCallback<Args extends unknown[], Return>(
|
|
144
|
+
fn: (...args: Args) => Return
|
|
145
|
+
): (...args: Args) => Return
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
const onTick = useEventCallback(() => setCount(count + 1))
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
const id = setInterval(onTick, 1000)
|
|
152
|
+
return () => clearInterval(id)
|
|
153
|
+
}, [onTick])
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## useMergedRefs
|
|
157
|
+
|
|
158
|
+
Merges two refs into one stable ref callback, so one element can be tracked by an internal ref and
|
|
159
|
+
by a `ref` the caller passes. It returns `null` when both are absent, so React attaches nothing,
|
|
160
|
+
and keeps its identity while the refs are the same.
|
|
161
|
+
|
|
162
|
+
Needs a browser: no. It only combines refs; attaching them is React's.
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
import { useMergedRefs } from '@gild-ui/hooks/useMergedRefs'
|
|
166
|
+
|
|
167
|
+
function useMergedRefs<T>(refA: Ref<T> | undefined, refB: Ref<T> | undefined): RefCallback<T> | null
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
```tsx
|
|
171
|
+
const inner = useRef<HTMLInputElement>(null)
|
|
172
|
+
const ref = useMergedRefs(inner, forwardedRef)
|
|
173
|
+
return <input ref={ref} />
|
|
174
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_useControlled = require("./useControlled.cjs");
|
|
3
|
+
const require_useCopyToClipboard = require("./useCopyToClipboard.cjs");
|
|
4
|
+
const require_useDebounce = require("./useDebounce.cjs");
|
|
5
|
+
const require_useDebouncedCallback = require("./useDebouncedCallback.cjs");
|
|
6
|
+
const require_useEventCallback = require("./useEventCallback.cjs");
|
|
7
|
+
const require_useMergedRefs = require("./useMergedRefs.cjs");
|
|
8
|
+
exports.COPIED_RESET_MS = require_useCopyToClipboard.COPIED_RESET_MS;
|
|
9
|
+
exports.DEBOUNCE_DELAY_MS = require_useDebounce.DEBOUNCE_DELAY_MS;
|
|
10
|
+
exports.useControlled = require_useControlled.useControlled;
|
|
11
|
+
exports.useCopyToClipboard = require_useCopyToClipboard.useCopyToClipboard;
|
|
12
|
+
exports.useDebounce = require_useDebounce.useDebounce;
|
|
13
|
+
exports.useDebouncedCallback = require_useDebouncedCallback.useDebouncedCallback;
|
|
14
|
+
exports.useEventCallback = require_useEventCallback.useEventCallback;
|
|
15
|
+
exports.useMergedRefs = require_useMergedRefs.useMergedRefs;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { UseControlledParams, useControlled } from "./useControlled.cjs";
|
|
2
|
+
import { COPIED_RESET_MS, UseCopyToClipboardResult, useCopyToClipboard } from "./useCopyToClipboard.cjs";
|
|
3
|
+
import { DEBOUNCE_DELAY_MS, useDebounce } from "./useDebounce.cjs";
|
|
4
|
+
import { useDebouncedCallback } from "./useDebouncedCallback.cjs";
|
|
5
|
+
import { useEventCallback } from "./useEventCallback.cjs";
|
|
6
|
+
import { useMergedRefs } from "./useMergedRefs.cjs";
|
|
7
|
+
export { COPIED_RESET_MS, DEBOUNCE_DELAY_MS, UseControlledParams, UseCopyToClipboardResult, useControlled, useCopyToClipboard, useDebounce, useDebouncedCallback, useEventCallback, useMergedRefs };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { UseControlledParams, useControlled } from "./useControlled.mjs";
|
|
2
|
+
import { COPIED_RESET_MS, UseCopyToClipboardResult, useCopyToClipboard } from "./useCopyToClipboard.mjs";
|
|
3
|
+
import { DEBOUNCE_DELAY_MS, useDebounce } from "./useDebounce.mjs";
|
|
4
|
+
import { useDebouncedCallback } from "./useDebouncedCallback.mjs";
|
|
5
|
+
import { useEventCallback } from "./useEventCallback.mjs";
|
|
6
|
+
import { useMergedRefs } from "./useMergedRefs.mjs";
|
|
7
|
+
export { COPIED_RESET_MS, DEBOUNCE_DELAY_MS, UseControlledParams, UseCopyToClipboardResult, useControlled, useCopyToClipboard, useDebounce, useDebouncedCallback, useEventCallback, useMergedRefs };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { useControlled } from "./useControlled.mjs";
|
|
2
|
+
import { COPIED_RESET_MS, useCopyToClipboard } from "./useCopyToClipboard.mjs";
|
|
3
|
+
import { DEBOUNCE_DELAY_MS, useDebounce } from "./useDebounce.mjs";
|
|
4
|
+
import { useDebouncedCallback } from "./useDebouncedCallback.mjs";
|
|
5
|
+
import { useEventCallback } from "./useEventCallback.mjs";
|
|
6
|
+
import { useMergedRefs } from "./useMergedRefs.mjs";
|
|
7
|
+
export { COPIED_RESET_MS, DEBOUNCE_DELAY_MS, useControlled, useCopyToClipboard, useDebounce, useDebouncedCallback, useEventCallback, useMergedRefs };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react = require("react");
|
|
3
|
+
// src/useControlled.ts
|
|
4
|
+
/**
|
|
5
|
+
* A value a control owns until its owner takes it over: the controlled value when there is one,
|
|
6
|
+
* otherwise its own state. The setter keeps that state moving either way, so a control that
|
|
7
|
+
* stays uncontrolled needs no branch.
|
|
8
|
+
*/
|
|
9
|
+
function useControlled({ controlled, default: defaultValue }) {
|
|
10
|
+
const [state, setState] = (0, react.useState)(defaultValue);
|
|
11
|
+
return [controlled !== void 0 ? controlled : state, setState];
|
|
12
|
+
}
|
|
13
|
+
exports.useControlled = useControlled;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface UseControlledParams<T> {
|
|
2
|
+
/** The controlled value - `undefined` leaves the control uncontrolled (`null` is a value). */
|
|
3
|
+
controlled: T | undefined;
|
|
4
|
+
/** The value at first, when uncontrolled. */
|
|
5
|
+
default: T;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A value a control owns until its owner takes it over: the controlled value when there is one,
|
|
9
|
+
* otherwise its own state. The setter keeps that state moving either way, so a control that
|
|
10
|
+
* stays uncontrolled needs no branch.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useControlled<T>({ controlled, default: defaultValue }: UseControlledParams<T>): [T, (value: T) => void];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface UseControlledParams<T> {
|
|
2
|
+
/** The controlled value - `undefined` leaves the control uncontrolled (`null` is a value). */
|
|
3
|
+
controlled: T | undefined;
|
|
4
|
+
/** The value at first, when uncontrolled. */
|
|
5
|
+
default: T;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A value a control owns until its owner takes it over: the controlled value when there is one,
|
|
9
|
+
* otherwise its own state. The setter keeps that state moving either way, so a control that
|
|
10
|
+
* stays uncontrolled needs no branch.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useControlled<T>({ controlled, default: defaultValue }: UseControlledParams<T>): [T, (value: T) => void];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
// src/useControlled.ts
|
|
3
|
+
/**
|
|
4
|
+
* A value a control owns until its owner takes it over: the controlled value when there is one,
|
|
5
|
+
* otherwise its own state. The setter keeps that state moving either way, so a control that
|
|
6
|
+
* stays uncontrolled needs no branch.
|
|
7
|
+
*/
|
|
8
|
+
function useControlled({ controlled, default: defaultValue }) {
|
|
9
|
+
const [state, setState] = useState(defaultValue);
|
|
10
|
+
return [controlled !== void 0 ? controlled : state, setState];
|
|
11
|
+
}
|
|
12
|
+
export { useControlled };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react = require("react");
|
|
3
|
+
// src/useCopyToClipboard.ts
|
|
4
|
+
/** How long the "copied" confirmation stays true before reverting, in ms. */
|
|
5
|
+
const COPIED_RESET_MS = 2e3;
|
|
6
|
+
/**
|
|
7
|
+
* Copy-to-clipboard with a transient `copied` flag. Guards the unavailable
|
|
8
|
+
* clipboard API and rejected writes so the caller never throws or gets stuck.
|
|
9
|
+
*/
|
|
10
|
+
function useCopyToClipboard(resetMs = COPIED_RESET_MS) {
|
|
11
|
+
const [copied, setCopied] = (0, react.useState)(false);
|
|
12
|
+
const timerRef = (0, react.useRef)(null);
|
|
13
|
+
(0, react.useEffect)(() => () => {
|
|
14
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
15
|
+
}, []);
|
|
16
|
+
return {
|
|
17
|
+
copied,
|
|
18
|
+
copy: (0, react.useCallback)((text) => {
|
|
19
|
+
if (!navigator.clipboard?.writeText) return;
|
|
20
|
+
navigator.clipboard.writeText(text).then(() => {
|
|
21
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
22
|
+
setCopied(true);
|
|
23
|
+
timerRef.current = setTimeout(() => {
|
|
24
|
+
setCopied(false);
|
|
25
|
+
timerRef.current = null;
|
|
26
|
+
}, resetMs);
|
|
27
|
+
}).catch(() => {
|
|
28
|
+
console.error("Clipboard write failed; leave `copied` in its idle state.");
|
|
29
|
+
});
|
|
30
|
+
}, [resetMs])
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
exports.COPIED_RESET_MS = COPIED_RESET_MS;
|
|
34
|
+
exports.useCopyToClipboard = useCopyToClipboard;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** How long the "copied" confirmation stays true before reverting, in ms. */
|
|
2
|
+
export declare const COPIED_RESET_MS = 2000;
|
|
3
|
+
export interface UseCopyToClipboardResult {
|
|
4
|
+
/** `true` for `resetMs` after a successful copy, then back to `false`. */
|
|
5
|
+
copied: boolean;
|
|
6
|
+
/** Copy `text` to the clipboard; a no-op in insecure/unsupported contexts. */
|
|
7
|
+
copy: (text: string) => void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Copy-to-clipboard with a transient `copied` flag. Guards the unavailable
|
|
11
|
+
* clipboard API and rejected writes so the caller never throws or gets stuck.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useCopyToClipboard(resetMs?: number): UseCopyToClipboardResult;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** How long the "copied" confirmation stays true before reverting, in ms. */
|
|
2
|
+
export declare const COPIED_RESET_MS = 2000;
|
|
3
|
+
export interface UseCopyToClipboardResult {
|
|
4
|
+
/** `true` for `resetMs` after a successful copy, then back to `false`. */
|
|
5
|
+
copied: boolean;
|
|
6
|
+
/** Copy `text` to the clipboard; a no-op in insecure/unsupported contexts. */
|
|
7
|
+
copy: (text: string) => void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Copy-to-clipboard with a transient `copied` flag. Guards the unavailable
|
|
11
|
+
* clipboard API and rejected writes so the caller never throws or gets stuck.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useCopyToClipboard(resetMs?: number): UseCopyToClipboardResult;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
// src/useCopyToClipboard.ts
|
|
3
|
+
/** How long the "copied" confirmation stays true before reverting, in ms. */
|
|
4
|
+
const COPIED_RESET_MS = 2e3;
|
|
5
|
+
/**
|
|
6
|
+
* Copy-to-clipboard with a transient `copied` flag. Guards the unavailable
|
|
7
|
+
* clipboard API and rejected writes so the caller never throws or gets stuck.
|
|
8
|
+
*/
|
|
9
|
+
function useCopyToClipboard(resetMs = COPIED_RESET_MS) {
|
|
10
|
+
const [copied, setCopied] = useState(false);
|
|
11
|
+
const timerRef = useRef(null);
|
|
12
|
+
useEffect(() => () => {
|
|
13
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
14
|
+
}, []);
|
|
15
|
+
return {
|
|
16
|
+
copied,
|
|
17
|
+
copy: useCallback((text) => {
|
|
18
|
+
if (!navigator.clipboard?.writeText) return;
|
|
19
|
+
navigator.clipboard.writeText(text).then(() => {
|
|
20
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
21
|
+
setCopied(true);
|
|
22
|
+
timerRef.current = setTimeout(() => {
|
|
23
|
+
setCopied(false);
|
|
24
|
+
timerRef.current = null;
|
|
25
|
+
}, resetMs);
|
|
26
|
+
}).catch(() => {
|
|
27
|
+
console.error("Clipboard write failed; leave `copied` in its idle state.");
|
|
28
|
+
});
|
|
29
|
+
}, [resetMs])
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export { COPIED_RESET_MS, useCopyToClipboard };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react = require("react");
|
|
3
|
+
// src/useDebounce.ts
|
|
4
|
+
/** How long the value must stay unchanged before `useDebounce` returns it, in ms. */
|
|
5
|
+
const DEBOUNCE_DELAY_MS = 500;
|
|
6
|
+
/**
|
|
7
|
+
* A value that follows `value` once it has stayed unchanged for `delayMs`: a search box can
|
|
8
|
+
* type fast and query on the pause. The first render returns `value` as it is, and a change
|
|
9
|
+
* during the delay restarts it. Needs no browser: it renders on the server as `value`.
|
|
10
|
+
*/
|
|
11
|
+
function useDebounce(value, delayMs = 500) {
|
|
12
|
+
const [debounced, setDebounced] = (0, react.useState)(value);
|
|
13
|
+
(0, react.useEffect)(() => {
|
|
14
|
+
const timer = setTimeout(() => setDebounced(value), delayMs);
|
|
15
|
+
return () => clearTimeout(timer);
|
|
16
|
+
}, [value, delayMs]);
|
|
17
|
+
return debounced;
|
|
18
|
+
}
|
|
19
|
+
exports.DEBOUNCE_DELAY_MS = DEBOUNCE_DELAY_MS;
|
|
20
|
+
exports.useDebounce = useDebounce;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** How long the value must stay unchanged before `useDebounce` returns it, in ms. */
|
|
2
|
+
export declare const DEBOUNCE_DELAY_MS = 500;
|
|
3
|
+
/**
|
|
4
|
+
* A value that follows `value` once it has stayed unchanged for `delayMs`: a search box can
|
|
5
|
+
* type fast and query on the pause. The first render returns `value` as it is, and a change
|
|
6
|
+
* during the delay restarts it. Needs no browser: it renders on the server as `value`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useDebounce<T>(value: T, delayMs?: number): T;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** How long the value must stay unchanged before `useDebounce` returns it, in ms. */
|
|
2
|
+
export declare const DEBOUNCE_DELAY_MS = 500;
|
|
3
|
+
/**
|
|
4
|
+
* A value that follows `value` once it has stayed unchanged for `delayMs`: a search box can
|
|
5
|
+
* type fast and query on the pause. The first render returns `value` as it is, and a change
|
|
6
|
+
* during the delay restarts it. Needs no browser: it renders on the server as `value`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useDebounce<T>(value: T, delayMs?: number): T;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
// src/useDebounce.ts
|
|
3
|
+
/** How long the value must stay unchanged before `useDebounce` returns it, in ms. */
|
|
4
|
+
const DEBOUNCE_DELAY_MS = 500;
|
|
5
|
+
/**
|
|
6
|
+
* A value that follows `value` once it has stayed unchanged for `delayMs`: a search box can
|
|
7
|
+
* type fast and query on the pause. The first render returns `value` as it is, and a change
|
|
8
|
+
* during the delay restarts it. Needs no browser: it renders on the server as `value`.
|
|
9
|
+
*/
|
|
10
|
+
function useDebounce(value, delayMs = 500) {
|
|
11
|
+
const [debounced, setDebounced] = useState(value);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
const timer = setTimeout(() => setDebounced(value), delayMs);
|
|
14
|
+
return () => clearTimeout(timer);
|
|
15
|
+
}, [value, delayMs]);
|
|
16
|
+
return debounced;
|
|
17
|
+
}
|
|
18
|
+
export { DEBOUNCE_DELAY_MS, useDebounce };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
require("./useDebounce.cjs");
|
|
3
|
+
const require_useEventCallback = require("./useEventCallback.cjs");
|
|
4
|
+
let react = require("react");
|
|
5
|
+
// src/useDebouncedCallback.ts
|
|
6
|
+
/**
|
|
7
|
+
* A function that calls `callback` with the arguments of its last call, once `delayMs` has
|
|
8
|
+
* passed with no other call. Its identity changes only with `delayMs`, the call always reaches
|
|
9
|
+
* the latest `callback`, and a call still waiting when the component unmounts never runs.
|
|
10
|
+
*/
|
|
11
|
+
function useDebouncedCallback(callback, delayMs = 500) {
|
|
12
|
+
const latest = require_useEventCallback.useEventCallback(callback);
|
|
13
|
+
const timer = (0, react.useRef)(void 0);
|
|
14
|
+
(0, react.useEffect)(() => () => clearTimeout(timer.current), []);
|
|
15
|
+
return (0, react.useCallback)((...args) => {
|
|
16
|
+
clearTimeout(timer.current);
|
|
17
|
+
timer.current = setTimeout(() => latest(...args), delayMs);
|
|
18
|
+
}, [latest, delayMs]);
|
|
19
|
+
}
|
|
20
|
+
exports.useDebouncedCallback = useDebouncedCallback;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A function that calls `callback` with the arguments of its last call, once `delayMs` has
|
|
3
|
+
* passed with no other call. Its identity changes only with `delayMs`, the call always reaches
|
|
4
|
+
* the latest `callback`, and a call still waiting when the component unmounts never runs.
|
|
5
|
+
*/
|
|
6
|
+
export declare function useDebouncedCallback<Args extends unknown[]>(callback: (...args: Args) => void, delayMs?: number): (...args: Args) => void;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A function that calls `callback` with the arguments of its last call, once `delayMs` has
|
|
3
|
+
* passed with no other call. Its identity changes only with `delayMs`, the call always reaches
|
|
4
|
+
* the latest `callback`, and a call still waiting when the component unmounts never runs.
|
|
5
|
+
*/
|
|
6
|
+
export declare function useDebouncedCallback<Args extends unknown[]>(callback: (...args: Args) => void, delayMs?: number): (...args: Args) => void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import "./useDebounce.mjs";
|
|
2
|
+
import { useEventCallback } from "./useEventCallback.mjs";
|
|
3
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
4
|
+
// src/useDebouncedCallback.ts
|
|
5
|
+
/**
|
|
6
|
+
* A function that calls `callback` with the arguments of its last call, once `delayMs` has
|
|
7
|
+
* passed with no other call. Its identity changes only with `delayMs`, the call always reaches
|
|
8
|
+
* the latest `callback`, and a call still waiting when the component unmounts never runs.
|
|
9
|
+
*/
|
|
10
|
+
function useDebouncedCallback(callback, delayMs = 500) {
|
|
11
|
+
const latest = useEventCallback(callback);
|
|
12
|
+
const timer = useRef(void 0);
|
|
13
|
+
useEffect(() => () => clearTimeout(timer.current), []);
|
|
14
|
+
return useCallback((...args) => {
|
|
15
|
+
clearTimeout(timer.current);
|
|
16
|
+
timer.current = setTimeout(() => latest(...args), delayMs);
|
|
17
|
+
}, [latest, delayMs]);
|
|
18
|
+
}
|
|
19
|
+
export { useDebouncedCallback };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react = require("react");
|
|
3
|
+
// src/useEventCallback.ts
|
|
4
|
+
/**
|
|
5
|
+
* Returns a stable function identity that always calls the latest `fn`. Lets
|
|
6
|
+
* effects depend on the callback without re-running when the handler changes.
|
|
7
|
+
*
|
|
8
|
+
* Syncs the ref in `useEffect` (not `useLayoutEffect`) so it stays SSR-safe -
|
|
9
|
+
* the timing difference is irrelevant for the user-triggered handlers it wraps.
|
|
10
|
+
*/
|
|
11
|
+
function useEventCallback(fn) {
|
|
12
|
+
const ref = (0, react.useRef)(fn);
|
|
13
|
+
(0, react.useEffect)(() => {
|
|
14
|
+
ref.current = fn;
|
|
15
|
+
});
|
|
16
|
+
return (0, react.useCallback)((...args) => ref.current(...args), []);
|
|
17
|
+
}
|
|
18
|
+
exports.useEventCallback = useEventCallback;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a stable function identity that always calls the latest `fn`. Lets
|
|
3
|
+
* effects depend on the callback without re-running when the handler changes.
|
|
4
|
+
*
|
|
5
|
+
* Syncs the ref in `useEffect` (not `useLayoutEffect`) so it stays SSR-safe -
|
|
6
|
+
* the timing difference is irrelevant for the user-triggered handlers it wraps.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useEventCallback<Args extends unknown[], Return>(fn: (...args: Args) => Return): (...args: Args) => Return;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a stable function identity that always calls the latest `fn`. Lets
|
|
3
|
+
* effects depend on the callback without re-running when the handler changes.
|
|
4
|
+
*
|
|
5
|
+
* Syncs the ref in `useEffect` (not `useLayoutEffect`) so it stays SSR-safe -
|
|
6
|
+
* the timing difference is irrelevant for the user-triggered handlers it wraps.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useEventCallback<Args extends unknown[], Return>(fn: (...args: Args) => Return): (...args: Args) => Return;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
2
|
+
// src/useEventCallback.ts
|
|
3
|
+
/**
|
|
4
|
+
* Returns a stable function identity that always calls the latest `fn`. Lets
|
|
5
|
+
* effects depend on the callback without re-running when the handler changes.
|
|
6
|
+
*
|
|
7
|
+
* Syncs the ref in `useEffect` (not `useLayoutEffect`) so it stays SSR-safe -
|
|
8
|
+
* the timing difference is irrelevant for the user-triggered handlers it wraps.
|
|
9
|
+
*/
|
|
10
|
+
function useEventCallback(fn) {
|
|
11
|
+
const ref = useRef(fn);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
ref.current = fn;
|
|
14
|
+
});
|
|
15
|
+
return useCallback((...args) => ref.current(...args), []);
|
|
16
|
+
}
|
|
17
|
+
export { useEventCallback };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react = require("react");
|
|
3
|
+
// src/useMergedRefs.ts
|
|
4
|
+
function assignRef(ref, value) {
|
|
5
|
+
if (typeof ref === "function") ref(value);
|
|
6
|
+
else if (ref) ref.current = value;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Merges two refs into a single stable ref callback so one DOM node can be
|
|
10
|
+
* tracked by both an internal ref and a forwarded `ref`. Returns `null` when
|
|
11
|
+
* both refs are absent, so React skips attaching a callback ref entirely.
|
|
12
|
+
*/
|
|
13
|
+
function useMergedRefs(refA, refB) {
|
|
14
|
+
return (0, react.useMemo)(() => {
|
|
15
|
+
if (refA == null && refB == null) return null;
|
|
16
|
+
return (value) => {
|
|
17
|
+
assignRef(refA, value);
|
|
18
|
+
assignRef(refB, value);
|
|
19
|
+
};
|
|
20
|
+
}, [refA, refB]);
|
|
21
|
+
}
|
|
22
|
+
exports.useMergedRefs = useMergedRefs;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Ref, RefCallback } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Merges two refs into a single stable ref callback so one DOM node can be
|
|
4
|
+
* tracked by both an internal ref and a forwarded `ref`. Returns `null` when
|
|
5
|
+
* both refs are absent, so React skips attaching a callback ref entirely.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useMergedRefs<T>(refA: Ref<T> | undefined, refB: Ref<T> | undefined): RefCallback<T> | null;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Ref, RefCallback } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Merges two refs into a single stable ref callback so one DOM node can be
|
|
4
|
+
* tracked by both an internal ref and a forwarded `ref`. Returns `null` when
|
|
5
|
+
* both refs are absent, so React skips attaching a callback ref entirely.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useMergedRefs<T>(refA: Ref<T> | undefined, refB: Ref<T> | undefined): RefCallback<T> | null;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
// src/useMergedRefs.ts
|
|
3
|
+
function assignRef(ref, value) {
|
|
4
|
+
if (typeof ref === "function") ref(value);
|
|
5
|
+
else if (ref) ref.current = value;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Merges two refs into a single stable ref callback so one DOM node can be
|
|
9
|
+
* tracked by both an internal ref and a forwarded `ref`. Returns `null` when
|
|
10
|
+
* both refs are absent, so React skips attaching a callback ref entirely.
|
|
11
|
+
*/
|
|
12
|
+
function useMergedRefs(refA, refB) {
|
|
13
|
+
return useMemo(() => {
|
|
14
|
+
if (refA == null && refB == null) return null;
|
|
15
|
+
return (value) => {
|
|
16
|
+
assignRef(refA, value);
|
|
17
|
+
assignRef(refB, value);
|
|
18
|
+
};
|
|
19
|
+
}, [refA, refB]);
|
|
20
|
+
}
|
|
21
|
+
export { useMergedRefs };
|
package/package.json
CHANGED
|
@@ -1,6 +1,137 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gild-ui/hooks",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "The React hooks of the Gild UI family that belong to no component and no theme - copy to the clipboard, a stable event callback, merged refs, a controlled-or-not value, a debounced value.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"hooks",
|
|
8
|
+
"design-system",
|
|
9
|
+
"debounce",
|
|
10
|
+
"clipboard",
|
|
11
|
+
"typescript"
|
|
12
|
+
],
|
|
13
|
+
"author": "Masoud Soroush <masoud@soroush.tech>",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"homepage": "https://gild-ui.soroush.design/react/hooks/",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/soroush-tech/gild-ui.git",
|
|
19
|
+
"directory": "packages/hooks"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/soroush-tech/gild-ui/issues"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./dist/index.d.mts",
|
|
33
|
+
"default": "./dist/index.mjs"
|
|
34
|
+
},
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./dist/index.d.cts",
|
|
37
|
+
"default": "./dist/index.cjs"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"./useControlled": {
|
|
41
|
+
"import": {
|
|
42
|
+
"types": "./dist/useControlled.d.mts",
|
|
43
|
+
"default": "./dist/useControlled.mjs"
|
|
44
|
+
},
|
|
45
|
+
"require": {
|
|
46
|
+
"types": "./dist/useControlled.d.cts",
|
|
47
|
+
"default": "./dist/useControlled.cjs"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"./useCopyToClipboard": {
|
|
51
|
+
"import": {
|
|
52
|
+
"types": "./dist/useCopyToClipboard.d.mts",
|
|
53
|
+
"default": "./dist/useCopyToClipboard.mjs"
|
|
54
|
+
},
|
|
55
|
+
"require": {
|
|
56
|
+
"types": "./dist/useCopyToClipboard.d.cts",
|
|
57
|
+
"default": "./dist/useCopyToClipboard.cjs"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"./useDebounce": {
|
|
61
|
+
"import": {
|
|
62
|
+
"types": "./dist/useDebounce.d.mts",
|
|
63
|
+
"default": "./dist/useDebounce.mjs"
|
|
64
|
+
},
|
|
65
|
+
"require": {
|
|
66
|
+
"types": "./dist/useDebounce.d.cts",
|
|
67
|
+
"default": "./dist/useDebounce.cjs"
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"./useDebouncedCallback": {
|
|
71
|
+
"import": {
|
|
72
|
+
"types": "./dist/useDebouncedCallback.d.mts",
|
|
73
|
+
"default": "./dist/useDebouncedCallback.mjs"
|
|
74
|
+
},
|
|
75
|
+
"require": {
|
|
76
|
+
"types": "./dist/useDebouncedCallback.d.cts",
|
|
77
|
+
"default": "./dist/useDebouncedCallback.cjs"
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
"./useEventCallback": {
|
|
81
|
+
"import": {
|
|
82
|
+
"types": "./dist/useEventCallback.d.mts",
|
|
83
|
+
"default": "./dist/useEventCallback.mjs"
|
|
84
|
+
},
|
|
85
|
+
"require": {
|
|
86
|
+
"types": "./dist/useEventCallback.d.cts",
|
|
87
|
+
"default": "./dist/useEventCallback.cjs"
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"./useMergedRefs": {
|
|
91
|
+
"import": {
|
|
92
|
+
"types": "./dist/useMergedRefs.d.mts",
|
|
93
|
+
"default": "./dist/useMergedRefs.mjs"
|
|
94
|
+
},
|
|
95
|
+
"require": {
|
|
96
|
+
"types": "./dist/useMergedRefs.d.cts",
|
|
97
|
+
"default": "./dist/useMergedRefs.cjs"
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
"types": "./dist/index.d.mts",
|
|
102
|
+
"publishConfig": {
|
|
103
|
+
"access": "public"
|
|
104
|
+
},
|
|
105
|
+
"peerDependencies": {
|
|
106
|
+
"react": "^19.2.8"
|
|
107
|
+
},
|
|
108
|
+
"devDependencies": {
|
|
109
|
+
"@testing-library/react": "^16.3.3",
|
|
110
|
+
"@types/react": "^19.3.0",
|
|
111
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
112
|
+
"jsdom": "^31.0.0",
|
|
113
|
+
"oxlint": "^1.93.0",
|
|
114
|
+
"react": "^19.3.1",
|
|
115
|
+
"react-dom": "^19.3.1",
|
|
116
|
+
"tsdown": "^0.23.7",
|
|
117
|
+
"typescript": "~7.0.2",
|
|
118
|
+
"vitest": "^5.1.0",
|
|
119
|
+
"@soroush.tech/eslint-config": "0.0.0"
|
|
120
|
+
},
|
|
121
|
+
"scripts": {
|
|
122
|
+
"build": "tsdown",
|
|
123
|
+
"lint": "oxlint --deny-warnings",
|
|
124
|
+
"typecheck": "tsc --noEmit",
|
|
125
|
+
"test": "vitest run",
|
|
126
|
+
"test:coverage": "vitest run --coverage"
|
|
127
|
+
},
|
|
128
|
+
"main": "./dist/index.cjs",
|
|
129
|
+
"module": "./dist/index.mjs",
|
|
130
|
+
"typesVersions": {
|
|
131
|
+
"*": {
|
|
132
|
+
"*": [
|
|
133
|
+
"./dist/*.d.cts"
|
|
134
|
+
]
|
|
135
|
+
}
|
|
136
|
+
}
|
|
6
137
|
}
|