@proedis/react 1.5.2 → 2.0.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/README.md +188 -0
- package/cjs/components/RenderWhen/RenderWhen.js +1 -1
- package/cjs/components/RenderWhen/RenderWhen.js.map +1 -1
- package/cjs/hooks/useAutoControlledState.js.map +1 -1
- package/cjs/hooks/useClickOutside.js +7 -0
- package/cjs/hooks/useClickOutside.js.map +1 -1
- package/cjs/hooks/useDataManager.js.map +1 -1
- package/cjs/hooks/useDataSelector.js +53 -19
- package/cjs/hooks/useDataSelector.js.map +1 -1
- package/cjs/hooks/useDebouncedCallback.js.map +1 -1
- package/cjs/hooks/useElementType.js.map +1 -1
- package/cjs/hooks/useEnhancedEffect.js.map +1 -1
- package/cjs/hooks/useEvent.js.map +1 -1
- package/cjs/hooks/useForkRef.js +37 -9
- package/cjs/hooks/useForkRef.js.map +1 -1
- package/cjs/hooks/useInputValue.js.map +1 -1
- package/cjs/hooks/useObjectState.js.map +1 -1
- package/cjs/hooks/useSafeState.js.map +1 -1
- package/cjs/hooks/useSyncedRef.js +37 -5
- package/cjs/hooks/useSyncedRef.js.map +1 -1
- package/cjs/hooks/useUnmountEffect.js.map +1 -1
- package/cjs/hooks/useWindowSize.js.map +1 -1
- package/cjs/package.json +3 -0
- package/cjs/utils/compose.js.map +1 -1
- package/cjs/utils/contextBuilder.js.map +1 -1
- package/cjs/utils/shorthands.js +5 -6
- package/cjs/utils/shorthands.js.map +1 -1
- package/esm/components/RenderWhen/RenderWhen.js +1 -1
- package/esm/components/RenderWhen/RenderWhen.js.map +1 -1
- package/esm/hooks/useAutoControlledState.js.map +1 -1
- package/esm/hooks/useClickOutside.js +7 -0
- package/esm/hooks/useClickOutside.js.map +1 -1
- package/esm/hooks/useDataManager.js.map +1 -1
- package/esm/hooks/useDataSelector.js +53 -19
- package/esm/hooks/useDataSelector.js.map +1 -1
- package/esm/hooks/useDebouncedCallback.js.map +1 -1
- package/esm/hooks/useElementType.js.map +1 -1
- package/esm/hooks/useEnhancedEffect.js.map +1 -1
- package/esm/hooks/useEvent.js.map +1 -1
- package/esm/hooks/useForkRef.js +37 -9
- package/esm/hooks/useForkRef.js.map +1 -1
- package/esm/hooks/useInputValue.js.map +1 -1
- package/esm/hooks/useObjectState.js.map +1 -1
- package/esm/hooks/useSafeState.js.map +1 -1
- package/esm/hooks/useSyncedRef.js +37 -5
- package/esm/hooks/useSyncedRef.js.map +1 -1
- package/esm/hooks/useUnmountEffect.js.map +1 -1
- package/esm/hooks/useWindowSize.js.map +1 -1
- package/esm/package.json +3 -0
- package/esm/utils/compose.js.map +1 -1
- package/esm/utils/contextBuilder.js.map +1 -1
- package/esm/utils/shorthands.js +5 -6
- package/esm/utils/shorthands.js.map +1 -1
- package/package.json +13 -3
- package/types/hooks/useClickOutside.d.ts +10 -2
- package/types/hooks/useForkRef.d.ts +31 -4
- package/types/hooks/useSyncedRef.d.ts +36 -5
- package/tsconfig.declaration.tsbuildinfo +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# `@proedis/react`
|
|
4
|
+
|
|
5
|
+
**The framework-agnostic half of a Proedis React app: typed contexts, shorthand props, and the
|
|
6
|
+
twenty hooks you would otherwise rewrite.** ⚛️
|
|
7
|
+
|
|
8
|
+
[](https://www.npmjs.com/package/@proedis/react)
|
|
9
|
+
[](https://github.com/proedis/Npm.Suite/blob/master/LICENSE)
|
|
10
|
+
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## ✨ What's in the box
|
|
16
|
+
|
|
17
|
+
No UI kit, no styling, no opinions about how anything looks. 🎨
|
|
18
|
+
|
|
19
|
+
| | |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| **Contexts** | `contextBuilder` — a context and its hook, with a real error instead of `undefined` |
|
|
22
|
+
| **Component props** | `UIComponentProps` / `UIVoidComponentProps`, the shared prop surface every component starts from |
|
|
23
|
+
| **Shorthands** | `ShorthandItem`, `createShorthandFactory`, `creatableComponent` — accept props *or* a node for the same slot |
|
|
24
|
+
| **Components** | `RenderWhen`, `compose` |
|
|
25
|
+
| **State hooks** | `useAutoControlledState`, `useObjectState`, `useSafeState`, `useInputValue`, `useDataManager`, `useDataSelector` |
|
|
26
|
+
| **Ref hooks** | `useSyncedRef`, `useForkRef` |
|
|
27
|
+
| **Effect hooks** | `useEnhancedEffect`, `useUnmountEffect`, `useDebouncedCallback` |
|
|
28
|
+
| **DOM hooks** | `useEvent`, `useClickOutside`, `useWindowSize`, `useElementType` |
|
|
29
|
+
|
|
30
|
+
## 📦 Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
yarn add @proedis/react
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`react` is a peer, accepted from **18 up to 19** — a range verified by compiling the emitted
|
|
37
|
+
declarations against `@types/react` 18 and 19 with `skipLibCheck` off, not by hoping.
|
|
38
|
+
|
|
39
|
+
## 🚀 Quick start
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { contextBuilder, RenderWhen, useSyncedRef } from '@proedis/react';
|
|
43
|
+
|
|
44
|
+
/** A context that fails loudly when consumed outside its provider */
|
|
45
|
+
const [ TicketContext, useTicketContext ] = contextBuilder<TicketContextValue>('Ticket');
|
|
46
|
+
|
|
47
|
+
function TicketPanel() {
|
|
48
|
+
const { ticket } = useTicketContext();
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<RenderWhen
|
|
52
|
+
condition={ticket.isOpen}
|
|
53
|
+
isTrue={<TicketBody ticket={ticket} />}
|
|
54
|
+
isFalse={<ClosedBanner />}
|
|
55
|
+
/>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## 📖 API
|
|
61
|
+
|
|
62
|
+
### 🏗️ `contextBuilder(name, initialContext?)`
|
|
63
|
+
|
|
64
|
+
Returns a `[ Context, useContext ]` pair. The hook throws a named error when it runs outside its
|
|
65
|
+
provider, so a missing provider is a message that tells you which context is missing — instead of an
|
|
66
|
+
`undefined` that explodes three components later.
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
const [ AuthContext, useAuthContext ] = contextBuilder<AuthContextValue>('Auth');
|
|
70
|
+
|
|
71
|
+
<AuthContext.Provider value={value}>…</AuthContext.Provider>
|
|
72
|
+
|
|
73
|
+
const { user } = useAuthContext(); // throws outside the provider, with the name in the message
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 🎛️ `UIComponentProps` and `UIVoidComponentProps`
|
|
77
|
+
|
|
78
|
+
The prop surface every Proedis component starts from — `className`, `style`, the element type
|
|
79
|
+
override, and children for the non-void one. Compose your own strict props on top:
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
interface StrictButtonProps {
|
|
83
|
+
color?: ButtonColor;
|
|
84
|
+
loading?: boolean;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
type ButtonProps = UIComponentProps<StrictButtonProps>;
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The second parameter is there for a component wrapping another one's props:
|
|
91
|
+
`UIComponentProps<StrictProps, BaseProps>` keeps your strict props authoritative and inherits the rest.
|
|
92
|
+
|
|
93
|
+
### 🎁 Shorthands
|
|
94
|
+
|
|
95
|
+
A shorthand slot accepts **either** a ready node **or** the props to build one — which is what lets a
|
|
96
|
+
component expose `icon={'user'}` and `icon={<CustomIcon />}` through a single prop.
|
|
97
|
+
|
|
98
|
+
| Symbol | What it is |
|
|
99
|
+
| --- | --- |
|
|
100
|
+
| `ShorthandItem<Props>` | a node, or the props of the component filling the slot |
|
|
101
|
+
| `ShorthandCollection<Props>` | an array of them, each carrying a `key` |
|
|
102
|
+
| `ShorthandContent` | plain renderable content |
|
|
103
|
+
| `createShorthandFactory(Component, mapValueToProps, computeKey?)` | build the factory that turns a value into an element |
|
|
104
|
+
| `creatableComponent(Component, …)` | attach a `create` method to a component |
|
|
105
|
+
| `renderShorthandContent({ children, content })` | pick between `children` and a `content` prop, in that order |
|
|
106
|
+
|
|
107
|
+
### 🔀 `RenderWhen` and `compose`
|
|
108
|
+
|
|
109
|
+
`RenderWhen` takes a `condition` and then **one of two shapes**, and the type will not let you mix
|
|
110
|
+
them: either children with an optional boolean `isFalse` inversion, or an `isTrue` / `isFalse` pair of
|
|
111
|
+
nodes and no children at all.
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
/** children form — render them when the condition holds */
|
|
115
|
+
<RenderWhen condition={isAdmin}>
|
|
116
|
+
<AdminPanel />
|
|
117
|
+
</RenderWhen>
|
|
118
|
+
|
|
119
|
+
/** children form, inverted — 'isFalse' is a boolean here */
|
|
120
|
+
<RenderWhen condition={isAdmin} isFalse>
|
|
121
|
+
<RequestAccess />
|
|
122
|
+
</RenderWhen>
|
|
123
|
+
|
|
124
|
+
/** ternary form — no children, both branches as nodes */
|
|
125
|
+
<RenderWhen condition={isAdmin} isTrue={<AdminPanel />} isFalse={<RequestAccess />} />
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
⚠️ `isFalse` means two different things across the two shapes: a boolean inversion with children, a
|
|
129
|
+
node without them. Passing children *and* a node is a compile error, which is the point.
|
|
130
|
+
|
|
131
|
+
`compose(...components)` nests a list of providers into one component, which turns a stack of eight
|
|
132
|
+
providers at the root of an app into a single element.
|
|
133
|
+
|
|
134
|
+
### 🪝 The hooks
|
|
135
|
+
|
|
136
|
+
| Hook | What it gives you |
|
|
137
|
+
| --- | --- |
|
|
138
|
+
| `useSyncedRef(value)` | a **stable** container whose `current` always reads the latest value — the way to let an effect or a handler see fresh data without being rebuilt |
|
|
139
|
+
| `useForkRef(...refs)` | one ref callback assigning to several refs, for a component that both exposes a ref and keeps one |
|
|
140
|
+
| `useEnhancedEffect` | `useLayoutEffect` in a browser, `useEffect` everywhere else, so SSR stays quiet |
|
|
141
|
+
| `useUnmountEffect(callback)` | run something once, on unmount, always the latest version of it |
|
|
142
|
+
| `useSafeState(initial)` | `useState` whose setter is a no-op after unmount |
|
|
143
|
+
| `useAutoControlledState(…)` | one state that works controlled *or* uncontrolled, the standard prop/defaultProp pattern |
|
|
144
|
+
| `useObjectState(values, options?)` | object state with per-key setters and an `onChange` notification |
|
|
145
|
+
| `useInputValue(initial?)` | value plus change handler for an uncontrolled-ish input |
|
|
146
|
+
| `useDebouncedCallback(fn, delay)` | a debounced callback that always calls the newest `fn` |
|
|
147
|
+
| `useDataManager()` | add / update / remove over a local collection |
|
|
148
|
+
| `useDataSelector(data, options?)` | a selection that is **always** backed by the current data |
|
|
149
|
+
| `useEvent(target, type, handler)` | a DOM listener attached once, calling the newest handler |
|
|
150
|
+
| `useClickOutside(target, callback)` | the outside-click primitive behind every dropdown |
|
|
151
|
+
| `useWindowSize(options?)` | viewport size, debounced |
|
|
152
|
+
| `useElementType(props, default)` | resolve the `as` prop into the element to render |
|
|
153
|
+
|
|
154
|
+
💡 Several of these are built on `useSyncedRef`, and that is the pattern worth internalising: keep the
|
|
155
|
+
*subscription* stable and let it read a moving value, instead of tearing it down whenever the value
|
|
156
|
+
changes.
|
|
157
|
+
|
|
158
|
+
⚠️ Read a `useSyncedRef` container from an effect, a handler or a cleanup — **not while rendering**. The
|
|
159
|
+
value is written during render, which is what makes it current for the commit it belongs to, and a
|
|
160
|
+
render that gets discarded leaves the container holding something that was never committed.
|
|
161
|
+
|
|
162
|
+
## 🔀 Migrating to 2.x
|
|
163
|
+
|
|
164
|
+
| Change | What to do |
|
|
165
|
+
| --- | --- |
|
|
166
|
+
| `useDataSelector` reconciles during render | Nothing, in most cases — and one render fewer per data change. The selection was previously corrected by an effect calling `setState`, so a render could show a selection the data no longer backed. |
|
|
167
|
+
| `onSelectedChange` fires once per real transition | Check any listener that counted calls. It used to fire from two places — synchronously inside the setter, and again from the reconciliation effect — so it also reported things that were not changes: selecting what was already selected, clearing an already empty selection (twice), and resolving `defaultSelected` against the data at mount. It now fires after the commit, once, and only when the resolved selection actually changed. The notification on unmount is unchanged. |
|
|
168
|
+
| No notification for the initial selection | If you relied on being told at mount what `defaultSelected` resolved to, read `selected` instead. The first value is not a transition, so nothing is reported — where before a `defaultSelected` that was not identity-equal to its item in the data produced a notification and an extra render. |
|
|
169
|
+
| A selection restores itself when its item comes back | Nothing to do, but know the shape: the hook stores what you *asked* for and resolves it against the current data on every render. An item that leaves the data and later returns — a filter applied and removed — becomes selected again, and that is reported. It used to store the resolved item and drop it for good. |
|
|
170
|
+
| `useForkRef` parameter type | Now a minimal structural `{ current: T \| null \| undefined }` rather than `React.MutableRefObject`. That is what keeps the same signature valid across `@types/react` 18 and 19; anything that was passing a real ref keeps working. |
|
|
171
|
+
| `RenderWhen` condition grouping | Its condition is now explicitly parenthesised. Same behaviour, but note the shape it always had: with the `isFalse` **inversion**, children render when the condition is false *even if there are no children* to render. |
|
|
172
|
+
|
|
173
|
+
## 🤝 Compatibility
|
|
174
|
+
|
|
175
|
+
| Requirement | Range |
|
|
176
|
+
| --- | --- |
|
|
177
|
+
| `react` | `>=18.0.0 <20.0.0` (peer) |
|
|
178
|
+
| `typescript` | `>=5.2.0` |
|
|
179
|
+
| Runtime | ES2022 — roughly Safari 16.4 / Chrome 94 |
|
|
180
|
+
|
|
181
|
+
Public signatures deliberately avoid `React.RefObject`, `React.MutableRefObject` and
|
|
182
|
+
`ReturnType<typeof React.useRef<T>>`: those aliases changed meaning between React 18 and 19, and are
|
|
183
|
+
the reason a hook can typecheck under one major and be rejected under the other. Minimal structural
|
|
184
|
+
shapes are used instead.
|
|
185
|
+
|
|
186
|
+
## 📄 License
|
|
187
|
+
|
|
188
|
+
MIT © [Proedis S.r.l.](https://proedis.net)
|
|
@@ -35,7 +35,7 @@ const RenderWhen = (props) => {
|
|
|
35
35
|
/** Get props from StrictConditionNode type */
|
|
36
36
|
const { children, isFalse: invertConditionLogic } = props;
|
|
37
37
|
/** Check if the node must render or not */
|
|
38
|
-
if (children &&
|
|
38
|
+
if ((children && !invertConditionLogic && condition) || (invertConditionLogic && !condition)) {
|
|
39
39
|
return (jsxRuntime.jsx(React__namespace.Fragment, { children: children }));
|
|
40
40
|
}
|
|
41
41
|
/** Return Empty */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RenderWhen.js","sources":["../../../../src/components/RenderWhen/RenderWhen.tsx"],"sourcesContent":[null],"names":["_jsx","React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA;;AAEc;AACd,MAAM,UAAU,GAA6C,CAAC,KAAK,KAAI;;;;AAKrE,IAAA,MAAM,EACJ,SAAS,EACV,GAAG,KAAK
|
|
1
|
+
{"version":3,"file":"RenderWhen.js","sources":["../../../../src/components/RenderWhen/RenderWhen.tsx"],"sourcesContent":[null],"names":["_jsx","React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA;;AAEc;AACd,MAAM,UAAU,GAA6C,CAAC,KAAK,KAAI;;;;AAKrE,IAAA,MAAM,EACJ,SAAS,EACV,GAAG,KAAK;;AAIT,IAAA,IAAI,UAAU,IAAI,KAAK,EAAE;;QAEvB,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,oBAAoB,EAAE,GAAG,KAAK;;AAGzD,QAAA,IAAI,CAAC,QAAQ,IAAI,CAAC,oBAAoB,IAAI,SAAS,MAAM,oBAAoB,IAAI,CAAC,SAAS,CAAC,EAAE;YAC5F,QACEA,eAACC,gBAAK,CAAC,QAAQ,EAAA,EAAA,QAAA,EACZ,QAAQ,EAAA,CACM;QAErB;;AAGA,QAAA,OAAO,IAAI;IACb;;IAIA,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,eAAe,EAAE,GAAG,KAAK;;AAIpE,IAAA,QACED,cAAA,CAACC,gBAAK,CAAC,QAAQ,cACZ,SAAS,GAAG,eAAe,GAAG,gBAAgB,EAAA,CAChC;AAGrB;AAEA,UAAU,CAAC,WAAW,GAAG,YAAY;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAutoControlledState.js","sources":["../../../src/hooks/useAutoControlledState.ts"],"sourcesContent":[null],"names":["React","useEnhancedEffect","useSafeState"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA+BA;;AAEc;AAEd;;;;;;;;AAQG;
|
|
1
|
+
{"version":3,"file":"useAutoControlledState.js","sources":["../../../src/hooks/useAutoControlledState.ts"],"sourcesContent":[null],"names":["React","useEnhancedEffect","useSafeState"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA+BA;;AAEc;AAEd;;;;;;;;AAQG;AACG,SAAU,sBAAsB,CACpC,YAA2B,EAC3B,OAA0C,EAAA;AAI1C;;;;AAIG;IACH,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,OAAO,IAAI,EAAE;AAG7C;;;;;AAKG;IACH,MAAM,gBAAgB,GAAGA,gBAAK,CAAC,MAAM,CAAU,KAAK,KAAK,SAAS,CAAC;AAGnE;;;AAGG;IACHC,mCAAiB,CACf,MAAK;AACH,QAAA,gBAAgB,CAAC,OAAO,GAAG,KAAK,KAAK,SAAS;AAChD,IAAA,CAAC,EACD,CAAE,KAAK,CAAE,CACV;AAGD;;;;;;AAMG;IACH,MAAM,CAAE,KAAK,EAAE,QAAQ,CAAE,GAAGC,yBAAY,CACtC,MAAK;;AAEH,QAAA,MAAM,WAAW,GAAG,KAAK,KAAK,SAAS,GAAG,YAAY,GAAG,SAAS;;AAGlE,QAAA,IAAI,WAAW,KAAK,SAAS,EAAE;AAC7B,YAAA,OAAO,WAAW;QACpB;;QAGA,OAAO,OAAO,YAAY,KAAK;cAC1B,YAAwB;cACzB,YAAY;AAClB,IAAA,CAAC,CACF;AAGD;;;;AAIG;IACH,MAAM,WAAW,GAA4CF,gBAAK,CAAC,WAAW,CAC5E,CAAC,QAAQ,KAAI;;AAEX,QAAA,IAAI,gBAAgB,CAAC,OAAO,EAAE;YAC5B,QAAQ,CAAC,QAAQ,CAAC;QACpB;AACF,IAAA,CAAC,EACD,CAAE,QAAQ,CAAE,CACb;;IAID,OAAO;QACL,KAAK,KAAK,SAAS,GAAG,KAAK,GAAG,KAAK;QACnC,WAAW;QACX;KACQ;AAEZ;;;;"}
|
|
@@ -11,6 +11,13 @@ const DEFAULT_EVENTS = [
|
|
|
11
11
|
/**
|
|
12
12
|
* Use this hook to fire an event every time a click occurs outside
|
|
13
13
|
* the provided target element
|
|
14
|
+
*
|
|
15
|
+
* The target is typed as the minimal structural shape the hook actually reads instead of
|
|
16
|
+
* 'React.RefObject<T> | React.MutableRefObject<T>'. Those aliases changed meaning between
|
|
17
|
+
* @types/react 18 and 19: in 19 'useRef<T>(null)' yields 'RefObject<T | null>', which no
|
|
18
|
+
* longer satisfies 'RefObject<T>' and made every call site fail to compile. The
|
|
19
|
+
* implementation already null-checks 'current', so admitting it in the type costs nothing.
|
|
20
|
+
*
|
|
14
21
|
* @param target
|
|
15
22
|
* @param callback
|
|
16
23
|
* @param events
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useClickOutside.js","sources":["../../../src/hooks/useClickOutside.ts"],"sourcesContent":[null],"names":["useSyncedRef","useEnhancedEffect"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"useClickOutside.js","sources":["../../../src/hooks/useClickOutside.ts"],"sourcesContent":[null],"names":["useSyncedRef","useEnhancedEffect"],"mappings":";;;;;AAIA;AACA,MAAM,cAAc,GAA6C;IAC/D,WAAW;IACX;CACD;AAGD;;;;;;;;;;;;;AAaG;AACG,SAAU,eAAe,CAC7B,MAAkD,EAClD,QAAuB,EACvB,SAAmD,cAAc,EAAA;;AAIjE,IAAA,MAAM,aAAa,GAAGA,yBAAY,CAAC,MAAM,CAAC;AAC1C,IAAA,MAAM,gBAAgB,GAAGA,yBAAY,CAAC,QAAQ,CAAC;;IAG/CC,mCAAiB,CACf,MAAK;AAEH;;;;;;AAMG;QACH,SAAS,OAAO,CAAoB,KAAY,EAAA;;AAE9C,YAAA,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,OAAO,EAAE;gBAClC;YACF;;AAGA,YAAA,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,KAAK;AACrC,YAAA,MAAM,EAAE,GAAG,gBAAgB,CAAC,OAAO;AAEnC,YAAA,IACE,CAAC;AACE,oBAAC,CAAC,CAAC,WAAW,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,WAAmB,CAAC,CAAC,EAAE;;AAEpF,gBAAA,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC;YACtB;QACF;;AAGA,QAAA,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,KAAI;;AAE3B,YAAA,QAAQ,CAAC,gBAAgB,CAAC,SAAS,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AAClE,QAAA,CAAC,CAAC;;AAGF,QAAA,OAAO,MAAK;AACV,YAAA,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,KAAI;;AAE3B,gBAAA,QAAQ,CAAC,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC;AAClD,YAAA,CAAC,CAAC;AACJ,QAAA,CAAC;IACH,CAAC;;IAED,CAAE,gBAAgB,EAAE,aAAa,EAAE,GAAG,MAAM,CAAE,CAC/C;AAEH;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDataManager.js","sources":["../../../src/hooks/useDataManager.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAiCA;;AAEc;SACE,cAAc,GAAA;;;;AAK5B,IAAA,MAAM,CAAE,QAAQ,EAAE,WAAW,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAgB,SAAS,CAAC
|
|
1
|
+
{"version":3,"file":"useDataManager.js","sources":["../../../src/hooks/useDataManager.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAiCA;;AAEc;SACE,cAAc,GAAA;;;;AAK5B,IAAA,MAAM,CAAE,QAAQ,EAAE,WAAW,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAgB,SAAS,CAAC;AAC1E,IAAA,MAAM,CAAE,QAAQ,EAAE,WAAW,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAgB,SAAS,CAAC;;;;AAM1E,IAAA,MAAM,aAAa,GAAGA,gBAAK,CAAC,WAAW,CACrC,MAAM,WAAW,CAAC,SAAS,CAAC,EAC5B,EAAE,CACH;AAED,IAAA,MAAM,aAAa,GAAGA,gBAAK,CAAC,WAAW,CACrC,MAAM,WAAW,CAAC,SAAS,CAAC,EAC5B,EAAE,CACH;IAED,MAAM,qBAAqB,GAAGA,gBAAK,CAAC,WAAW,CAC7C,CAAC,OAAsB,KAAK,MAAM,WAAW,CAAC,OAAO,CAAC,EACtD,EAAE,CACH;IAED,MAAM,qBAAqB,GAAGA,gBAAK,CAAC,WAAW,CAC7C,CAAC,OAAsB,KAAK,MAAM,WAAW,CAAC,OAAO,CAAC,EACtD,EAAE,CACH;;;;IAMD,OAAO;QACL,QAAQ;QACR,aAAa;QACb,aAAa;QACb,qBAAqB;QACrB,qBAAqB;QACrB,WAAW;QACX,WAAW;QACX;KACD;AAEH;;;;"}
|
|
@@ -36,7 +36,7 @@ function useDataSelector(data, options) {
|
|
|
36
36
|
// ----
|
|
37
37
|
// Internal State
|
|
38
38
|
// ----
|
|
39
|
-
const [
|
|
39
|
+
const [requestedSelection, setRequestedSelection] = React__namespace.useState(options?.defaultSelected);
|
|
40
40
|
// ----
|
|
41
41
|
// Internal Hooks
|
|
42
42
|
// ----
|
|
@@ -61,34 +61,68 @@ function useDataSelector(data, options) {
|
|
|
61
61
|
return selectableData.includes(item) ? item : undefined;
|
|
62
62
|
}, [data, userDefinedComparer]);
|
|
63
63
|
const setSelected = React__namespace.useCallback((item) => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const { current: currentUserDefinedOnSelectChange } = userDefinedOnSelectChange;
|
|
67
|
-
if (typeof currentUserDefinedOnSelectChange === 'function') {
|
|
68
|
-
currentUserDefinedOnSelectChange(sourceItem);
|
|
69
|
-
}
|
|
70
|
-
}, [getSourceItem, userDefinedOnSelectChange]);
|
|
64
|
+
setRequestedSelection(item);
|
|
65
|
+
}, []);
|
|
71
66
|
const clearSelected = React__namespace.useCallback(() => setSelected(undefined), [setSelected]);
|
|
72
67
|
const createSelectorHandler = React__namespace.useCallback((item) => () => setSelected(item), [setSelected]);
|
|
73
68
|
// ----
|
|
74
69
|
// Selected Assertion
|
|
75
70
|
// ----
|
|
71
|
+
/**
|
|
72
|
+
* Resolve the requested selection against the data as it is *now*, while rendering.
|
|
73
|
+
*
|
|
74
|
+
* 'getSourceItem' already answers every case the reconciliation has to cover: it returns the matching
|
|
75
|
+
* source item, or 'undefined' when the requested one is not in the data any more — and 'undefined' in,
|
|
76
|
+
* 'undefined' out. So the whole thing is one derivation.
|
|
77
|
+
*
|
|
78
|
+
* It used to be an effect that called setState, which meant every data change that invalidated the
|
|
79
|
+
* selection rendered twice: once with the stale selection, once with the corrected one. Deriving it
|
|
80
|
+
* here means a render never shows a selection the data does not back.
|
|
81
|
+
*/
|
|
82
|
+
const selected = getSourceItem(requestedSelection);
|
|
83
|
+
// ----
|
|
84
|
+
// Selection Change Notification
|
|
85
|
+
// ----
|
|
86
|
+
/**
|
|
87
|
+
* Notify the consumer once per actual change, from a single place.
|
|
88
|
+
*
|
|
89
|
+
* There used to be two sources for this callback — a synchronous call inside the setter and the
|
|
90
|
+
* reconciliation effect calling that same setter — and between them they reported things that were not
|
|
91
|
+
* changes: selecting what was already selected notified, clearing an already empty selection notified
|
|
92
|
+
* twice, and resolving `defaultSelected` against the data notified at mount. Watching the resolved
|
|
93
|
+
* value reports each transition exactly once, after the commit that made it real.
|
|
94
|
+
*/
|
|
95
|
+
const lastNotifiedSelection = React__namespace.useRef(selected);
|
|
76
96
|
React__namespace.useEffect(() => {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
/** If selected exists, but source item not, remove selection */
|
|
80
|
-
if (selected && !sourceItem) {
|
|
81
|
-
setSelected(undefined);
|
|
97
|
+
if (lastNotifiedSelection.current === selected) {
|
|
98
|
+
return;
|
|
82
99
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
100
|
+
lastNotifiedSelection.current = selected;
|
|
101
|
+
const { current: currentUserDefinedOnSelectChange } = userDefinedOnSelectChange;
|
|
102
|
+
if (typeof currentUserDefinedOnSelectChange === 'function') {
|
|
103
|
+
currentUserDefinedOnSelectChange(selected);
|
|
86
104
|
}
|
|
87
|
-
}, [
|
|
105
|
+
}, [selected, userDefinedOnSelectChange]);
|
|
88
106
|
// ----
|
|
89
|
-
//
|
|
107
|
+
// Unmount Notification
|
|
90
108
|
// ----
|
|
91
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Report the selection as gone when the component unmounts.
|
|
111
|
+
*
|
|
112
|
+
* This was `useUnmountEffect(clearSelected)`, and it notified only as a side effect of how the
|
|
113
|
+
* callback used to be wired: `clearSelected` went through the setter, and the setter notified
|
|
114
|
+
* synchronously. Now that notifying belongs to an effect — and effects do not run on the way out —
|
|
115
|
+
* that call became inert, which dropped a notification every consumer had always received.
|
|
116
|
+
*
|
|
117
|
+
* It fires unconditionally, which is what the old path did: it resolved `undefined` through
|
|
118
|
+
* `getSourceItem` and reported it even when nothing was selected to begin with.
|
|
119
|
+
*/
|
|
120
|
+
useUnmountEffect.useUnmountEffect(() => {
|
|
121
|
+
const { current: currentUserDefinedOnSelectChange } = userDefinedOnSelectChange;
|
|
122
|
+
if (typeof currentUserDefinedOnSelectChange === 'function') {
|
|
123
|
+
currentUserDefinedOnSelectChange(undefined);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
92
126
|
// ----
|
|
93
127
|
// Return utilities
|
|
94
128
|
// ----
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDataSelector.js","sources":["../../../src/hooks/useDataSelector.ts"],"sourcesContent":[null],"names":["React","useSyncedRef","useUnmountEffect"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAmCA;;AAEc;AAEd;;;;;AAKG;
|
|
1
|
+
{"version":3,"file":"useDataSelector.js","sources":["../../../src/hooks/useDataSelector.ts"],"sourcesContent":[null],"names":["React","useSyncedRef","useUnmountEffect"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAmCA;;AAEc;AAEd;;;;;AAKG;AACG,SAAU,eAAe,CAC7B,IAA4B,EAC5B,OAAmC,EAAA;;;;AAMnC,IAAA,MAAM,CAAE,kBAAkB,EAAE,qBAAqB,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAgB,OAAO,EAAE,eAAe,CAAC;;;;IAM7G,MAAM,yBAAyB,GAAGC,yBAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC;IACzE,MAAM,mBAAmB,GAAGA,yBAAY,CAAC,OAAO,EAAE,QAAQ,CAAC;;;;IAM3D,MAAM,aAAa,GAAGD,gBAAK,CAAC,WAAW,CACrC,CAAC,IAAmB,KAAmB;;AAErC,QAAA,IAAI,IAAI,KAAK,SAAS,EAAE;AACtB,YAAA,OAAO,SAAS;QAClB;;AAGA,QAAA,MAAM,cAAc,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,EAAE;;AAGtD,QAAA,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,GAAG,mBAAmB;AACxD,QAAA,IAAI,OAAO,eAAe,KAAK,UAAU,EAAE;AACzC,YAAA,OAAO,cAAc,CAAC,IAAI,CAAC,CAAC,KAAK,KAAK,eAAe,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACrE;;AAGA,QAAA,OAAO,cAAc,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS;AACzD,IAAA,CAAC,EACD,CAAE,IAAI,EAAE,mBAAmB,CAAE,CAC9B;IAED,MAAM,WAAW,GAAGA,gBAAK,CAAC,WAAW,CACnC,CAAC,IAAmB,KAAI;QACtB,qBAAqB,CAAC,IAAI,CAAC;IAC7B,CAAC,EACD,EAAE,CACH;AAED,IAAA,MAAM,aAAa,GAAGA,gBAAK,CAAC,WAAW,CACrC,MAAM,WAAW,CAAC,SAAS,CAAC,EAC5B,CAAE,WAAW,CAAE,CAChB;IAED,MAAM,qBAAqB,GAAGA,gBAAK,CAAC,WAAW,CAC7C,CAAC,IAAmB,KAAK,MAAM,WAAW,CAAC,IAAI,CAAC,EAChD,CAAE,WAAW,CAAE,CAChB;;;;AAMD;;;;;;;;;;AAUG;AACH,IAAA,MAAM,QAAQ,GAAG,aAAa,CAAC,kBAAkB,CAAC;;;;AAMlD;;;;;;;;AAQG;IACH,MAAM,qBAAqB,GAAGA,gBAAK,CAAC,MAAM,CAAC,QAAQ,CAAC;AAEpD,IAAAA,gBAAK,CAAC,SAAS,CACb,MAAK;AACH,QAAA,IAAI,qBAAqB,CAAC,OAAO,KAAK,QAAQ,EAAE;YAC9C;QACF;AAEA,QAAA,qBAAqB,CAAC,OAAO,GAAG,QAAQ;AAExC,QAAA,MAAM,EAAE,OAAO,EAAE,gCAAgC,EAAE,GAAG,yBAAyB;AAC/E,QAAA,IAAI,OAAO,gCAAgC,KAAK,UAAU,EAAE;YAC1D,gCAAgC,CAAC,QAAQ,CAAC;QAC5C;AACF,IAAA,CAAC,EACD,CAAE,QAAQ,EAAE,yBAAyB,CAAE,CACxC;;;;AAMD;;;;;;;;;;AAUG;IACHE,iCAAgB,CAAC,MAAK;AACpB,QAAA,MAAM,EAAE,OAAO,EAAE,gCAAgC,EAAE,GAAG,yBAAyB;AAE/E,QAAA,IAAI,OAAO,gCAAgC,KAAK,UAAU,EAAE;YAC1D,gCAAgC,CAAC,SAAS,CAAC;QAC7C;AACF,IAAA,CAAC,CAAC;;;;IAMF,OAAO;QACL,aAAa;QACb,qBAAqB;QACrB,QAAQ;QACR;KACD;AAEH;AAGA,eAAe,CAAC,UAAU,GAAG,CAAwB,KAAQ,EAAE,MAAS,MACtE,KAAK,CAAC,EAAE,KAAK,MAAM,CAAC,EAAE,CACvB;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDebouncedCallback.js","sources":["../../../src/hooks/useDebouncedCallback.ts"],"sourcesContent":[null],"names":["React","useSyncedRef","useUnmountEffect"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAcA;;;;;;;;;AASG;SACa,oBAAoB,CAClC,QAAY,EACZ,IAA0B,EAC1B,QAAgB,EAAA;;IAIhB,MAAM,OAAO,GAAGA,gBAAK,CAAC,MAAM,CAA4C,SAAS,CAAC
|
|
1
|
+
{"version":3,"file":"useDebouncedCallback.js","sources":["../../../src/hooks/useDebouncedCallback.ts"],"sourcesContent":[null],"names":["React","useSyncedRef","useUnmountEffect"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAcA;;;;;;;;;AASG;SACa,oBAAoB,CAClC,QAAY,EACZ,IAA0B,EAC1B,QAAgB,EAAA;;IAIhB,MAAM,OAAO,GAAGA,gBAAK,CAAC,MAAM,CAA4C,SAAS,CAAC;AAClF,IAAA,MAAM,gBAAgB,GAAGC,yBAAY,CAAC,QAAQ,CAAC;;AAG/C,IAAA,MAAM,eAAe,GAAGD,gBAAK,CAAC,WAAW,CACvC,MAAK;AACH,QAAA,IAAI,OAAO,CAAC,OAAO,EAAE;AACnB,YAAA,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC;AAC7B,YAAA,OAAO,CAAC,OAAO,GAAG,SAAS;QAC7B;IACF,CAAC,EACD,EAAE,CACH;;IAGDE,iCAAgB,CAAC,MAAK;AACpB,QAAA,eAAe,EAAE;AACnB,IAAA,CAAC,CAAC;;IAGF,OAAOF,gBAAK,CAAC,WAAW,CACtB,CAAC,GAAG,IAAI,KAAI;;AAEV,QAAA,eAAe,EAAE;;AAGjB,QAAA,OAAO,CAAC,OAAO,GAAG,UAAU,CAAC,MAAK;AAChC,YAAA,gBAAgB,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;QACnC,CAAC,EAAE,QAAQ,CAAC;IACd,CAAC,EACD,CAAE,eAAe,EAAE,QAAQ,EAAE,gBAAgB,CAAE,CAChD;AAEH;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useElementType.js","sources":["../../../src/hooks/useElementType.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAiCA;;AAEc;AAEd;;;;;;;AAOG;AACG,SAAU,cAAc,CAC5B,SAAmE,EACnE,KAAY,EACZ,kBAAsC,EACtC,UAAkC,EAAA;;AAIlC,IAAA,MAAM,EAAE,YAAY,EAAE,qBAAqB,EAAE,GAAG,SAAgB
|
|
1
|
+
{"version":3,"file":"useElementType.js","sources":["../../../src/hooks/useElementType.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAiCA;;AAEc;AAEd;;;;;;;AAOG;AACG,SAAU,cAAc,CAC5B,SAAmE,EACnE,KAAY,EACZ,kBAAsC,EACtC,UAAkC,EAAA;;AAIlC,IAAA,MAAM,EAAE,YAAY,EAAE,qBAAqB,EAAE,GAAG,SAAgB;;AAGhE,IAAA,IAAI,KAAK,CAAC,EAAE,IAAI,KAAK,CAAC,EAAE,KAAK,qBAAqB,EAAE,EAAE,EAAE;QACtD,OAAO,KAAK,CAAC,EAAE;IACjB;;AAGA,IAAA,IAAI,OAAO,UAAU,KAAK,UAAU,EAAE;AACpC,QAAA,MAAM,mBAAmB,GAAG,UAAU,CAAC,KAAK,CAAC;QAE7C,IAAI,mBAAmB,EAAE;AACvB,YAAA,OAAO,mBAAmB;QAC5B;IACF;;AAGA,IAAA,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;AAClC,QAAA,OAAO,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,IAAI,UAAU,CAAC,OAAO;IACtE;;AAGA,IAAA,IAAI,KAAK,CAAC,IAAI,EAAE;AACd,QAAA,OAAO,GAAG;IACZ;;AAGA,IAAA,OAAO,qBAAqB,EAAE,EAAE,IAAI,kBAAkB,IAAI,KAAK;AACjE;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useEnhancedEffect.js","sources":["../../../src/hooks/useEnhancedEffect.ts"],"sourcesContent":[null],"names":["isBrowser","React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA;;;;;AAKG;AACI,MAAM,iBAAiB,GAAGA
|
|
1
|
+
{"version":3,"file":"useEnhancedEffect.js","sources":["../../../src/hooks/useEnhancedEffect.ts"],"sourcesContent":[null],"names":["isBrowser","React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA;;;;;AAKG;AACI,MAAM,iBAAiB,GAAGA;MAC7BC,gBAAK,CAAC;AACR,MAAEA,gBAAK,CAAC;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useEvent.js","sources":["../../../src/hooks/useEvent.ts"],"sourcesContent":[null],"names":["useSyncedRef","useEnhancedEffect"],"mappings":";;;;;AAkBA;;;;;;AAMG;SACa,QAAQ,CACtB,IAAO,EACP,QAA0B,EAC1B,OAAyB,EAAA;;AAIzB,IAAA,MAAM,EACJ,QAAQ,GAAG,KAAK,EAChB,MAAM,GAAG,QAAQ,CAAC,eAAe,EAClC,GAAG,OAAO,IAAI,EAAE
|
|
1
|
+
{"version":3,"file":"useEvent.js","sources":["../../../src/hooks/useEvent.ts"],"sourcesContent":[null],"names":["useSyncedRef","useEnhancedEffect"],"mappings":";;;;;AAkBA;;;;;;AAMG;SACa,QAAQ,CACtB,IAAO,EACP,QAA0B,EAC1B,OAAyB,EAAA;;AAIzB,IAAA,MAAM,EACJ,QAAQ,GAAG,KAAK,EAChB,MAAM,GAAG,QAAQ,CAAC,eAAe,EAClC,GAAG,OAAO,IAAI,EAAE;AAGjB;;;;;;AAMG;AACH,IAAA,MAAM,OAAO,GAAGA,yBAAY,CAAmB,QAAQ,CAAC;IAGxDC,mCAAiB,CACf,MAAK;;QAEH,IAAI,QAAQ,EAAE;YACZ;QACF;AAEA;;;;;AAKG;QACH,MAAM,aAAa,GAAqB,CAAC,KAAK,MAC5C,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,EAAE,CAAE,KAAK,CAAE,CAAC,CACzC;;AAGD,QAAA,MAAM,CAAC,gBAAgB,CAAC,IAAI,EAAE,aAAa,CAAC;;AAG5C,QAAA,OAAO,MAAK;AACV,YAAA,MAAM,CAAC,mBAAmB,CAAC,IAAI,EAAE,aAAa,CAAC;AACjD,QAAA,CAAC;IACH,CAAC,EACD,CAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAE,CACpC;AAEH;;;;"}
|
package/cjs/hooks/useForkRef.js
CHANGED
|
@@ -21,21 +21,49 @@ function _interopNamespaceDefault(e) {
|
|
|
21
21
|
|
|
22
22
|
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
23
23
|
|
|
24
|
+
/* --------
|
|
25
|
+
* Hook Definition
|
|
26
|
+
* -------- */
|
|
24
27
|
/**
|
|
25
|
-
*
|
|
26
|
-
* assign the referenced element to each single ref.
|
|
28
|
+
* Merge several refs into the single ref callback a JSX element can accept.
|
|
27
29
|
*
|
|
28
|
-
*
|
|
30
|
+
* The returned callback assigns the element to every ref it was given, which is how a component both
|
|
31
|
+
* exposes a ref to its parent and keeps one for itself.
|
|
32
|
+
*
|
|
33
|
+
* Its identity is **stable** as long as the same ref containers are passed, which matters beyond
|
|
34
|
+
* tidiness: React calls a ref callback with `null` and then with the node again every time the
|
|
35
|
+
* callback's identity changes.
|
|
36
|
+
*
|
|
37
|
+
* @param refs The refs to assign to
|
|
38
|
+
* @returns A ref callback, stable across renders that pass the same refs
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* const Input = React.forwardRef<HTMLInputElement, Props>((props, ref) => {
|
|
42
|
+
* const innerRef = React.useRef<HTMLInputElement>(null);
|
|
43
|
+
* const handleRef = useForkRef(innerRef, ref as MutableRefLike<HTMLInputElement>);
|
|
44
|
+
*
|
|
45
|
+
* return <input {...props} ref={handleRef} />;
|
|
46
|
+
* });
|
|
29
47
|
*/
|
|
30
48
|
function useForkRef(...refs) {
|
|
31
49
|
return React__namespace.useCallback((refValue) => {
|
|
32
|
-
refs.forEach((ref) =>
|
|
50
|
+
refs.forEach((ref) => {
|
|
51
|
+
ref.current = refValue ?? undefined;
|
|
52
|
+
});
|
|
33
53
|
},
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
54
|
+
/**
|
|
55
|
+
* The rest parameter is a new array on every render, so the refs are spread into the dependency
|
|
56
|
+
* list one by one: ref containers are stable, so the callback survives every render that passes
|
|
57
|
+
* the same ones.
|
|
58
|
+
*
|
|
59
|
+
* Both hook rules object to this, and neither has a better answer here. 'exhaustive-deps' wants the
|
|
60
|
+
* array itself, which changes identity every render and would rebuild the callback each time.
|
|
61
|
+
* 'use-memo' wants a literal, which cannot express a variadic list. Routing the refs through a
|
|
62
|
+
* synced container satisfies both — and then the React Compiler refuses the memoization outright,
|
|
63
|
+
* because it cannot prove a closure reading 'ref.current' is stable. This is the shape the compiler
|
|
64
|
+
* is meant to replace: with it enabled, drop the useCallback entirely and let it do the work.
|
|
65
|
+
*/
|
|
66
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps, react-hooks/use-memo
|
|
39
67
|
[...refs]);
|
|
40
68
|
}
|
|
41
69
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useForkRef.js","sources":["../../../src/hooks/useForkRef.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"useForkRef.js","sources":["../../../src/hooks/useForkRef.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAoBA;;AAEc;AAEd;;;;;;;;;;;;;;;;;;;;AAoBG;AACG,SAAU,UAAU,CAAI,GAAG,IAAyB,EAAA;AACxD,IAAA,OAAOA,gBAAK,CAAC,WAAW,CACtB,CAAC,QAAkB,KAAI;AACrB,QAAA,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,KAAI;AACnB,YAAA,GAAG,CAAC,OAAO,GAAG,QAAQ,IAAI,SAAS;AACrC,QAAA,CAAC,CAAC;IACJ,CAAC;AACD;;;;;;;;;;;AAWG;;AAEH,IAAA,CAAE,GAAG,IAAI,CAAE,CACZ;AACH;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useInputValue.js","sources":["../../../src/hooks/useInputValue.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAGM,SAAU,aAAa,CAAC,YAAqB,EAAA;;;;AASjD,IAAA,MAAM,CAAE,KAAK,EAAE,QAAQ,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAS,YAAY,IAAI,EAAE,CAAC
|
|
1
|
+
{"version":3,"file":"useInputValue.js","sources":["../../../src/hooks/useInputValue.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAGM,SAAU,aAAa,CAAC,YAAqB,EAAA;;;;AASjD,IAAA,MAAM,CAAE,KAAK,EAAE,QAAQ,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAS,YAAY,IAAI,EAAE,CAAC;;;;IAMtE,MAAM,sBAAsB,GAAGA,gBAAK,CAAC,WAAW,CAC9C,CAAC,KAAK,KAAI;AACR,QAAA,QAAQ,CAAC,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC;IACrC,CAAC,EACD,EAAE,CACH;;;;AAMD,IAAA,OAAO,CAAE,KAAK,EAAE,sBAAsB,EAAE,QAAQ,CAAE;AAEpD;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useObjectState.js","sources":["../../../src/hooks/useObjectState.ts"],"sourcesContent":[null],"names":["useSafeState","useSyncedRef","React","getValueAt","setValueAt"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA;;;;;;AAMG;
|
|
1
|
+
{"version":3,"file":"useObjectState.js","sources":["../../../src/hooks/useObjectState.ts"],"sourcesContent":[null],"names":["useSafeState","useSyncedRef","React","getValueAt","setValueAt"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA;;;;;;AAMG;AACG,SAAU,cAAc,CAC5B,aAAsC,EACtC,OAAuC,EAAA;;IAIvC,MAAM,CAAE,KAAK,EAAE,QAAQ,CAAE,GAAGA,yBAAY,CAAC,aAAa,CAAC;;IAGvD,MAAM,gBAAgB,GAAGC,yBAAY,CAAC,OAAO,EAAE,QAAQ,CAAC;AACxD,IAAAC,gBAAK,CAAC,SAAS,CACb,MAAK;AACH,QAAA,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,GAAG,gBAAgB;AAErD,QAAA,IAAI,OAAO,eAAe,KAAK,UAAU,EAAE;YACzC,eAAe,CAAC,KAAK,CAAC;QACxB;AACF,IAAA,CAAC,EACD,CAAE,gBAAgB,EAAE,KAAK,CAAE,CAC5B;;IAGD,MAAM,QAAQ,GAAGA,gBAAK,CAAC,WAAW,CAChC,CAAC,SAAS,KAAI;AACZ,QAAA,IAAI,OAAO,SAAS,KAAK,UAAU,EAAE;AACnC,YAAA,QAAQ,CAAC,CAAC,IAAI,MAAM;gBAClB,GAAG,SAAS,CAAC,IAAI;AAClB,aAAA,CAAC,CAAC;QACL;aACK;AACH,YAAA,QAAQ,CAAC,EAAE,GAAG,SAAS,EAAE,CAAC;QAC5B;AACF,IAAA,CAAC,EACD,CAAE,QAAQ,CAAE,CACb;;IAGD,MAAM,QAAQ,GAAGA,gBAAK,CAAC,WAAW,CAChC,CAAkC,IAAU,MAC1CC,gBAAU,CAAC,KAAK,EAAE,IAAI,CAAC,CACxB,EACD,CAAE,KAAK,CAAE,CACV;;IAGD,MAAM,QAAQ,GAAGD,gBAAK,CAAC,WAAW,CAChC,CAAkC,IAAU,EAAE,KAAoC,KAAI;QACpF,QAAQ,CAAC,CAAC,IAAI,MACZE,gBAAU,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CACnD,CAAC;AACJ,IAAA,CAAC,EACD,CAAE,QAAQ,CAAE,CACb;;IAGD,OAAO,CAAE,KAAK,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAE;AAEpD;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSafeState.js","sources":["../../../src/hooks/useSafeState.ts"],"sourcesContent":[null],"names":["React","useEnhancedEffect"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA;;;;;;AAMG;AACG,SAAU,YAAY,CAC1B,YAA2B,EAAA;;AAI3B,IAAA,MAAM,CAAE,KAAK,EAAE,QAAQ,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAI,YAAY,CAAC
|
|
1
|
+
{"version":3,"file":"useSafeState.js","sources":["../../../src/hooks/useSafeState.ts"],"sourcesContent":[null],"names":["React","useEnhancedEffect"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA;;;;;;AAMG;AACG,SAAU,YAAY,CAC1B,YAA2B,EAAA;;AAI3B,IAAA,MAAM,CAAE,KAAK,EAAE,QAAQ,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAI,YAAY,CAAC;;IAG3D,MAAM,SAAS,GAAGA,gBAAK,CAAC,MAAM,CAAU,KAAK,CAAC;;IAG9CC,mCAAiB,CACf,MAAK;AACH,QAAA,SAAS,CAAC,OAAO,GAAG,IAAI;AAExB,QAAA,OAAO,MAAK;AACV,YAAA,SAAS,CAAC,OAAO,GAAG,KAAK;AAC3B,QAAA,CAAC;IACH,CAAC,EACD,EAAE,CACH;;IAGD,MAAM,cAAc,GAAGD,gBAAK,CAAC,WAAW,CACtC,CAAC,QAAQ,KAAI;AACX,QAAA,IAAI,SAAS,CAAC,OAAO,EAAE;YACrB,QAAQ,CAAC,QAAQ,CAAC;QACpB;IACF,CAAC,EACD,EAAE,CACH;;AAGD,IAAA,OAAO,CAAE,KAAK,EAAE,cAAc,CAAE;AAElC;;;;"}
|
|
@@ -22,15 +22,47 @@ function _interopNamespaceDefault(e) {
|
|
|
22
22
|
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
26
|
-
* It will return a 'ref' style object with the
|
|
27
|
-
* current key memoized, returning each time
|
|
28
|
-
* the most updated version of the value
|
|
25
|
+
* Keep the latest value passed to a component inside a stable, read only ref container.
|
|
29
26
|
*
|
|
30
|
-
*
|
|
27
|
+
* The container identity never changes, so it can sit in a dependency array without ever
|
|
28
|
+
* invalidating it — which is the whole point: it lets an event handler, a debounce timer or a
|
|
29
|
+
* cleanup function read the *current* value without the effect that owns them having to be torn
|
|
30
|
+
* down and rebuilt on every render.
|
|
31
|
+
*
|
|
32
|
+
* The returned object is frozen and exposes `current` as a getter, so a consumer can read the value
|
|
33
|
+
* but cannot write it.
|
|
34
|
+
*
|
|
35
|
+
* @param value The value to keep in sync
|
|
36
|
+
* @returns A stable container whose `current` always reads the latest value
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* function useAutoSave(onSave: () => void, delay: number) {
|
|
40
|
+
* const latestOnSave = useSyncedRef(onSave);
|
|
41
|
+
*
|
|
42
|
+
* React.useEffect(
|
|
43
|
+
* () => {
|
|
44
|
+
* // the interval is created once, and still calls the newest callback
|
|
45
|
+
* const id = setInterval(() => latestOnSave.current(), delay);
|
|
46
|
+
* return () => clearInterval(id);
|
|
47
|
+
* },
|
|
48
|
+
* [ delay, latestOnSave ]
|
|
49
|
+
* );
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* ⚠️ Read `current` from an effect, an event handler or a cleanup function — **not while rendering**.
|
|
54
|
+
* The value is written during render, which is what makes it available to the very commit it belongs
|
|
55
|
+
* to, and reading a ref during render is not something React guarantees anything about: a render that
|
|
56
|
+
* gets discarded leaves the container holding a value that was never committed.
|
|
57
|
+
*
|
|
58
|
+
* That write is also why `react-hooks/refs` is switched off just below. Every alternative loses the
|
|
59
|
+
* guarantee this hook exists to provide — writing in an effect makes `current` lag one commit behind,
|
|
60
|
+
* which is precisely the bug the callers of this hook are avoiding. The exposure is bounded: the
|
|
61
|
+
* container is only ever read after a commit, and every commit writes it again.
|
|
31
62
|
*/
|
|
32
63
|
function useSyncedRef(value) {
|
|
33
64
|
const ref = React__namespace.useRef(value);
|
|
65
|
+
// eslint-disable-next-line react-hooks/refs -- see the remark above: the render time write is the contract
|
|
34
66
|
ref.current = value;
|
|
35
67
|
return React__namespace.useMemo(() => Object.freeze({
|
|
36
68
|
get current() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSyncedRef.js","sources":["../../../src/hooks/useSyncedRef.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAGA
|
|
1
|
+
{"version":3,"file":"useSyncedRef.js","sources":["../../../src/hooks/useSyncedRef.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCG;AACG,SAAU,YAAY,CAAI,KAAQ,EAAA;IACtC,MAAM,GAAG,GAAGA,gBAAK,CAAC,MAAM,CAAC,KAAK,CAAC;;AAG/B,IAAA,GAAG,CAAC,OAAO,GAAG,KAAK;IAEnB,OAAOA,gBAAK,CAAC,OAAO,CAClB,MAAM,MAAM,CAAC,MAAM,CAAC;AAClB,QAAA,IAAI,OAAO,GAAA;YACT,OAAO,GAAG,CAAC,OAAO;QACpB;KACD,CAAC,EACF,EAAE,CACH;AACH;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useUnmountEffect.js","sources":["../../../src/hooks/useUnmountEffect.ts"],"sourcesContent":[null],"names":["useSyncedRef","useEnhancedEffect"],"mappings":";;;;;AAIA;;;;;;AAMG;AACG,SAAU,gBAAgB,CAC9B,QAA0B,EAAA;;AAI1B,IAAA,MAAM,eAAe,GAAGA,yBAAY,CAAC,QAAQ,CAAC
|
|
1
|
+
{"version":3,"file":"useUnmountEffect.js","sources":["../../../src/hooks/useUnmountEffect.ts"],"sourcesContent":[null],"names":["useSyncedRef","useEnhancedEffect"],"mappings":";;;;;AAIA;;;;;;AAMG;AACG,SAAU,gBAAgB,CAC9B,QAA0B,EAAA;;AAI1B,IAAA,MAAM,eAAe,GAAGA,yBAAY,CAAC,QAAQ,CAAC;;AAG9C,IAAAC,mCAAiB,CACf,MAAM,MAAK;QACT,eAAe,CAAC,OAAO,EAAE;AAC3B,IAAA,CAAC,EACD,CAAE,eAAe,CAAE,CACpB;AAEH;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useWindowSize.js","sources":["../../../src/hooks/useWindowSize.ts"],"sourcesContent":[null],"names":["React","useEnhancedEffect"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAsBA;;;;;;;;;;;;;;;;;AAiBG;AACG,SAAU,aAAa,CAAC,OAA8B,EAAA;AAE1D,IAAA,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,IAAI,EAAE
|
|
1
|
+
{"version":3,"file":"useWindowSize.js","sources":["../../../src/hooks/useWindowSize.ts"],"sourcesContent":[null],"names":["React","useEnhancedEffect"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAsBA;;;;;;;;;;;;;;;;;AAiBG;AACG,SAAU,aAAa,CAAC,OAA8B,EAAA;AAE1D,IAAA,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,IAAI,EAAE;;;;AAKlC,IAAA,MAAM,CAAE,WAAW,EAAE,cAAc,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAa,OAAO;AACxE,QAAA,MAAM,EAAE,OAAO,MAAM,KAAK,WAAW,GAAG,MAAM,CAAC,WAAW,GAAG,CAAC;AAC9D,QAAA,KAAK,EAAG,OAAO,MAAM,KAAK,WAAW,GAAG,MAAM,CAAC,UAAU,GAAG;AAC7D,KAAA,CAAC,CAAC;;;;AAMH,IAAA,MAAM,sBAAsB,GAAGA,gBAAK,CAAC,WAAW,CAC9C,MAAM,cAAc,CAAC,CAAC,IAAI,KAAI;AAC5B,QAAA,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,WAAW;AAChC,QAAA,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,UAAU;AAC9B,QAAA,OAAO,IAAI;AACb,IAAA,CAAC,CAAC,EACF,EAAE,CACH;;;;IAMDC,mCAAiB,CACf,MAAK;;QAEH,IAAI,QAAQ,EAAE;YACZ;QACF;;AAGA,QAAA,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,sBAAsB,CAAC;;AAGzD,QAAA,OAAO,MAAK;AACV,YAAA,MAAM,CAAC,mBAAmB,CAAC,QAAQ,EAAE,sBAAsB,CAAC;AAC9D,QAAA,CAAC;AACH,IAAA,CAAC,EACD,CAAE,QAAQ,EAAE,sBAAsB,CAAE,CACrC;;;;AAMD,IAAA,OAAO,WAAW;AAEpB;;;;"}
|
package/cjs/package.json
ADDED
package/cjs/utils/compose.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compose.js","sources":["../../../src/utils/compose.tsx"],"sourcesContent":[null],"names":["_jsx","React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAeA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;
|
|
1
|
+
{"version":3,"file":"compose.js","sources":["../../../src/utils/compose.tsx"],"sourcesContent":[null],"names":["_jsx","React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAeA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACG,SAAU,OAAO,CAAC,GAAG,UAAgC,EAAA;IAEzD,MAAM,iBAAiB,GAAqD,CAAC,KAAK,MAChFA,cAAA,CAACC,gBAAK,CAAC,QAAQ,EAAA,EAAA,QAAA,EACZ,UAAU,CAAC,WAAW,CAAC,CAAC,QAAQ,EAAE,QAAQ,KAAI;;AAE7C,YAAA,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;;AAE3B,gBAAA,MAAM,CAAE,SAAS,EAAE,cAAc,CAAE,GAAG,QAAQ;;gBAE9C,QACED,eAAC,SAAS,EAAA,EAAA,GAAK,cAAc,EAAA,QAAA,EAC1B,QAAQ,EAAA,CACC;YAEhB;;AAGA,YAAA,QACEA,cAAA,CAAC,QAAQ,cACN,QAAQ,EAAA,CACA;AAEf,QAAA,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,EAAA,CACH,CAClB;AAED,IAAA,iBAAiB,CAAC,WAAW,GAAG,mBAAmB;AAEnD,IAAA,OAAO,iBAAiB;AAE1B;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contextBuilder.js","sources":["../../../src/utils/contextBuilder.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAWA;;;;;;AAMG;
|
|
1
|
+
{"version":3,"file":"contextBuilder.js","sources":["../../../src/utils/contextBuilder.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAWA;;;;;;AAMG;AACG,SAAU,cAAc,CAAsB,IAAY,EAAE,cAAkB,EAAA;;IAGlF,MAAM,WAAW,GAAGA,gBAAK,CAAC,aAAa,CAAgB,cAAc,CAAC;;IAGtE,MAAM,cAAc,GAAY,MAAK;;QAEnC,MAAM,YAAY,GAAGA,gBAAK,CAAC,UAAU,CAAC,WAAW,CAAC;;QAElD,IAAI,CAAC,YAAY,EAAE;AACjB,YAAA,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,CAAA,yCAAA,CAA2C,CAAC;QAC3F;;AAEA,QAAA,OAAO,YAAY;AACrB,IAAA,CAAC;;AAGD,IAAA,WAAW,CAAC,WAAW,GAAG,IAAI;AAE9B,IAAA,OAAO,CAAE,WAAW,EAAE,cAAc,CAAE;AACxC;;;;"}
|