@proedis/react 2.0.0 → 2.1.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 +50 -0
- package/cjs/hooks/useAutoSizer.js +246 -0
- package/cjs/hooks/useAutoSizer.js.map +1 -0
- package/cjs/index.js +3 -0
- package/cjs/index.js.map +1 -1
- package/esm/hooks/useAutoSizer.js +224 -0
- package/esm/hooks/useAutoSizer.js.map +1 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/package.json +3 -3
- package/types/hooks/index.d.ts +1 -0
- package/types/hooks/useAutoSizer.d.ts +82 -0
package/README.md
CHANGED
|
@@ -26,6 +26,7 @@ No UI kit, no styling, no opinions about how anything looks. 🎨
|
|
|
26
26
|
| **Ref hooks** | `useSyncedRef`, `useForkRef` |
|
|
27
27
|
| **Effect hooks** | `useEnhancedEffect`, `useUnmountEffect`, `useDebouncedCallback` |
|
|
28
28
|
| **DOM hooks** | `useEvent`, `useClickOutside`, `useWindowSize`, `useElementType` |
|
|
29
|
+
| **Measurement** | `useAutoSizer` — the room an element has, kept up to date; `getAncestorsSpace` |
|
|
29
30
|
|
|
30
31
|
## 📦 Installation
|
|
31
32
|
|
|
@@ -150,6 +151,7 @@ providers at the root of an app into a single element.
|
|
|
150
151
|
| `useClickOutside(target, callback)` | the outside-click primitive behind every dropdown |
|
|
151
152
|
| `useWindowSize(options?)` | viewport size, debounced |
|
|
152
153
|
| `useElementType(props, default)` | resolve the `as` prop into the element to render |
|
|
154
|
+
| `useAutoSizer(options?)` | the room an element has, in pixels, kept up to date — see below |
|
|
153
155
|
|
|
154
156
|
💡 Several of these are built on `useSyncedRef`, and that is the pattern worth internalising: keep the
|
|
155
157
|
*subscription* stable and let it read a moving value, instead of tearing it down whenever the value
|
|
@@ -159,6 +161,54 @@ changes.
|
|
|
159
161
|
value is written during render, which is what makes it current for the commit it belongs to, and a
|
|
160
162
|
render that gets discarded leaves the container holding something that was never committed.
|
|
161
163
|
|
|
164
|
+
### 📐 `useAutoSizer(options?)`
|
|
165
|
+
|
|
166
|
+
The replacement for `height: calc(100vh - 320px)`.
|
|
167
|
+
|
|
168
|
+
A virtualized table, a map or a chart needs a pixel height; the height available depends on
|
|
169
|
+
everything rendered above it; and that number is not a constant — a filter bar wraps onto a second
|
|
170
|
+
line, a sidebar collapses, a notification appears, the font finally loads.
|
|
171
|
+
|
|
172
|
+
```tsx
|
|
173
|
+
const [ AutoSizer, { height } ] = useAutoSizer({ minHeight: 240 });
|
|
174
|
+
|
|
175
|
+
return (
|
|
176
|
+
<AutoSizer>
|
|
177
|
+
<VirtualizedTable height={height} rows={rows} />
|
|
178
|
+
</AutoSizer>
|
|
179
|
+
);
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Each axis answers with **the room left between the element and the far edge of the viewport**, minus
|
|
183
|
+
what the ancestors still need for their own padding, margin and border. That subtraction is the
|
|
184
|
+
point: without it a table filling "the rest of the screen" overflows its card by exactly the card's
|
|
185
|
+
padding, and the page grows a scrollbar nobody asked for.
|
|
186
|
+
|
|
187
|
+
| Option | Effect |
|
|
188
|
+
| --- | --- |
|
|
189
|
+
| `fixedHeight` / `fixedWidth` | pin an axis and skip its computation |
|
|
190
|
+
| `minHeight` / `maxHeight`, `minWidth` / `maxWidth` | clamp the result |
|
|
191
|
+
| `useOwnHeight` / `useOwnWidth` | measure the element itself instead of the room around it |
|
|
192
|
+
| `disabled` | stop measuring, keeping the last size — for a collapsed panel or a hidden tab |
|
|
193
|
+
|
|
194
|
+
Options are read through a ref, so changing them mid-life is safe and never re-subscribes anything.
|
|
195
|
+
|
|
196
|
+
**What it watches**: the element and its parent through a `ResizeObserver`, the viewport through a
|
|
197
|
+
resize listener, and its own visibility through an `IntersectionObserver` — so a tab that was hidden
|
|
198
|
+
when it mounted measures itself the moment it is shown, instead of reporting zero until the next
|
|
199
|
+
window resize. Measurements are coalesced into one animation frame, because a resize observer fires
|
|
200
|
+
several times per frame while a layout settles.
|
|
201
|
+
|
|
202
|
+
The returned component is a plain `div` that forwards its ref and every prop, and its **identity is
|
|
203
|
+
stable**: a component whose identity changes remounts its subtree, which for a virtualized table
|
|
204
|
+
means losing the scroll position on every resize.
|
|
205
|
+
|
|
206
|
+
`getAncestorsSpace(element)` is that subtraction on its own, for anything sizing an element it does
|
|
207
|
+
not own — a canvas, a third-party widget.
|
|
208
|
+
|
|
209
|
+
⚠️ Server-side it reports the `fixed*` values, or zero: there is nothing to measure until the effect
|
|
210
|
+
runs. Give the sized content a sensible `minHeight` if the first paint matters.
|
|
211
|
+
|
|
162
212
|
## 🔀 Migrating to 2.x
|
|
163
213
|
|
|
164
214
|
| Change | What to do |
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var useSyncedRef = require('./useSyncedRef.js');
|
|
6
|
+
|
|
7
|
+
function _interopNamespaceDefault(e) {
|
|
8
|
+
var n = Object.create(null);
|
|
9
|
+
if (e) {
|
|
10
|
+
Object.keys(e).forEach(function (k) {
|
|
11
|
+
if (k !== 'default') {
|
|
12
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
13
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () { return e[k]; }
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
n.default = e;
|
|
21
|
+
return Object.freeze(n);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
25
|
+
|
|
26
|
+
/* --------
|
|
27
|
+
* Utilities
|
|
28
|
+
* -------- */
|
|
29
|
+
/**
|
|
30
|
+
* Cross-realm `instanceof HTMLElement`.
|
|
31
|
+
*
|
|
32
|
+
* An element rendered into a portal, an iframe or a popup window belongs to a different realm, where
|
|
33
|
+
* the global `HTMLElement` is a different constructor — so the plain `instanceof` answers `false` on
|
|
34
|
+
* a perfectly valid element. Asking its own document's view keeps the walk working there.
|
|
35
|
+
*/
|
|
36
|
+
function isHtmlElement(element) {
|
|
37
|
+
const view = element?.ownerDocument?.defaultView;
|
|
38
|
+
return !!view && element instanceof view.HTMLElement;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Sum the space every ancestor still needs *after* the element, walking up to the document.
|
|
42
|
+
*
|
|
43
|
+
* This is what makes the hook worth having. An element's `getBoundingClientRect().top` says where it
|
|
44
|
+
* starts; the room left below it is the rest of the viewport **minus** the bottom padding, margin and
|
|
45
|
+
* border of each ancestor — otherwise a table filling "the rest of the screen" overflows its card by
|
|
46
|
+
* exactly the card's own padding, and the page grows a scrollbar nobody asked for.
|
|
47
|
+
*
|
|
48
|
+
* Exported because it is the useful half on its own: anything sizing an element it does not own — a
|
|
49
|
+
* canvas, a third-party widget — needs the same number and has no reason to reimplement the walk.
|
|
50
|
+
*
|
|
51
|
+
* @param element - Where to start. Usually the parent of the element being sized.
|
|
52
|
+
*/
|
|
53
|
+
function getAncestorsSpace(element) {
|
|
54
|
+
const space = { bottom: 0, right: 0 };
|
|
55
|
+
let current = element;
|
|
56
|
+
while (isHtmlElement(current)) {
|
|
57
|
+
const styles = current.ownerDocument.defaultView.getComputedStyle(current);
|
|
58
|
+
space.bottom += (parseFloat(styles.marginBottom) || 0)
|
|
59
|
+
+ (parseFloat(styles.paddingBottom) || 0)
|
|
60
|
+
+ (parseFloat(styles.borderBottomWidth) || 0);
|
|
61
|
+
space.right += (parseFloat(styles.marginRight) || 0)
|
|
62
|
+
+ (parseFloat(styles.paddingRight) || 0)
|
|
63
|
+
+ (parseFloat(styles.borderRightWidth) || 0);
|
|
64
|
+
current = current.parentElement;
|
|
65
|
+
}
|
|
66
|
+
return { bottom: Math.ceil(space.bottom), right: Math.ceil(space.right) };
|
|
67
|
+
}
|
|
68
|
+
function clamp(value, min, max) {
|
|
69
|
+
return Math.min(max ?? Number.MAX_SAFE_INTEGER, Math.max(min ?? 0, value));
|
|
70
|
+
}
|
|
71
|
+
/* --------
|
|
72
|
+
* Hook Definition
|
|
73
|
+
* -------- */
|
|
74
|
+
/**
|
|
75
|
+
* Measure the room an element has, and keep measuring it.
|
|
76
|
+
*
|
|
77
|
+
* The problem it replaces is `height: calc(100vh - 320px)`: a virtualized table, a map or a chart
|
|
78
|
+
* needs a pixel height, the height available depends on everything rendered above it, and that
|
|
79
|
+
* number is not a constant — a filter bar wraps onto a second line, a sidebar collapses, a
|
|
80
|
+
* notification appears, the font finally loads.
|
|
81
|
+
*
|
|
82
|
+
* Render the returned component where the sized content goes, and use the numbers:
|
|
83
|
+
*
|
|
84
|
+
* ```tsx
|
|
85
|
+
* const [ AutoSizer, { height } ] = useAutoSizer({ minHeight: 240 });
|
|
86
|
+
*
|
|
87
|
+
* return (
|
|
88
|
+
* <AutoSizer>
|
|
89
|
+
* <VirtualizedTable height={height} rows={rows} />
|
|
90
|
+
* </AutoSizer>
|
|
91
|
+
* );
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* By default each axis answers with **the room left between the element and the far edge of the
|
|
95
|
+
* viewport**, minus what the ancestors still need for their own padding, margin and border. Switch an
|
|
96
|
+
* axis to `useOwnHeight` / `useOwnWidth` to measure the element itself instead, or pin it with
|
|
97
|
+
* `fixedHeight` / `fixedWidth`.
|
|
98
|
+
*
|
|
99
|
+
* What it watches: the element and its parent through a `ResizeObserver`, the viewport through a
|
|
100
|
+
* resize listener, and its own visibility through an `IntersectionObserver` — so a tab that was
|
|
101
|
+
* hidden when it mounted measures itself the moment it is shown, instead of reporting zero until the
|
|
102
|
+
* next window resize. Measurements are coalesced into one animation frame, because a resize observer
|
|
103
|
+
* fires several times per frame while a layout settles.
|
|
104
|
+
*
|
|
105
|
+
* @param options - See `UseAutoSizerOptions`. Read through a ref, so changing them mid-life is safe
|
|
106
|
+
* and never re-subscribes the observers.
|
|
107
|
+
*/
|
|
108
|
+
function useAutoSizer(options) {
|
|
109
|
+
// ----
|
|
110
|
+
// Internal State
|
|
111
|
+
// ----
|
|
112
|
+
/**
|
|
113
|
+
* The element lives in state, not only in a ref: the observers must be attached when it mounts,
|
|
114
|
+
* and an effect cannot depend on a ref's content.
|
|
115
|
+
*/
|
|
116
|
+
const [element, setElement] = React__namespace.useState(null);
|
|
117
|
+
const [size, setSize] = React__namespace.useState(() => ({
|
|
118
|
+
height: options?.fixedHeight ?? 0,
|
|
119
|
+
width: options?.fixedWidth ?? 0
|
|
120
|
+
}));
|
|
121
|
+
// ----
|
|
122
|
+
// Internal Hooks
|
|
123
|
+
// ----
|
|
124
|
+
const optionsRef = useSyncedRef.useSyncedRef(options);
|
|
125
|
+
/** The last emitted size, so the measure function never depends on the state it writes */
|
|
126
|
+
const sizeRef = React__namespace.useRef(size);
|
|
127
|
+
const frameRef = React__namespace.useRef(null);
|
|
128
|
+
// ----
|
|
129
|
+
// Handlers
|
|
130
|
+
// ----
|
|
131
|
+
const measure = React__namespace.useCallback(() => {
|
|
132
|
+
const { disabled, fixedHeight, fixedWidth, maxHeight, maxWidth, minHeight, minWidth, useOwnHeight, useOwnWidth } = optionsRef.current ?? {};
|
|
133
|
+
if (!element || disabled) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const view = element.ownerDocument.defaultView;
|
|
137
|
+
if (!view) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const space = getAncestorsSpace(element.parentElement);
|
|
141
|
+
const rect = element.getBoundingClientRect();
|
|
142
|
+
const nextHeight = clamp(fixedHeight
|
|
143
|
+
?? (useOwnHeight
|
|
144
|
+
? element.clientHeight
|
|
145
|
+
: Math.max(view.innerHeight - rect.top - space.bottom, 0)), minHeight, maxHeight);
|
|
146
|
+
const nextWidth = clamp(fixedWidth
|
|
147
|
+
?? (useOwnWidth
|
|
148
|
+
? element.clientWidth
|
|
149
|
+
: Math.max(view.innerWidth - rect.left - space.right, 0)), minWidth, maxWidth);
|
|
150
|
+
const { current: lastSize } = sizeRef;
|
|
151
|
+
if (nextHeight === lastSize.height && nextWidth === lastSize.width) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
sizeRef.current = { height: nextHeight, width: nextWidth };
|
|
155
|
+
setSize(sizeRef.current);
|
|
156
|
+
}, [element, optionsRef]);
|
|
157
|
+
/**
|
|
158
|
+
* Coalesce into one frame. A `ResizeObserver` fires repeatedly while a layout settles, and each
|
|
159
|
+
* measurement reads the layout back — measuring once per frame is the difference between a smooth
|
|
160
|
+
* resize and a janky one.
|
|
161
|
+
*/
|
|
162
|
+
const scheduleMeasure = React__namespace.useCallback(() => {
|
|
163
|
+
const view = element?.ownerDocument.defaultView;
|
|
164
|
+
if (!view) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (frameRef.current !== null) {
|
|
168
|
+
view.cancelAnimationFrame(frameRef.current);
|
|
169
|
+
}
|
|
170
|
+
frameRef.current = view.requestAnimationFrame(() => {
|
|
171
|
+
frameRef.current = null;
|
|
172
|
+
measure();
|
|
173
|
+
});
|
|
174
|
+
}, [element, measure]);
|
|
175
|
+
// ----
|
|
176
|
+
// Lifecycle Events
|
|
177
|
+
// ----
|
|
178
|
+
React__namespace.useEffect(() => {
|
|
179
|
+
const view = element?.ownerDocument.defaultView;
|
|
180
|
+
if (!element || !view) {
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
/** Measure once, synchronously, so the first paint is not a frame late */
|
|
184
|
+
measure();
|
|
185
|
+
const observed = [element, element.parentElement].filter(Boolean);
|
|
186
|
+
const resizeObserver = new view.ResizeObserver(scheduleMeasure);
|
|
187
|
+
observed.forEach(target => resizeObserver.observe(target));
|
|
188
|
+
/**
|
|
189
|
+
* A hidden element has a zero rect, so measuring it produces zero. Watching its visibility is
|
|
190
|
+
* what makes a tab, an accordion or a collapsed panel measure itself the moment it appears.
|
|
191
|
+
*/
|
|
192
|
+
const intersectionObserver = new view.IntersectionObserver((entries) => {
|
|
193
|
+
if (entries.some(entry => entry.isIntersecting)) {
|
|
194
|
+
scheduleMeasure();
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
intersectionObserver.observe(element);
|
|
198
|
+
view.addEventListener('resize', scheduleMeasure);
|
|
199
|
+
view.addEventListener('orientationchange', scheduleMeasure);
|
|
200
|
+
return () => {
|
|
201
|
+
if (frameRef.current !== null) {
|
|
202
|
+
view.cancelAnimationFrame(frameRef.current);
|
|
203
|
+
frameRef.current = null;
|
|
204
|
+
}
|
|
205
|
+
resizeObserver.disconnect();
|
|
206
|
+
intersectionObserver.disconnect();
|
|
207
|
+
view.removeEventListener('resize', scheduleMeasure);
|
|
208
|
+
view.removeEventListener('orientationchange', scheduleMeasure);
|
|
209
|
+
};
|
|
210
|
+
}, [element, measure, scheduleMeasure]);
|
|
211
|
+
// ----
|
|
212
|
+
// Memoized Data
|
|
213
|
+
// ----
|
|
214
|
+
/**
|
|
215
|
+
* One element, and a stable component identity.
|
|
216
|
+
*
|
|
217
|
+
* Both matter. The ancestor of this hook rendered *two* divs — a zero-height "viewport detector"
|
|
218
|
+
* plus the measured one — and spread the caller's props onto both, so a `className` with padding
|
|
219
|
+
* was applied twice. And a component whose identity changes remounts its whole subtree, which for
|
|
220
|
+
* a virtualized table means losing its scroll position on every resize.
|
|
221
|
+
*/
|
|
222
|
+
const AutoSizer = React__namespace.useMemo(() => {
|
|
223
|
+
const Component = React__namespace.forwardRef((props, forwardedRef) => {
|
|
224
|
+
const handleRef = React__namespace.useCallback((node) => {
|
|
225
|
+
setElement(node);
|
|
226
|
+
if (typeof forwardedRef === 'function') {
|
|
227
|
+
forwardedRef(node);
|
|
228
|
+
}
|
|
229
|
+
else if (forwardedRef) {
|
|
230
|
+
forwardedRef.current = node;
|
|
231
|
+
}
|
|
232
|
+
}, [forwardedRef]);
|
|
233
|
+
return jsxRuntime.jsx("div", { ...props, ref: handleRef });
|
|
234
|
+
});
|
|
235
|
+
Component.displayName = 'AutoSizer';
|
|
236
|
+
return Component;
|
|
237
|
+
}, []);
|
|
238
|
+
// ----
|
|
239
|
+
// Hook Return
|
|
240
|
+
// ----
|
|
241
|
+
return [AutoSizer, size];
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
exports.getAncestorsSpace = getAncestorsSpace;
|
|
245
|
+
exports.useAutoSizer = useAutoSizer;
|
|
246
|
+
//# sourceMappingURL=useAutoSizer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAutoSizer.js","sources":["../../../src/hooks/useAutoSizer.tsx"],"sourcesContent":[null],"names":["React","useSyncedRef","_jsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA8DA;;AAEc;AAEd;;;;;;AAMG;AACH,SAAS,aAAa,CAAC,OAAmC,EAAA;AACxD,IAAA,MAAM,IAAI,GAAG,OAAO,EAAE,aAAa,EAAE,WAAW;IAEhD,OAAO,CAAC,CAAC,IAAI,IAAI,OAAO,YAAY,IAAI,CAAC,WAAW;AACtD;AAGA;;;;;;;;;;;;AAYG;AACG,SAAU,iBAAiB,CAAC,OAA2B,EAAA;IAC3D,MAAM,KAAK,GAAmB,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE;IAErD,IAAI,OAAO,GAAuB,OAAO;AAEzC,IAAA,OAAO,aAAa,CAAC,OAAO,CAAC,EAAE;AAC7B,QAAA,MAAM,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,WAAY,CAAC,gBAAgB,CAAC,OAAO,CAAC;AAE3E,QAAA,KAAK,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC;eAChD,UAAU,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC;eACrC,UAAU,CAAC,MAAM,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;AAE/C,QAAA,KAAK,CAAC,KAAK,IAAI,CAAC,UAAU,CAA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AC3C,QAAA,CAAC,CACF;AAED,QAAA,SAAS,CAAC,WAAW,GAAG,WAAW;AAEnC,QAAA,OAAO,SAAS;IAClB,CAAC,EACD,EAAE,CACH;;;;AAMD,IAAA,OAAO,CAAE,SAAS,EAAE,IAAI,CAAE;AAE5B;;;;;"}
|
package/cjs/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var RenderWhen = require('./components/RenderWhen/RenderWhen.js');
|
|
4
4
|
var useAutoControlledState = require('./hooks/useAutoControlledState.js');
|
|
5
|
+
var useAutoSizer = require('./hooks/useAutoSizer.js');
|
|
5
6
|
var useClickOutside = require('./hooks/useClickOutside.js');
|
|
6
7
|
var useDataManager = require('./hooks/useDataManager.js');
|
|
7
8
|
var useDataSelector = require('./hooks/useDataSelector.js');
|
|
@@ -24,6 +25,8 @@ var shorthands = require('./utils/shorthands.js');
|
|
|
24
25
|
|
|
25
26
|
exports.RenderWhen = RenderWhen;
|
|
26
27
|
exports.useAutoControlledState = useAutoControlledState.useAutoControlledState;
|
|
28
|
+
exports.getAncestorsSpace = useAutoSizer.getAncestorsSpace;
|
|
29
|
+
exports.useAutoSizer = useAutoSizer.useAutoSizer;
|
|
27
30
|
exports.useClickOutside = useClickOutside.useClickOutside;
|
|
28
31
|
exports.useDataManager = useDataManager.useDataManager;
|
|
29
32
|
exports.useDataSelector = useDataSelector.useDataSelector;
|
package/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { useSyncedRef } from './useSyncedRef.js';
|
|
4
|
+
|
|
5
|
+
/* --------
|
|
6
|
+
* Utilities
|
|
7
|
+
* -------- */
|
|
8
|
+
/**
|
|
9
|
+
* Cross-realm `instanceof HTMLElement`.
|
|
10
|
+
*
|
|
11
|
+
* An element rendered into a portal, an iframe or a popup window belongs to a different realm, where
|
|
12
|
+
* the global `HTMLElement` is a different constructor — so the plain `instanceof` answers `false` on
|
|
13
|
+
* a perfectly valid element. Asking its own document's view keeps the walk working there.
|
|
14
|
+
*/
|
|
15
|
+
function isHtmlElement(element) {
|
|
16
|
+
const view = element?.ownerDocument?.defaultView;
|
|
17
|
+
return !!view && element instanceof view.HTMLElement;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Sum the space every ancestor still needs *after* the element, walking up to the document.
|
|
21
|
+
*
|
|
22
|
+
* This is what makes the hook worth having. An element's `getBoundingClientRect().top` says where it
|
|
23
|
+
* starts; the room left below it is the rest of the viewport **minus** the bottom padding, margin and
|
|
24
|
+
* border of each ancestor — otherwise a table filling "the rest of the screen" overflows its card by
|
|
25
|
+
* exactly the card's own padding, and the page grows a scrollbar nobody asked for.
|
|
26
|
+
*
|
|
27
|
+
* Exported because it is the useful half on its own: anything sizing an element it does not own — a
|
|
28
|
+
* canvas, a third-party widget — needs the same number and has no reason to reimplement the walk.
|
|
29
|
+
*
|
|
30
|
+
* @param element - Where to start. Usually the parent of the element being sized.
|
|
31
|
+
*/
|
|
32
|
+
function getAncestorsSpace(element) {
|
|
33
|
+
const space = { bottom: 0, right: 0 };
|
|
34
|
+
let current = element;
|
|
35
|
+
while (isHtmlElement(current)) {
|
|
36
|
+
const styles = current.ownerDocument.defaultView.getComputedStyle(current);
|
|
37
|
+
space.bottom += (parseFloat(styles.marginBottom) || 0)
|
|
38
|
+
+ (parseFloat(styles.paddingBottom) || 0)
|
|
39
|
+
+ (parseFloat(styles.borderBottomWidth) || 0);
|
|
40
|
+
space.right += (parseFloat(styles.marginRight) || 0)
|
|
41
|
+
+ (parseFloat(styles.paddingRight) || 0)
|
|
42
|
+
+ (parseFloat(styles.borderRightWidth) || 0);
|
|
43
|
+
current = current.parentElement;
|
|
44
|
+
}
|
|
45
|
+
return { bottom: Math.ceil(space.bottom), right: Math.ceil(space.right) };
|
|
46
|
+
}
|
|
47
|
+
function clamp(value, min, max) {
|
|
48
|
+
return Math.min(max ?? Number.MAX_SAFE_INTEGER, Math.max(min ?? 0, value));
|
|
49
|
+
}
|
|
50
|
+
/* --------
|
|
51
|
+
* Hook Definition
|
|
52
|
+
* -------- */
|
|
53
|
+
/**
|
|
54
|
+
* Measure the room an element has, and keep measuring it.
|
|
55
|
+
*
|
|
56
|
+
* The problem it replaces is `height: calc(100vh - 320px)`: a virtualized table, a map or a chart
|
|
57
|
+
* needs a pixel height, the height available depends on everything rendered above it, and that
|
|
58
|
+
* number is not a constant — a filter bar wraps onto a second line, a sidebar collapses, a
|
|
59
|
+
* notification appears, the font finally loads.
|
|
60
|
+
*
|
|
61
|
+
* Render the returned component where the sized content goes, and use the numbers:
|
|
62
|
+
*
|
|
63
|
+
* ```tsx
|
|
64
|
+
* const [ AutoSizer, { height } ] = useAutoSizer({ minHeight: 240 });
|
|
65
|
+
*
|
|
66
|
+
* return (
|
|
67
|
+
* <AutoSizer>
|
|
68
|
+
* <VirtualizedTable height={height} rows={rows} />
|
|
69
|
+
* </AutoSizer>
|
|
70
|
+
* );
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* By default each axis answers with **the room left between the element and the far edge of the
|
|
74
|
+
* viewport**, minus what the ancestors still need for their own padding, margin and border. Switch an
|
|
75
|
+
* axis to `useOwnHeight` / `useOwnWidth` to measure the element itself instead, or pin it with
|
|
76
|
+
* `fixedHeight` / `fixedWidth`.
|
|
77
|
+
*
|
|
78
|
+
* What it watches: the element and its parent through a `ResizeObserver`, the viewport through a
|
|
79
|
+
* resize listener, and its own visibility through an `IntersectionObserver` — so a tab that was
|
|
80
|
+
* hidden when it mounted measures itself the moment it is shown, instead of reporting zero until the
|
|
81
|
+
* next window resize. Measurements are coalesced into one animation frame, because a resize observer
|
|
82
|
+
* fires several times per frame while a layout settles.
|
|
83
|
+
*
|
|
84
|
+
* @param options - See `UseAutoSizerOptions`. Read through a ref, so changing them mid-life is safe
|
|
85
|
+
* and never re-subscribes the observers.
|
|
86
|
+
*/
|
|
87
|
+
function useAutoSizer(options) {
|
|
88
|
+
// ----
|
|
89
|
+
// Internal State
|
|
90
|
+
// ----
|
|
91
|
+
/**
|
|
92
|
+
* The element lives in state, not only in a ref: the observers must be attached when it mounts,
|
|
93
|
+
* and an effect cannot depend on a ref's content.
|
|
94
|
+
*/
|
|
95
|
+
const [element, setElement] = React.useState(null);
|
|
96
|
+
const [size, setSize] = React.useState(() => ({
|
|
97
|
+
height: options?.fixedHeight ?? 0,
|
|
98
|
+
width: options?.fixedWidth ?? 0
|
|
99
|
+
}));
|
|
100
|
+
// ----
|
|
101
|
+
// Internal Hooks
|
|
102
|
+
// ----
|
|
103
|
+
const optionsRef = useSyncedRef(options);
|
|
104
|
+
/** The last emitted size, so the measure function never depends on the state it writes */
|
|
105
|
+
const sizeRef = React.useRef(size);
|
|
106
|
+
const frameRef = React.useRef(null);
|
|
107
|
+
// ----
|
|
108
|
+
// Handlers
|
|
109
|
+
// ----
|
|
110
|
+
const measure = React.useCallback(() => {
|
|
111
|
+
const { disabled, fixedHeight, fixedWidth, maxHeight, maxWidth, minHeight, minWidth, useOwnHeight, useOwnWidth } = optionsRef.current ?? {};
|
|
112
|
+
if (!element || disabled) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const view = element.ownerDocument.defaultView;
|
|
116
|
+
if (!view) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const space = getAncestorsSpace(element.parentElement);
|
|
120
|
+
const rect = element.getBoundingClientRect();
|
|
121
|
+
const nextHeight = clamp(fixedHeight
|
|
122
|
+
?? (useOwnHeight
|
|
123
|
+
? element.clientHeight
|
|
124
|
+
: Math.max(view.innerHeight - rect.top - space.bottom, 0)), minHeight, maxHeight);
|
|
125
|
+
const nextWidth = clamp(fixedWidth
|
|
126
|
+
?? (useOwnWidth
|
|
127
|
+
? element.clientWidth
|
|
128
|
+
: Math.max(view.innerWidth - rect.left - space.right, 0)), minWidth, maxWidth);
|
|
129
|
+
const { current: lastSize } = sizeRef;
|
|
130
|
+
if (nextHeight === lastSize.height && nextWidth === lastSize.width) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
sizeRef.current = { height: nextHeight, width: nextWidth };
|
|
134
|
+
setSize(sizeRef.current);
|
|
135
|
+
}, [element, optionsRef]);
|
|
136
|
+
/**
|
|
137
|
+
* Coalesce into one frame. A `ResizeObserver` fires repeatedly while a layout settles, and each
|
|
138
|
+
* measurement reads the layout back — measuring once per frame is the difference between a smooth
|
|
139
|
+
* resize and a janky one.
|
|
140
|
+
*/
|
|
141
|
+
const scheduleMeasure = React.useCallback(() => {
|
|
142
|
+
const view = element?.ownerDocument.defaultView;
|
|
143
|
+
if (!view) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (frameRef.current !== null) {
|
|
147
|
+
view.cancelAnimationFrame(frameRef.current);
|
|
148
|
+
}
|
|
149
|
+
frameRef.current = view.requestAnimationFrame(() => {
|
|
150
|
+
frameRef.current = null;
|
|
151
|
+
measure();
|
|
152
|
+
});
|
|
153
|
+
}, [element, measure]);
|
|
154
|
+
// ----
|
|
155
|
+
// Lifecycle Events
|
|
156
|
+
// ----
|
|
157
|
+
React.useEffect(() => {
|
|
158
|
+
const view = element?.ownerDocument.defaultView;
|
|
159
|
+
if (!element || !view) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
/** Measure once, synchronously, so the first paint is not a frame late */
|
|
163
|
+
measure();
|
|
164
|
+
const observed = [element, element.parentElement].filter(Boolean);
|
|
165
|
+
const resizeObserver = new view.ResizeObserver(scheduleMeasure);
|
|
166
|
+
observed.forEach(target => resizeObserver.observe(target));
|
|
167
|
+
/**
|
|
168
|
+
* A hidden element has a zero rect, so measuring it produces zero. Watching its visibility is
|
|
169
|
+
* what makes a tab, an accordion or a collapsed panel measure itself the moment it appears.
|
|
170
|
+
*/
|
|
171
|
+
const intersectionObserver = new view.IntersectionObserver((entries) => {
|
|
172
|
+
if (entries.some(entry => entry.isIntersecting)) {
|
|
173
|
+
scheduleMeasure();
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
intersectionObserver.observe(element);
|
|
177
|
+
view.addEventListener('resize', scheduleMeasure);
|
|
178
|
+
view.addEventListener('orientationchange', scheduleMeasure);
|
|
179
|
+
return () => {
|
|
180
|
+
if (frameRef.current !== null) {
|
|
181
|
+
view.cancelAnimationFrame(frameRef.current);
|
|
182
|
+
frameRef.current = null;
|
|
183
|
+
}
|
|
184
|
+
resizeObserver.disconnect();
|
|
185
|
+
intersectionObserver.disconnect();
|
|
186
|
+
view.removeEventListener('resize', scheduleMeasure);
|
|
187
|
+
view.removeEventListener('orientationchange', scheduleMeasure);
|
|
188
|
+
};
|
|
189
|
+
}, [element, measure, scheduleMeasure]);
|
|
190
|
+
// ----
|
|
191
|
+
// Memoized Data
|
|
192
|
+
// ----
|
|
193
|
+
/**
|
|
194
|
+
* One element, and a stable component identity.
|
|
195
|
+
*
|
|
196
|
+
* Both matter. The ancestor of this hook rendered *two* divs — a zero-height "viewport detector"
|
|
197
|
+
* plus the measured one — and spread the caller's props onto both, so a `className` with padding
|
|
198
|
+
* was applied twice. And a component whose identity changes remounts its whole subtree, which for
|
|
199
|
+
* a virtualized table means losing its scroll position on every resize.
|
|
200
|
+
*/
|
|
201
|
+
const AutoSizer = React.useMemo(() => {
|
|
202
|
+
const Component = React.forwardRef((props, forwardedRef) => {
|
|
203
|
+
const handleRef = React.useCallback((node) => {
|
|
204
|
+
setElement(node);
|
|
205
|
+
if (typeof forwardedRef === 'function') {
|
|
206
|
+
forwardedRef(node);
|
|
207
|
+
}
|
|
208
|
+
else if (forwardedRef) {
|
|
209
|
+
forwardedRef.current = node;
|
|
210
|
+
}
|
|
211
|
+
}, [forwardedRef]);
|
|
212
|
+
return jsx("div", { ...props, ref: handleRef });
|
|
213
|
+
});
|
|
214
|
+
Component.displayName = 'AutoSizer';
|
|
215
|
+
return Component;
|
|
216
|
+
}, []);
|
|
217
|
+
// ----
|
|
218
|
+
// Hook Return
|
|
219
|
+
// ----
|
|
220
|
+
return [AutoSizer, size];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export { getAncestorsSpace, useAutoSizer };
|
|
224
|
+
//# sourceMappingURL=useAutoSizer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAutoSizer.js","sources":["../../../src/hooks/useAutoSizer.tsx"],"sourcesContent":[null],"names":["_jsx"],"mappings":";;;;AA8DA;;AAEc;AAEd;;;;;;AAMG;AACH,SAAS,aAAa,CAAC,OAAmC,EAAA;AACxD,IAAA,MAAM,IAAI,GAAG,OAAO,EAAE,aAAa,EAAE,WAAW;IAEhD,OAAO,CAAC,CAAC,IAAI,IAAI,OAAO,YAAY,IAAI,CAAC,WAAW;AACtD;AAGA;;;;;;;;;;;;AAYG;AACG,SAAU,iBAAiB,CAAC,OAA2B,EAAA;IAC3D,MAAM,KAAK,GAAmB,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE;IAErD,IAAI,OAAO,GAAuB,OAAO;AAEzC,IAAA,OAAO,aAAa,CAAC,OAAO,CAAC,EAAE;AAC7B,QAAA,MAAM,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,WAAY,CAAC,gBAAgB,CAAC,OAAO,CAAC;AAE3E,QAAA,KAAK,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC;eAChD,UAAU,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC;eACrC,UAAU,CAAC,MAAM,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;AAE/C,QAAA,KAAK,C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AAI;AAC3C,QAAA,CAAC,CACF;AAED,QAAA,SAAS,CAAC,WAAW,GAAG,WAAW;AAEnC,QAAA,OAAO,SAAS;IAClB,CAAC,EACD,EAAE,CACH;;;;AAMD,IAAA,OAAO,CAAE,SAAS,EAAE,IAAI,CAAE;AAE5B;;;;"}
|
package/esm/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { default as RenderWhen } from './components/RenderWhen/RenderWhen.js';
|
|
2
2
|
export { useAutoControlledState } from './hooks/useAutoControlledState.js';
|
|
3
|
+
export { getAncestorsSpace, useAutoSizer } from './hooks/useAutoSizer.js';
|
|
3
4
|
export { useClickOutside } from './hooks/useClickOutside.js';
|
|
4
5
|
export { useDataManager } from './hooks/useDataManager.js';
|
|
5
6
|
export { useDataSelector } from './hooks/useDataSelector.js';
|
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@proedis/react",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "A set of React Hooks, that add UI or Core functionality to your Apps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"proedis",
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"main": "./cjs/index.js",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@proedis/types": "^2.
|
|
14
|
-
"@proedis/utils": "^2.
|
|
13
|
+
"@proedis/types": "^2.1.0",
|
|
14
|
+
"@proedis/utils": "^2.1.0",
|
|
15
15
|
"clsx": "^2.1.1",
|
|
16
16
|
"kind-of": "^6.0.3"
|
|
17
17
|
},
|
package/types/hooks/index.d.ts
CHANGED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
export interface UseAutoSizerOptions {
|
|
3
|
+
/** Stop measuring, keeping the last size. For a panel that is collapsed, or a tab that is hidden */
|
|
4
|
+
disabled?: boolean;
|
|
5
|
+
/** Skip the height computation entirely and answer with this value */
|
|
6
|
+
fixedHeight?: number;
|
|
7
|
+
/** Skip the width computation entirely and answer with this value */
|
|
8
|
+
fixedWidth?: number;
|
|
9
|
+
/** Never grow taller than this */
|
|
10
|
+
maxHeight?: number;
|
|
11
|
+
/** Never grow wider than this */
|
|
12
|
+
maxWidth?: number;
|
|
13
|
+
/** Never shrink shorter than this */
|
|
14
|
+
minHeight?: number;
|
|
15
|
+
/** Never shrink narrower than this */
|
|
16
|
+
minWidth?: number;
|
|
17
|
+
/** Measure the element's own height instead of the room left below it */
|
|
18
|
+
useOwnHeight?: boolean;
|
|
19
|
+
/** Measure the element's own width instead of the room left beside it */
|
|
20
|
+
useOwnWidth?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface AutoSizerSize {
|
|
23
|
+
height: number;
|
|
24
|
+
width: number;
|
|
25
|
+
}
|
|
26
|
+
/** The measurement anchor: a plain `div` that reports the room around it. */
|
|
27
|
+
export type AutoSizerComponent = React.ForwardRefExoticComponent<React.PropsWithoutRef<React.ComponentProps<'div'>> & React.RefAttributes<HTMLDivElement>>;
|
|
28
|
+
export type UseAutoSizerReturn = [AutoSizerComponent, AutoSizerSize];
|
|
29
|
+
/** The room the ancestors still need after the element, on each axis */
|
|
30
|
+
export interface AncestorsSpace {
|
|
31
|
+
bottom: number;
|
|
32
|
+
right: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Sum the space every ancestor still needs *after* the element, walking up to the document.
|
|
36
|
+
*
|
|
37
|
+
* This is what makes the hook worth having. An element's `getBoundingClientRect().top` says where it
|
|
38
|
+
* starts; the room left below it is the rest of the viewport **minus** the bottom padding, margin and
|
|
39
|
+
* border of each ancestor — otherwise a table filling "the rest of the screen" overflows its card by
|
|
40
|
+
* exactly the card's own padding, and the page grows a scrollbar nobody asked for.
|
|
41
|
+
*
|
|
42
|
+
* Exported because it is the useful half on its own: anything sizing an element it does not own — a
|
|
43
|
+
* canvas, a third-party widget — needs the same number and has no reason to reimplement the walk.
|
|
44
|
+
*
|
|
45
|
+
* @param element - Where to start. Usually the parent of the element being sized.
|
|
46
|
+
*/
|
|
47
|
+
export declare function getAncestorsSpace(element: HTMLElement | null): AncestorsSpace;
|
|
48
|
+
/**
|
|
49
|
+
* Measure the room an element has, and keep measuring it.
|
|
50
|
+
*
|
|
51
|
+
* The problem it replaces is `height: calc(100vh - 320px)`: a virtualized table, a map or a chart
|
|
52
|
+
* needs a pixel height, the height available depends on everything rendered above it, and that
|
|
53
|
+
* number is not a constant — a filter bar wraps onto a second line, a sidebar collapses, a
|
|
54
|
+
* notification appears, the font finally loads.
|
|
55
|
+
*
|
|
56
|
+
* Render the returned component where the sized content goes, and use the numbers:
|
|
57
|
+
*
|
|
58
|
+
* ```tsx
|
|
59
|
+
* const [ AutoSizer, { height } ] = useAutoSizer({ minHeight: 240 });
|
|
60
|
+
*
|
|
61
|
+
* return (
|
|
62
|
+
* <AutoSizer>
|
|
63
|
+
* <VirtualizedTable height={height} rows={rows} />
|
|
64
|
+
* </AutoSizer>
|
|
65
|
+
* );
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* By default each axis answers with **the room left between the element and the far edge of the
|
|
69
|
+
* viewport**, minus what the ancestors still need for their own padding, margin and border. Switch an
|
|
70
|
+
* axis to `useOwnHeight` / `useOwnWidth` to measure the element itself instead, or pin it with
|
|
71
|
+
* `fixedHeight` / `fixedWidth`.
|
|
72
|
+
*
|
|
73
|
+
* What it watches: the element and its parent through a `ResizeObserver`, the viewport through a
|
|
74
|
+
* resize listener, and its own visibility through an `IntersectionObserver` — so a tab that was
|
|
75
|
+
* hidden when it mounted measures itself the moment it is shown, instead of reporting zero until the
|
|
76
|
+
* next window resize. Measurements are coalesced into one animation frame, because a resize observer
|
|
77
|
+
* fires several times per frame while a layout settles.
|
|
78
|
+
*
|
|
79
|
+
* @param options - See `UseAutoSizerOptions`. Read through a ref, so changing them mid-life is safe
|
|
80
|
+
* and never re-subscribes the observers.
|
|
81
|
+
*/
|
|
82
|
+
export declare function useAutoSizer(options?: UseAutoSizerOptions): UseAutoSizerReturn;
|