@wallarm-org/design-system 0.53.0 → 0.53.1
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/dist/hooks/useOverflowItems.d.ts +2 -4
- package/dist/hooks/useOverflowItems.js +56 -38
- package/dist/hooks/useOverflowItems.observer.d.ts +12 -0
- package/dist/hooks/useOverflowItems.observer.js +14 -0
- package/dist/hooks/useOverflowItems.scheduler.d.ts +8 -3
- package/dist/metadata/components.json +2 -2
- package/package.json +1 -1
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { type ReactElement, type RefObject } from 'react';
|
|
2
2
|
export interface UseOverflowItemsOptions<T> {
|
|
3
3
|
/**
|
|
4
|
-
* Invariant:
|
|
5
|
-
*
|
|
6
|
-
* renderers/reserveSpace, so changing those to alter widths needs a fresh
|
|
7
|
-
* array.
|
|
4
|
+
* Invariant: measurements are cached by array identity, so changing the
|
|
5
|
+
* renderers/reserveSpace to alter widths needs a fresh array.
|
|
8
6
|
*/
|
|
9
7
|
items: T[];
|
|
10
8
|
renderItem: (item: T) => ReactElement;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useCallback, useLayoutEffect, useRef, useState } from "react";
|
|
3
3
|
import { calculateVisibleCount } from "./useOverflowItems.helpers.js";
|
|
4
|
+
import { observeOverflowResize } from "./useOverflowItems.observer.js";
|
|
4
5
|
import { scheduleOverflowMeasurement } from "./useOverflowItems.scheduler.js";
|
|
5
6
|
const crossMountCache = new WeakMap();
|
|
6
7
|
function useOverflowItems({ items, renderItem, renderMeasurementItem, overflowRenderer, reserveSpace = 60 }) {
|
|
@@ -18,87 +19,104 @@ function useOverflowItems({ items, renderItem, renderMeasurementItem, overflowRe
|
|
|
18
19
|
const cacheRef = useRef({
|
|
19
20
|
widths: [],
|
|
20
21
|
gap: 0,
|
|
21
|
-
indicatorWidth: reserveSpace
|
|
22
|
+
indicatorWidth: reserveSpace,
|
|
23
|
+
lastCount: 0
|
|
22
24
|
});
|
|
23
25
|
const recompute = useCallback((availableWidth)=>{
|
|
24
26
|
const container = containerRef.current;
|
|
25
27
|
if (!container) return;
|
|
28
|
+
const width = availableWidth ?? container.offsetWidth;
|
|
29
|
+
if (0 === width) return;
|
|
26
30
|
const { widths, gap, indicatorWidth } = cacheRef.current;
|
|
27
31
|
if (0 === widths.length) return;
|
|
28
32
|
const next = calculateVisibleCount({
|
|
29
33
|
itemWidths: widths,
|
|
30
34
|
gap,
|
|
31
|
-
availableWidth:
|
|
35
|
+
availableWidth: width,
|
|
32
36
|
indicatorWidth
|
|
33
37
|
});
|
|
34
38
|
const entry = crossMountCache.get(itemsRef.current);
|
|
35
39
|
if (entry) entry.lastCount = next;
|
|
36
40
|
setVisibleCount((prev)=>prev === next ? prev : next);
|
|
37
41
|
}, []);
|
|
42
|
+
const pendingMeasureRef = useRef(false);
|
|
43
|
+
const measure = useCallback(()=>{
|
|
44
|
+
const container = containerRef.current;
|
|
45
|
+
if (container && 0 === container.offsetWidth) return ()=>{
|
|
46
|
+
pendingMeasureRef.current = true;
|
|
47
|
+
};
|
|
48
|
+
const gap = container ? Number.parseFloat(getComputedStyle(container).gap || '0') || 0 : 0;
|
|
49
|
+
const widths = measurementRefs.current.slice(0, items.length).map((ref)=>ref?.offsetWidth ?? 0);
|
|
50
|
+
const indicatorWidth = indicatorRef.current?.offsetWidth || reserveSpace;
|
|
51
|
+
const availableWidth = container?.offsetWidth ?? 0;
|
|
52
|
+
return ()=>{
|
|
53
|
+
pendingMeasureRef.current = false;
|
|
54
|
+
const entry = {
|
|
55
|
+
widths,
|
|
56
|
+
gap,
|
|
57
|
+
indicatorWidth,
|
|
58
|
+
lastCount: items.length
|
|
59
|
+
};
|
|
60
|
+
cacheRef.current = entry;
|
|
61
|
+
crossMountCache.set(items, entry);
|
|
62
|
+
recompute(availableWidth);
|
|
63
|
+
measurementLayerRef.current?.style.setProperty('display', 'none');
|
|
64
|
+
};
|
|
65
|
+
}, [
|
|
66
|
+
items,
|
|
67
|
+
reserveSpace,
|
|
68
|
+
recompute
|
|
69
|
+
]);
|
|
70
|
+
const measureRef = useRef(measure);
|
|
71
|
+
measureRef.current = measure;
|
|
72
|
+
const recomputeRead = useCallback(()=>{
|
|
73
|
+
const availableWidth = containerRef.current?.offsetWidth ?? 0;
|
|
74
|
+
return ()=>recompute(availableWidth);
|
|
75
|
+
}, [
|
|
76
|
+
recompute
|
|
77
|
+
]);
|
|
38
78
|
useLayoutEffect(()=>{
|
|
39
79
|
if (0 === items.length) {
|
|
40
80
|
cacheRef.current = {
|
|
41
81
|
widths: [],
|
|
42
82
|
gap: 0,
|
|
43
|
-
indicatorWidth: reserveSpace
|
|
83
|
+
indicatorWidth: reserveSpace,
|
|
84
|
+
lastCount: 0
|
|
44
85
|
};
|
|
45
86
|
setVisibleCount(0);
|
|
87
|
+
pendingMeasureRef.current = false;
|
|
46
88
|
return;
|
|
47
89
|
}
|
|
48
90
|
const cached = crossMountCache.get(items);
|
|
49
91
|
if (cached) {
|
|
50
92
|
cacheRef.current = cached;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return ()=>recompute(availableWidth);
|
|
54
|
-
});
|
|
93
|
+
pendingMeasureRef.current = false;
|
|
94
|
+
return scheduleOverflowMeasurement(recomputeRead);
|
|
55
95
|
}
|
|
56
96
|
measurementLayerRef.current?.style.removeProperty('display');
|
|
57
|
-
return scheduleOverflowMeasurement(
|
|
58
|
-
const container = containerRef.current;
|
|
59
|
-
const gap = container ? Number.parseFloat(getComputedStyle(container).gap || '0') || 0 : 0;
|
|
60
|
-
const widths = measurementRefs.current.slice(0, items.length).map((ref)=>ref?.offsetWidth ?? 0);
|
|
61
|
-
const indicatorWidth = indicatorRef.current?.offsetWidth || reserveSpace;
|
|
62
|
-
const availableWidth = container?.offsetWidth ?? 0;
|
|
63
|
-
return ()=>{
|
|
64
|
-
const entry = {
|
|
65
|
-
widths,
|
|
66
|
-
gap,
|
|
67
|
-
indicatorWidth,
|
|
68
|
-
lastCount: items.length
|
|
69
|
-
};
|
|
70
|
-
cacheRef.current = entry;
|
|
71
|
-
crossMountCache.set(items, entry);
|
|
72
|
-
recompute(availableWidth);
|
|
73
|
-
measurementLayerRef.current?.style.setProperty('display', 'none');
|
|
74
|
-
};
|
|
75
|
-
});
|
|
97
|
+
return scheduleOverflowMeasurement(measure);
|
|
76
98
|
}, [
|
|
77
99
|
items,
|
|
78
100
|
renderItem,
|
|
79
101
|
renderMeasurementItem,
|
|
80
102
|
overflowRenderer,
|
|
81
103
|
reserveSpace,
|
|
82
|
-
|
|
104
|
+
recomputeRead,
|
|
105
|
+
measure
|
|
83
106
|
]);
|
|
84
107
|
useLayoutEffect(()=>{
|
|
85
108
|
const container = containerRef.current;
|
|
86
109
|
if (!container) return;
|
|
87
|
-
let
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
frame = requestAnimationFrame(()=>{
|
|
91
|
-
frame = 0;
|
|
92
|
-
recompute();
|
|
93
|
-
});
|
|
110
|
+
let cancelScheduled;
|
|
111
|
+
const unobserve = observeOverflowResize(container, ()=>{
|
|
112
|
+
cancelScheduled = pendingMeasureRef.current ? scheduleOverflowMeasurement(measureRef.current) : scheduleOverflowMeasurement(recomputeRead);
|
|
94
113
|
});
|
|
95
|
-
observer.observe(container);
|
|
96
114
|
return ()=>{
|
|
97
|
-
|
|
98
|
-
|
|
115
|
+
cancelScheduled?.();
|
|
116
|
+
unobserve();
|
|
99
117
|
};
|
|
100
118
|
}, [
|
|
101
|
-
|
|
119
|
+
recomputeRead
|
|
102
120
|
]);
|
|
103
121
|
const visibleItems = items.slice(0, visibleCount);
|
|
104
122
|
const hiddenItems = items.slice(visibleCount);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single shared ResizeObserver for all overflow lists. The browser drains
|
|
3
|
+
* microtasks between separate observer callbacks, so per-instance observers
|
|
4
|
+
* cannot share one read/write batch — one observer delivers every entry in a
|
|
5
|
+
* single callback, letting all instances enqueue into the same flush. It also
|
|
6
|
+
* fires pre-paint: a measurement scheduled here lands before the frame paints.
|
|
7
|
+
*/
|
|
8
|
+
/** Called when the observed element resizes (including display:none toggles). */
|
|
9
|
+
export interface OverflowResizeHandler {
|
|
10
|
+
(): void;
|
|
11
|
+
}
|
|
12
|
+
export declare const observeOverflowResize: (element: Element, handler: OverflowResizeHandler) => (() => void);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const handlers = new Map();
|
|
2
|
+
let observer = null;
|
|
3
|
+
const observeOverflowResize = (element, handler)=>{
|
|
4
|
+
observer ??= new ResizeObserver((entries)=>{
|
|
5
|
+
for (const entry of entries)handlers.get(entry.target)?.();
|
|
6
|
+
});
|
|
7
|
+
handlers.set(element, handler);
|
|
8
|
+
observer.observe(element);
|
|
9
|
+
return ()=>{
|
|
10
|
+
handlers.delete(element);
|
|
11
|
+
observer?.unobserve(element);
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export { observeOverflowResize };
|
|
@@ -5,12 +5,17 @@
|
|
|
5
5
|
* the next reads `offsetWidth`. One pre-paint microtask runs all reads against
|
|
6
6
|
* clean layout, then all writes — React batches them, no interleaving.
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/** Write phase of a batched measurement: state + style writes, no DOM reads. */
|
|
9
|
+
export interface WritePhase {
|
|
10
|
+
(): void;
|
|
11
|
+
}
|
|
12
|
+
/** Read phase: DOM reads only; returns the write to run after all reads. */
|
|
13
|
+
export interface ReadPhase {
|
|
14
|
+
(): WritePhase;
|
|
15
|
+
}
|
|
10
16
|
/**
|
|
11
17
|
* Queue a measurement for the next pre-paint flush. Returns a cancel
|
|
12
18
|
* function — call it on effect cleanup so unmounted or re-rendered
|
|
13
19
|
* instances never measure stale refs.
|
|
14
20
|
*/
|
|
15
21
|
export declare const scheduleOverflowMeasurement: (read: ReadPhase) => (() => void);
|
|
16
|
-
export {};
|