@wordpress/grid 0.9.0 → 0.11.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 +35 -14
- package/build/dashboard-grid/grid-item.cjs +232 -295
- package/build/dashboard-grid/grid-item.cjs.map +1 -7
- package/build/dashboard-grid/index.cjs +525 -632
- package/build/dashboard-grid/index.cjs.map +1 -7
- package/build/dashboard-grid/resolve-fill-widths.cjs +159 -203
- package/build/dashboard-grid/resolve-fill-widths.cjs.map +1 -7
- package/build/dashboard-grid/types.cjs +4 -18
- package/build/dashboard-lanes/index.cjs +494 -576
- package/build/dashboard-lanes/index.cjs.map +1 -7
- package/build/dashboard-lanes/lane-placement.cjs +77 -100
- package/build/dashboard-lanes/lane-placement.cjs.map +1 -7
- package/build/dashboard-lanes/lanes-item.cjs +222 -278
- package/build/dashboard-lanes/lanes-item.cjs.map +1 -7
- package/build/dashboard-lanes/types.cjs +4 -18
- package/build/dashboard-lanes/use-lane-placement.cjs +172 -198
- package/build/dashboard-lanes/use-lane-placement.cjs.map +1 -7
- package/build/index.cjs +16 -31
- package/build/shared/array-move-with-pinned.cjs +29 -45
- package/build/shared/array-move-with-pinned.cjs.map +1 -7
- package/build/shared/column-policy.cjs +58 -0
- package/build/shared/column-policy.cjs.map +1 -0
- package/build/shared/drag-overlay-drop-animation.cjs +40 -66
- package/build/shared/drag-overlay-drop-animation.cjs.map +1 -7
- package/build/shared/grid-item-key.cjs +12 -29
- package/build/shared/grid-item-key.cjs.map +1 -7
- package/build/shared/grid-overlay.cjs +163 -164
- package/build/shared/grid-overlay.cjs.map +1 -7
- package/build/shared/item-exit-overlay.cjs +132 -134
- package/build/shared/item-exit-overlay.cjs.map +1 -7
- package/build/shared/resize-handle.cjs +213 -197
- package/build/shared/resize-handle.cjs.map +1 -7
- package/build/shared/resize-snap.cjs +116 -121
- package/build/shared/resize-snap.cjs.map +1 -7
- package/build/shared/types.cjs +4 -18
- package/build/shared/use-item-exit-animation.cjs +96 -143
- package/build/shared/use-item-exit-animation.cjs.map +1 -7
- package/build/shared/use-layout-shift-animation.cjs +121 -150
- package/build/shared/use-layout-shift-animation.cjs.map +1 -7
- package/build/shared/use-span-bounds.cjs +65 -79
- package/build/shared/use-span-bounds.cjs.map +1 -7
- package/build-module/dashboard-grid/grid-item.mjs +209 -265
- package/build-module/dashboard-grid/grid-item.mjs.map +1 -7
- package/build-module/dashboard-grid/index.mjs +502 -623
- package/build-module/dashboard-grid/index.mjs.map +1 -7
- package/build-module/dashboard-grid/resolve-fill-widths.mjs +155 -179
- package/build-module/dashboard-grid/resolve-fill-widths.mjs.map +1 -7
- package/build-module/dashboard-grid/types.mjs +0 -1
- package/build-module/dashboard-lanes/index.mjs +470 -567
- package/build-module/dashboard-lanes/index.mjs.map +1 -7
- package/build-module/dashboard-lanes/lane-placement.mjs +73 -76
- package/build-module/dashboard-lanes/lane-placement.mjs.map +1 -7
- package/build-module/dashboard-lanes/lanes-item.mjs +199 -248
- package/build-module/dashboard-lanes/lanes-item.mjs.map +1 -7
- package/build-module/dashboard-lanes/types.mjs +0 -1
- package/build-module/dashboard-lanes/use-lane-placement.mjs +167 -173
- package/build-module/dashboard-lanes/use-lane-placement.mjs.map +1 -7
- package/build-module/index.mjs +1 -6
- package/build-module/shared/array-move-with-pinned.mjs +25 -21
- package/build-module/shared/array-move-with-pinned.mjs.map +1 -7
- package/build-module/shared/column-policy.mjs +52 -0
- package/build-module/shared/column-policy.mjs.map +1 -0
- package/build-module/shared/drag-overlay-drop-animation.mjs +35 -44
- package/build-module/shared/drag-overlay-drop-animation.mjs.map +1 -7
- package/build-module/shared/grid-item-key.mjs +8 -5
- package/build-module/shared/grid-item-key.mjs.map +1 -7
- package/build-module/shared/grid-overlay.mjs +143 -136
- package/build-module/shared/grid-overlay.mjs.map +1 -7
- package/build-module/shared/item-exit-overlay.mjs +128 -110
- package/build-module/shared/item-exit-overlay.mjs.map +1 -7
- package/build-module/shared/resize-handle.mjs +191 -171
- package/build-module/shared/resize-handle.mjs.map +1 -7
- package/build-module/shared/resize-snap.mjs +108 -93
- package/build-module/shared/resize-snap.mjs.map +1 -7
- package/build-module/shared/types.mjs +0 -1
- package/build-module/shared/use-item-exit-animation.mjs +92 -124
- package/build-module/shared/use-item-exit-animation.mjs.map +1 -7
- package/build-module/shared/use-layout-shift-animation.mjs +114 -123
- package/build-module/shared/use-layout-shift-animation.mjs.map +1 -7
- package/build-module/shared/use-span-bounds.mjs +60 -57
- package/build-module/shared/use-span-bounds.mjs.map +1 -7
- package/build-types/dashboard-grid/index.d.ts.map +1 -1
- package/build-types/dashboard-grid/types.d.ts +9 -5
- package/build-types/dashboard-grid/types.d.ts.map +1 -1
- package/build-types/dashboard-lanes/index.d.ts.map +1 -1
- package/build-types/dashboard-lanes/types.d.ts +9 -5
- package/build-types/dashboard-lanes/types.d.ts.map +1 -1
- package/build-types/index.d.ts +1 -0
- package/build-types/index.d.ts.map +1 -1
- package/build-types/shared/column-policy.d.ts +43 -0
- package/build-types/shared/column-policy.d.ts.map +1 -0
- package/build-types/shared/drag-overlay-drop-animation.d.ts +2 -2
- package/build-types/shared/use-item-exit-animation.d.ts +1 -1
- package/build-types/shared/use-item-exit-animation.d.ts.map +1 -1
- package/package.json +13 -14
- package/src/dashboard-grid/grid-item.tsx +1 -1
- package/src/dashboard-grid/index.tsx +11 -12
- package/src/dashboard-grid/stories/index.story.tsx +129 -50
- package/src/dashboard-grid/test/item-interactions.browser.test.tsx +1 -1
- package/src/dashboard-grid/types.ts +9 -5
- package/src/dashboard-lanes/index.tsx +11 -11
- package/src/dashboard-lanes/lanes-item.tsx +1 -1
- package/src/dashboard-lanes/stories/index.story.tsx +124 -28
- package/src/dashboard-lanes/test/item-interactions.browser.test.tsx +1 -1
- package/src/dashboard-lanes/types.ts +9 -5
- package/src/index.ts +1 -0
- package/src/shared/column-policy.ts +100 -0
- package/src/shared/drag-overlay-drop-animation.ts +2 -2
- package/src/shared/test/column-policy.test.ts +215 -0
- package/src/shared/test/sortable-semantics.browser.test.tsx +122 -0
- package/src/shared/use-item-exit-animation.ts +1 -3
- package/CHANGELOG.md +0 -93
- package/build/dashboard-grid/types.cjs.map +0 -7
- package/build/dashboard-lanes/types.cjs.map +0 -7
- package/build/index.cjs.map +0 -7
- package/build/shared/types.cjs.map +0 -7
- package/build-module/dashboard-grid/types.mjs.map +0 -7
- package/build-module/dashboard-lanes/types.mjs.map +0 -7
- package/build-module/index.mjs.map +0 -7
- package/build-module/shared/types.mjs.map +0 -7
|
@@ -1,181 +1,175 @@
|
|
|
1
|
-
|
|
2
|
-
import { useState, useLayoutEffect, useMemo } from "@wordpress/element";
|
|
1
|
+
import { useLayoutEffect, useMemo, useState } from "@wordpress/element";
|
|
3
2
|
import { computeLanePlacements } from "./lane-placement.mjs";
|
|
4
3
|
import { GRID_ITEM_DATA_KEY } from "../shared/grid-item-key.mjs";
|
|
5
|
-
|
|
4
|
+
// packages/grid/src/dashboard-lanes/use-lane-placement.ts
|
|
5
|
+
const DEFAULT_ROW_UNIT = 4;
|
|
6
6
|
function supportsGridLanes() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
return CSS.supports("display", "grid-lanes");
|
|
7
|
+
if (typeof CSS === "undefined" || !CSS.supports) return false;
|
|
8
|
+
return CSS.supports("display", "grid-lanes");
|
|
11
9
|
}
|
|
12
10
|
function clampSpan(span) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
return Math.max(1, Math.floor(span));
|
|
11
|
+
if (typeof span !== "number" || !Number.isFinite(span)) return 1;
|
|
12
|
+
return Math.max(1, Math.floor(span));
|
|
17
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Hook that measures item heights and resolves their placement when
|
|
16
|
+
* `display: grid-lanes` is unavailable, falling through to a no-op
|
|
17
|
+
* pass when the host browser supports the feature natively.
|
|
18
|
+
*
|
|
19
|
+
* Usage from the renderer:
|
|
20
|
+
*
|
|
21
|
+
* ```tsx
|
|
22
|
+
* const [ container, setContainer ] = useState< HTMLDivElement | null >( null );
|
|
23
|
+
* const { itemStyles } = useLanePlacement( container, {
|
|
24
|
+
* items: layout,
|
|
25
|
+
* lanes: columns,
|
|
26
|
+
* gap: gapPx,
|
|
27
|
+
* flowTolerance: 16,
|
|
28
|
+
* } );
|
|
29
|
+
*
|
|
30
|
+
* return (
|
|
31
|
+
* <div ref={ setContainer } style={ { display: 'grid-lanes', ... } }>
|
|
32
|
+
* { items.map( ( item ) => (
|
|
33
|
+
* <div
|
|
34
|
+
* key={ item.key }
|
|
35
|
+
* data-wp-grid-item-key={ item.key }
|
|
36
|
+
* style={ itemStyles.get( item.key ) }
|
|
37
|
+
* >
|
|
38
|
+
* { ... }
|
|
39
|
+
* </div>
|
|
40
|
+
* ) ) }
|
|
41
|
+
* </div>
|
|
42
|
+
* );
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @param container HTMLElement (or null pre-mount) hosting the items.
|
|
46
|
+
* @param input Logical items, lane count, gap, and tuning.
|
|
47
|
+
* @return Per-item styles plus the `isPolyfilled` flag.
|
|
48
|
+
*/
|
|
18
49
|
function useLanePlacement(container, input) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
mutationObserver.observe(container, {
|
|
142
|
-
childList: true,
|
|
143
|
-
subtree: true,
|
|
144
|
-
attributes: true,
|
|
145
|
-
attributeFilter: [GRID_ITEM_DATA_KEY]
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
refreshObserved();
|
|
149
|
-
recompute();
|
|
150
|
-
return () => {
|
|
151
|
-
cancelled = true;
|
|
152
|
-
if (rafId !== null) {
|
|
153
|
-
cancelAnimationFrame(rafId);
|
|
154
|
-
}
|
|
155
|
-
resizeObserver.disconnect();
|
|
156
|
-
mutationObserver?.disconnect();
|
|
157
|
-
};
|
|
158
|
-
}, [
|
|
159
|
-
container,
|
|
160
|
-
isPolyfilled,
|
|
161
|
-
lanes,
|
|
162
|
-
gap,
|
|
163
|
-
flowTolerance,
|
|
164
|
-
rowUnit,
|
|
165
|
-
itemsForPlacement
|
|
166
|
-
]);
|
|
167
|
-
if (!isPolyfilled) {
|
|
168
|
-
return { itemStyles: nativeStyles, isPolyfilled: false };
|
|
169
|
-
}
|
|
170
|
-
if (itemStyles.size === 0) {
|
|
171
|
-
return {
|
|
172
|
-
itemStyles: nativeStyles,
|
|
173
|
-
isPolyfilled: true
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
return { itemStyles, isPolyfilled: true };
|
|
50
|
+
const [isPolyfilled] = useState(() => !supportsGridLanes());
|
|
51
|
+
const [itemStyles, setItemStyles] = useState(() => /* @__PURE__ */ new Map());
|
|
52
|
+
const nativeStyles = useMemo(() => {
|
|
53
|
+
const map = /* @__PURE__ */ new Map();
|
|
54
|
+
for (const item of input.items) map.set(item.key, { gridColumn: `span ${clampSpan(item.span)}` });
|
|
55
|
+
return map;
|
|
56
|
+
}, [input.items]);
|
|
57
|
+
const itemsSignature = useMemo(() => {
|
|
58
|
+
return input.items.map((item) => `${item.key}/${item.span ?? 1}/${item.lane ?? ""}`).join("\0");
|
|
59
|
+
}, [input.items]);
|
|
60
|
+
const itemsForPlacement = useMemo(() => input.items, [itemsSignature]);
|
|
61
|
+
const { lanes, gap, flowTolerance, rowUnit } = input;
|
|
62
|
+
useLayoutEffect(() => {
|
|
63
|
+
if (!isPolyfilled || !container) return;
|
|
64
|
+
if (typeof ResizeObserver === "undefined") return;
|
|
65
|
+
const heights = /* @__PURE__ */ new Map();
|
|
66
|
+
const observed = /* @__PURE__ */ new Set();
|
|
67
|
+
let cancelled = false;
|
|
68
|
+
let rafId = null;
|
|
69
|
+
const recompute = () => {
|
|
70
|
+
if (rafId !== null || cancelled) return;
|
|
71
|
+
rafId = requestAnimationFrame(() => {
|
|
72
|
+
rafId = null;
|
|
73
|
+
if (cancelled) return;
|
|
74
|
+
const itemsWithHeight = itemsForPlacement.map((item) => ({
|
|
75
|
+
key: item.key,
|
|
76
|
+
span: clampSpan(item.span),
|
|
77
|
+
lane: item.lane,
|
|
78
|
+
height: heights.get(item.key) ?? 0
|
|
79
|
+
}));
|
|
80
|
+
const result = computeLanePlacements({
|
|
81
|
+
items: itemsWithHeight,
|
|
82
|
+
lanes,
|
|
83
|
+
gap,
|
|
84
|
+
flowTolerance
|
|
85
|
+
});
|
|
86
|
+
const effectiveRowUnit = Math.max(1, rowUnit ?? DEFAULT_ROW_UNIT);
|
|
87
|
+
const next = /* @__PURE__ */ new Map();
|
|
88
|
+
for (const item of itemsForPlacement) {
|
|
89
|
+
const placement = result.placements.get(item.key);
|
|
90
|
+
if (!placement) continue;
|
|
91
|
+
const height = heights.get(item.key) ?? 0;
|
|
92
|
+
const rowStart = Math.floor(placement.top / effectiveRowUnit) + 1;
|
|
93
|
+
const rowSpan = Math.max(1, Math.ceil(height / effectiveRowUnit));
|
|
94
|
+
next.set(item.key, {
|
|
95
|
+
gridColumnStart: placement.lane + 1,
|
|
96
|
+
gridColumnEnd: `span ${placement.span}`,
|
|
97
|
+
gridRowStart: rowStart,
|
|
98
|
+
gridRowEnd: `span ${rowSpan}`
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
setItemStyles(next);
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
const resizeObserver = new ResizeObserver((entries) => {
|
|
105
|
+
let changed = false;
|
|
106
|
+
for (const entry of entries) {
|
|
107
|
+
const key = entry.target.getAttribute(GRID_ITEM_DATA_KEY);
|
|
108
|
+
if (!key) continue;
|
|
109
|
+
const newHeight = entry.contentRect.height;
|
|
110
|
+
if (heights.get(key) !== newHeight) {
|
|
111
|
+
heights.set(key, newHeight);
|
|
112
|
+
changed = true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (changed) recompute();
|
|
116
|
+
});
|
|
117
|
+
const refreshObserved = () => {
|
|
118
|
+
const current = container.querySelectorAll(`[${GRID_ITEM_DATA_KEY}]`);
|
|
119
|
+
for (const element of current) if (!observed.has(element)) {
|
|
120
|
+
observed.add(element);
|
|
121
|
+
resizeObserver.observe(element);
|
|
122
|
+
const key = element.getAttribute(GRID_ITEM_DATA_KEY);
|
|
123
|
+
if (key) {
|
|
124
|
+
const rect = element.getBoundingClientRect();
|
|
125
|
+
heights.set(key, rect.height);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
for (const element of observed) if (!container.contains(element)) {
|
|
129
|
+
resizeObserver.unobserve(element);
|
|
130
|
+
observed.delete(element);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
const mutationObserver = typeof MutationObserver !== "undefined" ? new MutationObserver(() => {
|
|
134
|
+
refreshObserved();
|
|
135
|
+
recompute();
|
|
136
|
+
}) : null;
|
|
137
|
+
if (mutationObserver) mutationObserver.observe(container, {
|
|
138
|
+
childList: true,
|
|
139
|
+
subtree: true,
|
|
140
|
+
attributes: true,
|
|
141
|
+
attributeFilter: [GRID_ITEM_DATA_KEY]
|
|
142
|
+
});
|
|
143
|
+
refreshObserved();
|
|
144
|
+
recompute();
|
|
145
|
+
return () => {
|
|
146
|
+
cancelled = true;
|
|
147
|
+
if (rafId !== null) cancelAnimationFrame(rafId);
|
|
148
|
+
resizeObserver.disconnect();
|
|
149
|
+
mutationObserver?.disconnect();
|
|
150
|
+
};
|
|
151
|
+
}, [
|
|
152
|
+
container,
|
|
153
|
+
isPolyfilled,
|
|
154
|
+
lanes,
|
|
155
|
+
gap,
|
|
156
|
+
flowTolerance,
|
|
157
|
+
rowUnit,
|
|
158
|
+
itemsForPlacement
|
|
159
|
+
]);
|
|
160
|
+
if (!isPolyfilled) return {
|
|
161
|
+
itemStyles: nativeStyles,
|
|
162
|
+
isPolyfilled: false
|
|
163
|
+
};
|
|
164
|
+
if (itemStyles.size === 0) return {
|
|
165
|
+
itemStyles: nativeStyles,
|
|
166
|
+
isPolyfilled: true
|
|
167
|
+
};
|
|
168
|
+
return {
|
|
169
|
+
itemStyles,
|
|
170
|
+
isPolyfilled: true
|
|
171
|
+
};
|
|
177
172
|
}
|
|
178
|
-
export {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
//# sourceMappingURL=use-lane-placement.mjs.map
|
|
173
|
+
export { useLanePlacement };
|
|
174
|
+
|
|
175
|
+
//# sourceMappingURL=use-lane-placement.mjs.map
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/dashboard-lanes/use-lane-placement.ts"],
|
|
4
|
-
"sourcesContent": ["import { useState, useLayoutEffect, useMemo } from '@wordpress/element';\nimport { computeLanePlacements } from './lane-placement';\nimport { GRID_ITEM_DATA_KEY } from '../shared/grid-item-key';\n\nconst DEFAULT_ROW_UNIT = 4;\n\nfunction supportsGridLanes(): boolean {\n\tif ( typeof CSS === 'undefined' || ! CSS.supports ) {\n\t\treturn false;\n\t}\n\treturn CSS.supports( 'display', 'grid-lanes' );\n}\n\nfunction clampSpan( span: number | undefined ): number {\n\tif ( typeof span !== 'number' || ! Number.isFinite( span ) ) {\n\t\treturn 1;\n\t}\n\treturn Math.max( 1, Math.floor( span ) );\n}\n\n/**\n * Logical item passed to the hook. The renderer is responsible for\n * mounting a DOM node with `data-wp-grid-item-key={ item.key }` for each\n * entry; the hook will measure that node and produce inline styles.\n */\nexport type LaneItemInput = {\n\tkey: string;\n\tspan?: number;\n\tlane?: number;\n};\n\nexport type UseLanePlacementInput = {\n\titems: ReadonlyArray< LaneItemInput >;\n\tlanes: number;\n\tgap: number;\n\tflowTolerance: number;\n\t/**\n\t * Snap unit for `grid-row-start` / `grid-row-end: span N` math.\n\t * Smaller values produce sharper placement at the cost of more\n\t * implicit rows. Defaults to 4 (px).\n\t */\n\trowUnit?: number;\n};\n\nexport type UseLanePlacementResult = {\n\t/**\n\t * Inline styles to apply to each item, keyed by item key. On\n\t * native (`display: grid-lanes`), entries carry only\n\t * `gridColumn: span N`; the browser handles row placement. While\n\t * polyfilling, entries also carry explicit `grid-column-start` /\n\t * `grid-row-*` values.\n\t */\n\titemStyles: Map< string, React.CSSProperties >;\n\n\t/**\n\t * `false` when the host browser supports `display: grid-lanes`\n\t * natively. The hook avoids mounting any observers in that case.\n\t */\n\tisPolyfilled: boolean;\n};\n\n/**\n * Hook that measures item heights and resolves their placement when\n * `display: grid-lanes` is unavailable, falling through to a no-op\n * pass when the host browser supports the feature natively.\n *\n * Usage from the renderer:\n *\n * ```tsx\n * const [ container, setContainer ] = useState< HTMLDivElement | null >( null );\n * const { itemStyles } = useLanePlacement( container, {\n * items: layout,\n * lanes: columns,\n * gap: gapPx,\n * flowTolerance: 16,\n * } );\n *\n * return (\n * <div ref={ setContainer } style={ { display: 'grid-lanes', ... } }>\n * { items.map( ( item ) => (\n * <div\n * key={ item.key }\n * data-wp-grid-item-key={ item.key }\n * style={ itemStyles.get( item.key ) }\n * >\n * { ... }\n * </div>\n * ) ) }\n * </div>\n * );\n * ```\n *\n * @param container HTMLElement (or null pre-mount) hosting the items.\n * @param input Logical items, lane count, gap, and tuning.\n * @return Per-item styles plus the `isPolyfilled` flag.\n */\nexport function useLanePlacement(\n\tcontainer: HTMLElement | null,\n\tinput: UseLanePlacementInput\n): UseLanePlacementResult {\n\t// Detect once at mount. SSR returns `true` (CSS undefined); the\n\t// client-first render returns the real value. Either path produces\n\t// the same DOM until the polyfill effect runs (both emit\n\t// span-only styles), so there is no hydration mismatch.\n\tconst [ isPolyfilled ] = useState( () => ! supportsGridLanes() );\n\n\tconst [ itemStyles, setItemStyles ] = useState<\n\t\tMap< string, React.CSSProperties >\n\t>( () => new Map() );\n\n\t// Native pass-through: items only need their column span; the\n\t// browser handles row placement. Memoized so a stable items\n\t// array yields a stable Map identity.\n\tconst nativeStyles = useMemo( () => {\n\t\tconst map = new Map< string, React.CSSProperties >();\n\t\tfor ( const item of input.items ) {\n\t\t\tmap.set( item.key, {\n\t\t\t\tgridColumn: `span ${ clampSpan( item.span ) }`,\n\t\t\t} );\n\t\t}\n\t\treturn map;\n\t}, [ input.items ] );\n\n\t// Stable signature of items for deps. Keys, spans, and explicit\n\t// lanes are the only fields that influence observer wiring or\n\t// placement, so we hash exactly those.\n\tconst itemsSignature = useMemo( () => {\n\t\treturn input.items\n\t\t\t.map(\n\t\t\t\t( item ) =>\n\t\t\t\t\t`${ item.key }/${ item.span ?? 1 }/${ item.lane ?? '' }`\n\t\t\t)\n\t\t\t.join( '\\0' );\n\t}, [ input.items ] );\n\n\t// Stable array identity while placement-relevant fields match\n\t// `itemsSignature`, so the layout effect is not torn down on every\n\t// parent re-render that passes a fresh `items` reference.\n\t// eslint-disable-next-line react-hooks/exhaustive-deps -- `itemsSignature` encodes keys/spans/lanes; `input.items` reference often changes without placement changes.\n\tconst itemsForPlacement = useMemo( () => input.items, [ itemsSignature ] );\n\n\tconst { lanes, gap, flowTolerance, rowUnit } = input;\n\n\tuseLayoutEffect( () => {\n\t\tif ( ! isPolyfilled || ! container ) {\n\t\t\treturn;\n\t\t}\n\t\tif ( typeof ResizeObserver === 'undefined' ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst heights = new Map< string, number >();\n\t\tconst observed = new Set< Element >();\n\t\tlet cancelled = false;\n\t\tlet rafId: number | null = null;\n\n\t\tconst recompute = () => {\n\t\t\tif ( rafId !== null || cancelled ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t// One layout per frame even when ResizeObserver and\n\t\t\t// MutationObserver fire in the same tick.\n\t\t\trafId = requestAnimationFrame( () => {\n\t\t\t\trafId = null;\n\t\t\t\tif ( cancelled ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst itemsWithHeight = itemsForPlacement.map( ( item ) => ( {\n\t\t\t\t\tkey: item.key,\n\t\t\t\t\tspan: clampSpan( item.span ),\n\t\t\t\t\tlane: item.lane,\n\t\t\t\t\theight: heights.get( item.key ) ?? 0,\n\t\t\t\t} ) );\n\t\t\t\tconst result = computeLanePlacements( {\n\t\t\t\t\titems: itemsWithHeight,\n\t\t\t\t\tlanes,\n\t\t\t\t\tgap,\n\t\t\t\t\tflowTolerance,\n\t\t\t\t} );\n\t\t\t\tconst effectiveRowUnit = Math.max(\n\t\t\t\t\t1,\n\t\t\t\t\trowUnit ?? DEFAULT_ROW_UNIT\n\t\t\t\t);\n\t\t\t\tconst next = new Map< string, React.CSSProperties >();\n\t\t\t\tfor ( const item of itemsForPlacement ) {\n\t\t\t\t\tconst placement = result.placements.get( item.key );\n\t\t\t\t\tif ( ! placement ) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tconst height = heights.get( item.key ) ?? 0;\n\t\t\t\t\tconst rowStart =\n\t\t\t\t\t\tMath.floor( placement.top / effectiveRowUnit ) + 1;\n\t\t\t\t\tconst rowSpan = Math.max(\n\t\t\t\t\t\t1,\n\t\t\t\t\t\tMath.ceil( height / effectiveRowUnit )\n\t\t\t\t\t);\n\t\t\t\t\tnext.set( item.key, {\n\t\t\t\t\t\tgridColumnStart: placement.lane + 1,\n\t\t\t\t\t\tgridColumnEnd: `span ${ placement.span }`,\n\t\t\t\t\t\tgridRowStart: rowStart,\n\t\t\t\t\t\tgridRowEnd: `span ${ rowSpan }`,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\tsetItemStyles( next );\n\t\t\t} );\n\t\t};\n\n\t\tconst resizeObserver = new ResizeObserver( ( entries ) => {\n\t\t\tlet changed = false;\n\t\t\tfor ( const entry of entries ) {\n\t\t\t\tconst key = ( entry.target as HTMLElement ).getAttribute(\n\t\t\t\t\tGRID_ITEM_DATA_KEY\n\t\t\t\t);\n\t\t\t\tif ( ! key ) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tconst newHeight = entry.contentRect.height;\n\t\t\t\tif ( heights.get( key ) !== newHeight ) {\n\t\t\t\t\theights.set( key, newHeight );\n\t\t\t\t\tchanged = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif ( changed ) {\n\t\t\t\trecompute();\n\t\t\t}\n\t\t} );\n\n\t\tconst refreshObserved = () => {\n\t\t\tconst current = container.querySelectorAll(\n\t\t\t\t`[${ GRID_ITEM_DATA_KEY }]`\n\t\t\t);\n\t\t\tfor ( const element of current ) {\n\t\t\t\tif ( ! observed.has( element ) ) {\n\t\t\t\t\tobserved.add( element );\n\t\t\t\t\tresizeObserver.observe( element );\n\t\t\t\t\tconst key = element.getAttribute( GRID_ITEM_DATA_KEY );\n\t\t\t\t\tif ( key ) {\n\t\t\t\t\t\tconst rect = (\n\t\t\t\t\t\t\telement as HTMLElement\n\t\t\t\t\t\t ).getBoundingClientRect();\n\t\t\t\t\t\theights.set( key, rect.height );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor ( const element of observed ) {\n\t\t\t\tif ( ! container.contains( element ) ) {\n\t\t\t\t\tresizeObserver.unobserve( element );\n\t\t\t\t\tobserved.delete( element );\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\n\t\t// Children may mount, unmount, or change `data-wp-grid-item-key`\n\t\t// after the container exists (drag reorders, additions). The\n\t\t// mutation observer keeps the observed set in sync.\n\t\tconst mutationObserver =\n\t\t\ttypeof MutationObserver !== 'undefined'\n\t\t\t\t? new MutationObserver( () => {\n\t\t\t\t\t\trefreshObserved();\n\t\t\t\t\t\trecompute();\n\t\t\t\t\t} )\n\t\t\t\t: null;\n\t\tif ( mutationObserver ) {\n\t\t\tmutationObserver.observe( container, {\n\t\t\t\tchildList: true,\n\t\t\t\tsubtree: true,\n\t\t\t\tattributes: true,\n\t\t\t\tattributeFilter: [ GRID_ITEM_DATA_KEY ],\n\t\t\t} );\n\t\t}\n\n\t\trefreshObserved();\n\t\trecompute();\n\n\t\treturn () => {\n\t\t\tcancelled = true;\n\t\t\tif ( rafId !== null ) {\n\t\t\t\tcancelAnimationFrame( rafId );\n\t\t\t}\n\t\t\tresizeObserver.disconnect();\n\t\t\tmutationObserver?.disconnect();\n\t\t};\n\t}, [\n\t\tcontainer,\n\t\tisPolyfilled,\n\t\tlanes,\n\t\tgap,\n\t\tflowTolerance,\n\t\trowUnit,\n\t\titemsForPlacement,\n\t] );\n\n\tif ( ! isPolyfilled ) {\n\t\treturn { itemStyles: nativeStyles, isPolyfilled: false };\n\t}\n\tif ( itemStyles.size === 0 ) {\n\t\t// Pre-measurement frame: emit native-shape styles so items\n\t\t// appear in their default span rather than collapsing to 1\n\t\t// column at the top-left.\n\t\treturn {\n\t\t\titemStyles: nativeStyles as Map< string, React.CSSProperties >,\n\t\t\tisPolyfilled: true,\n\t\t};\n\t}\n\treturn { itemStyles, isPolyfilled: true };\n}\n"],
|
|
5
|
-
"mappings": ";AAAA,SAAS,UAAU,iBAAiB,eAAe;AACnD,SAAS,6BAA6B;AACtC,SAAS,0BAA0B;AAEnC,IAAM,mBAAmB;AAEzB,SAAS,oBAA6B;AACrC,MAAK,OAAO,QAAQ,eAAe,CAAE,IAAI,UAAW;AACnD,WAAO;AAAA,EACR;AACA,SAAO,IAAI,SAAU,WAAW,YAAa;AAC9C;AAEA,SAAS,UAAW,MAAmC;AACtD,MAAK,OAAO,SAAS,YAAY,CAAE,OAAO,SAAU,IAAK,GAAI;AAC5D,WAAO;AAAA,EACR;AACA,SAAO,KAAK,IAAK,GAAG,KAAK,MAAO,IAAK,CAAE;AACxC;AA8EO,SAAS,iBACf,WACA,OACyB;AAKzB,QAAM,CAAE,YAAa,IAAI,SAAU,MAAM,CAAE,kBAAkB,CAAE;AAE/D,QAAM,CAAE,YAAY,aAAc,IAAI,SAEnC,MAAM,oBAAI,IAAI,CAAE;AAKnB,QAAM,eAAe,QAAS,MAAM;AACnC,UAAM,MAAM,oBAAI,IAAmC;AACnD,eAAY,QAAQ,MAAM,OAAQ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L,QACE,sBAAsB;AACzB,oBAAQ,IAAK,KAAK,KAAK,MAAO;AAAA,UAC/B;AAAA,QACD;AAAA,MACD;AACA,iBAAY,WAAW,UAAW;AACjC,YAAK,CAAE,UAAU,SAAU,OAAQ,GAAI;AACtC,yBAAe,UAAW,OAAQ;AAClC,mBAAS,OAAQ,OAAQ;AAAA,QAC1B;AAAA,MACD;AAAA,IACD;AAKA,UAAM,mBACL,OAAO,qBAAqB,cACzB,IAAI,iBAAkB,MAAM;AAC5B,sBAAgB;AAChB,gBAAU;AAAA,IACX,CAAE,IACD;AACJ,QAAK,kBAAmB;AACvB,uBAAiB,QAAS,WAAW;AAAA,QACpC,WAAW;AAAA,QACX,SAAS;AAAA,QACT,YAAY;AAAA,QACZ,iBAAiB,CAAE,kBAAmB;AAAA,MACvC,CAAE;AAAA,IACH;AAEA,oBAAgB;AAChB,cAAU;AAEV,WAAO,MAAM;AACZ,kBAAY;AACZ,UAAK,UAAU,MAAO;AACrB,6BAAsB,KAAM;AAAA,MAC7B;AACA,qBAAe,WAAW;AAC1B,wBAAkB,WAAW;AAAA,IAC9B;AAAA,EACD,GAAG;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,CAAE;AAEF,MAAK,CAAE,cAAe;AACrB,WAAO,EAAE,YAAY,cAAc,cAAc,MAAM;AAAA,EACxD;AACA,MAAK,WAAW,SAAS,GAAI;AAI5B,WAAO;AAAA,MACN,YAAY;AAAA,MACZ,cAAc;AAAA,IACf;AAAA,EACD;AACA,SAAO,EAAE,YAAY,cAAc,KAAK;AACzC;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
1
|
+
{"version":3,"file":"use-lane-placement.mjs","names":[],"sources":["../../src/dashboard-lanes/use-lane-placement.ts"],"sourcesContent":["import { useState, useLayoutEffect, useMemo } from '@wordpress/element';\nimport { computeLanePlacements } from './lane-placement';\nimport { GRID_ITEM_DATA_KEY } from '../shared/grid-item-key';\n\nconst DEFAULT_ROW_UNIT = 4;\n\nfunction supportsGridLanes(): boolean {\n\tif ( typeof CSS === 'undefined' || ! CSS.supports ) {\n\t\treturn false;\n\t}\n\treturn CSS.supports( 'display', 'grid-lanes' );\n}\n\nfunction clampSpan( span: number | undefined ): number {\n\tif ( typeof span !== 'number' || ! Number.isFinite( span ) ) {\n\t\treturn 1;\n\t}\n\treturn Math.max( 1, Math.floor( span ) );\n}\n\n/**\n * Logical item passed to the hook. The renderer is responsible for\n * mounting a DOM node with `data-wp-grid-item-key={ item.key }` for each\n * entry; the hook will measure that node and produce inline styles.\n */\nexport type LaneItemInput = {\n\tkey: string;\n\tspan?: number;\n\tlane?: number;\n};\n\nexport type UseLanePlacementInput = {\n\titems: ReadonlyArray< LaneItemInput >;\n\tlanes: number;\n\tgap: number;\n\tflowTolerance: number;\n\t/**\n\t * Snap unit for `grid-row-start` / `grid-row-end: span N` math.\n\t * Smaller values produce sharper placement at the cost of more\n\t * implicit rows. Defaults to 4 (px).\n\t */\n\trowUnit?: number;\n};\n\nexport type UseLanePlacementResult = {\n\t/**\n\t * Inline styles to apply to each item, keyed by item key. On\n\t * native (`display: grid-lanes`), entries carry only\n\t * `gridColumn: span N`; the browser handles row placement. While\n\t * polyfilling, entries also carry explicit `grid-column-start` /\n\t * `grid-row-*` values.\n\t */\n\titemStyles: Map< string, React.CSSProperties >;\n\n\t/**\n\t * `false` when the host browser supports `display: grid-lanes`\n\t * natively. The hook avoids mounting any observers in that case.\n\t */\n\tisPolyfilled: boolean;\n};\n\n/**\n * Hook that measures item heights and resolves their placement when\n * `display: grid-lanes` is unavailable, falling through to a no-op\n * pass when the host browser supports the feature natively.\n *\n * Usage from the renderer:\n *\n * ```tsx\n * const [ container, setContainer ] = useState< HTMLDivElement | null >( null );\n * const { itemStyles } = useLanePlacement( container, {\n * items: layout,\n * lanes: columns,\n * gap: gapPx,\n * flowTolerance: 16,\n * } );\n *\n * return (\n * <div ref={ setContainer } style={ { display: 'grid-lanes', ... } }>\n * { items.map( ( item ) => (\n * <div\n * key={ item.key }\n * data-wp-grid-item-key={ item.key }\n * style={ itemStyles.get( item.key ) }\n * >\n * { ... }\n * </div>\n * ) ) }\n * </div>\n * );\n * ```\n *\n * @param container HTMLElement (or null pre-mount) hosting the items.\n * @param input Logical items, lane count, gap, and tuning.\n * @return Per-item styles plus the `isPolyfilled` flag.\n */\nexport function useLanePlacement(\n\tcontainer: HTMLElement | null,\n\tinput: UseLanePlacementInput\n): UseLanePlacementResult {\n\t// Detect once at mount. SSR returns `true` (CSS undefined); the\n\t// client-first render returns the real value. Either path produces\n\t// the same DOM until the polyfill effect runs (both emit\n\t// span-only styles), so there is no hydration mismatch.\n\tconst [ isPolyfilled ] = useState( () => ! supportsGridLanes() );\n\n\tconst [ itemStyles, setItemStyles ] = useState<\n\t\tMap< string, React.CSSProperties >\n\t>( () => new Map() );\n\n\t// Native pass-through: items only need their column span; the\n\t// browser handles row placement. Memoized so a stable items\n\t// array yields a stable Map identity.\n\tconst nativeStyles = useMemo( () => {\n\t\tconst map = new Map< string, React.CSSProperties >();\n\t\tfor ( const item of input.items ) {\n\t\t\tmap.set( item.key, {\n\t\t\t\tgridColumn: `span ${ clampSpan( item.span ) }`,\n\t\t\t} );\n\t\t}\n\t\treturn map;\n\t}, [ input.items ] );\n\n\t// Stable signature of items for deps. Keys, spans, and explicit\n\t// lanes are the only fields that influence observer wiring or\n\t// placement, so we hash exactly those.\n\tconst itemsSignature = useMemo( () => {\n\t\treturn input.items\n\t\t\t.map(\n\t\t\t\t( item ) =>\n\t\t\t\t\t`${ item.key }/${ item.span ?? 1 }/${ item.lane ?? '' }`\n\t\t\t)\n\t\t\t.join( '\\0' );\n\t}, [ input.items ] );\n\n\t// Stable array identity while placement-relevant fields match\n\t// `itemsSignature`, so the layout effect is not torn down on every\n\t// parent re-render that passes a fresh `items` reference.\n\t// eslint-disable-next-line react-hooks/exhaustive-deps -- `itemsSignature` encodes keys/spans/lanes; `input.items` reference often changes without placement changes.\n\tconst itemsForPlacement = useMemo( () => input.items, [ itemsSignature ] );\n\n\tconst { lanes, gap, flowTolerance, rowUnit } = input;\n\n\tuseLayoutEffect( () => {\n\t\tif ( ! isPolyfilled || ! container ) {\n\t\t\treturn;\n\t\t}\n\t\tif ( typeof ResizeObserver === 'undefined' ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst heights = new Map< string, number >();\n\t\tconst observed = new Set< Element >();\n\t\tlet cancelled = false;\n\t\tlet rafId: number | null = null;\n\n\t\tconst recompute = () => {\n\t\t\tif ( rafId !== null || cancelled ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t// One layout per frame even when ResizeObserver and\n\t\t\t// MutationObserver fire in the same tick.\n\t\t\trafId = requestAnimationFrame( () => {\n\t\t\t\trafId = null;\n\t\t\t\tif ( cancelled ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst itemsWithHeight = itemsForPlacement.map( ( item ) => ( {\n\t\t\t\t\tkey: item.key,\n\t\t\t\t\tspan: clampSpan( item.span ),\n\t\t\t\t\tlane: item.lane,\n\t\t\t\t\theight: heights.get( item.key ) ?? 0,\n\t\t\t\t} ) );\n\t\t\t\tconst result = computeLanePlacements( {\n\t\t\t\t\titems: itemsWithHeight,\n\t\t\t\t\tlanes,\n\t\t\t\t\tgap,\n\t\t\t\t\tflowTolerance,\n\t\t\t\t} );\n\t\t\t\tconst effectiveRowUnit = Math.max(\n\t\t\t\t\t1,\n\t\t\t\t\trowUnit ?? DEFAULT_ROW_UNIT\n\t\t\t\t);\n\t\t\t\tconst next = new Map< string, React.CSSProperties >();\n\t\t\t\tfor ( const item of itemsForPlacement ) {\n\t\t\t\t\tconst placement = result.placements.get( item.key );\n\t\t\t\t\tif ( ! placement ) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tconst height = heights.get( item.key ) ?? 0;\n\t\t\t\t\tconst rowStart =\n\t\t\t\t\t\tMath.floor( placement.top / effectiveRowUnit ) + 1;\n\t\t\t\t\tconst rowSpan = Math.max(\n\t\t\t\t\t\t1,\n\t\t\t\t\t\tMath.ceil( height / effectiveRowUnit )\n\t\t\t\t\t);\n\t\t\t\t\tnext.set( item.key, {\n\t\t\t\t\t\tgridColumnStart: placement.lane + 1,\n\t\t\t\t\t\tgridColumnEnd: `span ${ placement.span }`,\n\t\t\t\t\t\tgridRowStart: rowStart,\n\t\t\t\t\t\tgridRowEnd: `span ${ rowSpan }`,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\tsetItemStyles( next );\n\t\t\t} );\n\t\t};\n\n\t\tconst resizeObserver = new ResizeObserver( ( entries ) => {\n\t\t\tlet changed = false;\n\t\t\tfor ( const entry of entries ) {\n\t\t\t\tconst key = ( entry.target as HTMLElement ).getAttribute(\n\t\t\t\t\tGRID_ITEM_DATA_KEY\n\t\t\t\t);\n\t\t\t\tif ( ! key ) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tconst newHeight = entry.contentRect.height;\n\t\t\t\tif ( heights.get( key ) !== newHeight ) {\n\t\t\t\t\theights.set( key, newHeight );\n\t\t\t\t\tchanged = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif ( changed ) {\n\t\t\t\trecompute();\n\t\t\t}\n\t\t} );\n\n\t\tconst refreshObserved = () => {\n\t\t\tconst current = container.querySelectorAll(\n\t\t\t\t`[${ GRID_ITEM_DATA_KEY }]`\n\t\t\t);\n\t\t\tfor ( const element of current ) {\n\t\t\t\tif ( ! observed.has( element ) ) {\n\t\t\t\t\tobserved.add( element );\n\t\t\t\t\tresizeObserver.observe( element );\n\t\t\t\t\tconst key = element.getAttribute( GRID_ITEM_DATA_KEY );\n\t\t\t\t\tif ( key ) {\n\t\t\t\t\t\tconst rect = (\n\t\t\t\t\t\t\telement as HTMLElement\n\t\t\t\t\t\t ).getBoundingClientRect();\n\t\t\t\t\t\theights.set( key, rect.height );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor ( const element of observed ) {\n\t\t\t\tif ( ! container.contains( element ) ) {\n\t\t\t\t\tresizeObserver.unobserve( element );\n\t\t\t\t\tobserved.delete( element );\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\n\t\t// Children may mount, unmount, or change `data-wp-grid-item-key`\n\t\t// after the container exists (drag reorders, additions). The\n\t\t// mutation observer keeps the observed set in sync.\n\t\tconst mutationObserver =\n\t\t\ttypeof MutationObserver !== 'undefined'\n\t\t\t\t? new MutationObserver( () => {\n\t\t\t\t\t\trefreshObserved();\n\t\t\t\t\t\trecompute();\n\t\t\t\t\t} )\n\t\t\t\t: null;\n\t\tif ( mutationObserver ) {\n\t\t\tmutationObserver.observe( container, {\n\t\t\t\tchildList: true,\n\t\t\t\tsubtree: true,\n\t\t\t\tattributes: true,\n\t\t\t\tattributeFilter: [ GRID_ITEM_DATA_KEY ],\n\t\t\t} );\n\t\t}\n\n\t\trefreshObserved();\n\t\trecompute();\n\n\t\treturn () => {\n\t\t\tcancelled = true;\n\t\t\tif ( rafId !== null ) {\n\t\t\t\tcancelAnimationFrame( rafId );\n\t\t\t}\n\t\t\tresizeObserver.disconnect();\n\t\t\tmutationObserver?.disconnect();\n\t\t};\n\t}, [\n\t\tcontainer,\n\t\tisPolyfilled,\n\t\tlanes,\n\t\tgap,\n\t\tflowTolerance,\n\t\trowUnit,\n\t\titemsForPlacement,\n\t] );\n\n\tif ( ! isPolyfilled ) {\n\t\treturn { itemStyles: nativeStyles, isPolyfilled: false };\n\t}\n\tif ( itemStyles.size === 0 ) {\n\t\t// Pre-measurement frame: emit native-shape styles so items\n\t\t// appear in their default span rather than collapsing to 1\n\t\t// column at the top-left.\n\t\treturn {\n\t\t\titemStyles: nativeStyles as Map< string, React.CSSProperties >,\n\t\t\tisPolyfilled: true,\n\t\t};\n\t}\n\treturn { itemStyles, isPolyfilled: true };\n}\n"],"mappings":";;;;AAIA,MAAM,mBAAmB;AAEzB,SAAS,oBAA6B;CACrC,IAAK,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S,OAAQ;IAChC,MAAM,MAAM,QAAQ,aAAc,kBAAmB;IACrD,IAAK,KAAM;KACV,MAAM,OACL,QACE,sBAAsB;KACzB,QAAQ,IAAK,KAAK,KAAK,MAAO;IAC/B;GACD;GAED,KAAM,MAAM,WAAW,UACtB,IAAK,CAAE,UAAU,SAAU,OAAQ,GAAI;IACtC,eAAe,UAAW,OAAQ;IAClC,SAAS,OAAQ,OAAQ;GAC1B;EAEF;EAKA,MAAM,mBACL,OAAO,qBAAqB,cACzB,IAAI,uBAAwB;GAC5B,gBAAgB;GAChB,UAAU;EACX,CAAE,IACD;EACJ,IAAK,kBACJ,iBAAiB,QAAS,WAAW;GACpC,WAAW;GACX,SAAS;GACT,YAAY;GACZ,iBAAiB,CAAE,kBAAmB;EACvC,CAAE;EAGH,gBAAgB;EAChB,UAAU;EAEV,aAAa;GACZ,YAAY;GACZ,IAAK,UAAU,MACd,qBAAsB,KAAM;GAE7B,eAAe,WAAW;GAC1B,kBAAkB,WAAW;EAC9B;CACD,GAAG;EACF;EACA;EACA;EACA;EACA;EACA;EACA;CACD,CAAE;CAEF,IAAK,CAAE,cACN,OAAO;EAAE,YAAY;EAAc,cAAc;CAAM;CAExD,IAAK,WAAW,SAAS,GAIxB,OAAO;EACN,YAAY;EACZ,cAAc;CACf;CAED,OAAO;EAAE;EAAY,cAAc;CAAK;AACzC"}
|
package/build-module/index.mjs
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
// packages/grid/src/index.ts
|
|
2
1
|
import { DashboardGrid } from "./dashboard-grid/index.mjs";
|
|
3
2
|
import { DashboardLanes } from "./dashboard-lanes/index.mjs";
|
|
4
|
-
export {
|
|
5
|
-
DashboardGrid,
|
|
6
|
-
DashboardLanes
|
|
7
|
-
};
|
|
8
|
-
//# sourceMappingURL=index.mjs.map
|
|
3
|
+
export { DashboardGrid, DashboardLanes };
|
|
@@ -1,24 +1,28 @@
|
|
|
1
|
-
// packages/grid/src/shared/array-move-with-pinned.ts
|
|
2
1
|
import { arrayMove } from "@dnd-kit/sortable";
|
|
2
|
+
// packages/grid/src/shared/array-move-with-pinned.ts
|
|
3
|
+
/**
|
|
4
|
+
* Moves the item at `from` to `to` while pinned items hold their index:
|
|
5
|
+
* the free items reorder among the remaining slots. Moving a pinned item
|
|
6
|
+
* is a no-op.
|
|
7
|
+
*
|
|
8
|
+
* @param items Items in display order.
|
|
9
|
+
* @param from Index of the moved item.
|
|
10
|
+
* @param to Target index.
|
|
11
|
+
* @param isPinned Whether an item holds its index.
|
|
12
|
+
* @return The reordered items.
|
|
13
|
+
*/
|
|
3
14
|
function arrayMoveWithPinned(items, from, to, isPinned) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return item;
|
|
15
|
-
}
|
|
16
|
-
const next = free[nextFree];
|
|
17
|
-
nextFree += 1;
|
|
18
|
-
return next;
|
|
19
|
-
});
|
|
15
|
+
const pinned = new Set(items.filter(isPinned));
|
|
16
|
+
if (pinned.size === 0) return arrayMove(items, from, to);
|
|
17
|
+
const free = arrayMove(items, from, to).filter((item) => !pinned.has(item));
|
|
18
|
+
let nextFree = 0;
|
|
19
|
+
return items.map((item) => {
|
|
20
|
+
if (pinned.has(item)) return item;
|
|
21
|
+
const next = free[nextFree];
|
|
22
|
+
nextFree += 1;
|
|
23
|
+
return next;
|
|
24
|
+
});
|
|
20
25
|
}
|
|
21
|
-
export {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
//# sourceMappingURL=array-move-with-pinned.mjs.map
|
|
26
|
+
export { arrayMoveWithPinned };
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=array-move-with-pinned.mjs.map
|
|
@@ -1,7 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/shared/array-move-with-pinned.ts"],
|
|
4
|
-
"sourcesContent": ["import { arrayMove } from '@dnd-kit/sortable';\n\n/**\n * Moves the item at `from` to `to` while pinned items hold their index:\n * the free items reorder among the remaining slots. Moving a pinned item\n * is a no-op.\n *\n * @param items Items in display order.\n * @param from Index of the moved item.\n * @param to Target index.\n * @param isPinned Whether an item holds its index.\n * @return The reordered items.\n */\nexport function arrayMoveWithPinned< T >(\n\titems: T[],\n\tfrom: number,\n\tto: number,\n\tisPinned: ( item: T ) => boolean\n): T[] {\n\tconst pinned = new Set( items.filter( isPinned ) );\n\tif ( pinned.size === 0 ) {\n\t\treturn arrayMove( items, from, to );\n\t}\n\tconst free = arrayMove( items, from, to ).filter(\n\t\t( item ) => ! pinned.has( item )\n\t);\n\tlet nextFree = 0;\n\treturn items.map( ( item ) => {\n\t\tif ( pinned.has( item ) ) {\n\t\t\treturn item;\n\t\t}\n\t\tconst next = free[ nextFree ];\n\t\tnextFree += 1;\n\t\treturn next;\n\t} );\n}\n"],
|
|
5
|
-
"mappings": ";AAAA,SAAS,iBAAiB;AAanB,SAAS,oBACf,OACA,MACA,IACA,UACM;AACN,QAAM,SAAS,IAAI,IAAK,MAAM,OAAQ,QAAS,CAAE;AACjD,MAAK,OAAO,SAAS,GAAI;AACxB,WAAO,UAAW,OAAO,MAAM,EAAG;AAAA,EACnC;AACA,QAAM,OAAO,UAAW,OAAO,MAAM,EAAG,EAAE;AAAA,IACzC,CAAE,SAAU,CAAE,OAAO,IAAK,IAAK;AAAA,EAChC;AACA,MAAI,WAAW;AACf,SAAO,MAAM,IAAK,CAAE,SAAU;AAC7B,QAAK,OAAO,IAAK,IAAK,GAAI;AACzB,aAAO;AAAA,IACR;AACA,UAAM,OAAO,KAAM,QAAS;AAC5B,gBAAY;AACZ,WAAO;AAAA,EACR,CAAE;AACH;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
1
|
+
{"version":3,"file":"array-move-with-pinned.mjs","names":[],"sources":["../../src/shared/array-move-with-pinned.ts"],"sourcesContent":["import { arrayMove } from '@dnd-kit/sortable';\n\n/**\n * Moves the item at `from` to `to` while pinned items hold their index:\n * the free items reorder among the remaining slots. Moving a pinned item\n * is a no-op.\n *\n * @param items Items in display order.\n * @param from Index of the moved item.\n * @param to Target index.\n * @param isPinned Whether an item holds its index.\n * @return The reordered items.\n */\nexport function arrayMoveWithPinned< T >(\n\titems: T[],\n\tfrom: number,\n\tto: number,\n\tisPinned: ( item: T ) => boolean\n): T[] {\n\tconst pinned = new Set( items.filter( isPinned ) );\n\tif ( pinned.size === 0 ) {\n\t\treturn arrayMove( items, from, to );\n\t}\n\tconst free = arrayMove( items, from, to ).filter(\n\t\t( item ) => ! pinned.has( item )\n\t);\n\tlet nextFree = 0;\n\treturn items.map( ( item ) => {\n\t\tif ( pinned.has( item ) ) {\n\t\t\treturn item;\n\t\t}\n\t\tconst next = free[ nextFree ];\n\t\tnextFree += 1;\n\t\treturn next;\n\t} );\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,SAAgB,oBACf,OACA,MACA,IACA,UACM;CACN,MAAM,SAAS,IAAI,IAAK,MAAM,OAAQ,QAAS,CAAE;CACjD,IAAK,OAAO,SAAS,GACpB,OAAO,UAAW,OAAO,MAAM,EAAG;CAEnC,MAAM,OAAO,UAAW,OAAO,MAAM,EAAG,CAAC,CAAC,QACvC,SAAU,CAAE,OAAO,IAAK,IAAK,CAChC;CACA,IAAI,WAAW;CACf,OAAO,MAAM,KAAO,SAAU;EAC7B,IAAK,OAAO,IAAK,IAAK,GACrB,OAAO;EAER,MAAM,OAAO,KAAM;EACnB,YAAY;EACZ,OAAO;CACR,CAAE;AACH"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// packages/grid/src/shared/column-policy.ts
|
|
2
|
+
function normalizeCount(count) {
|
|
3
|
+
return typeof count === "number" && Number.isFinite(count) ? Math.max(1, Math.floor(count)) : void 0;
|
|
4
|
+
}
|
|
5
|
+
function resolveFloor({ minColumnWidth }) {
|
|
6
|
+
return typeof minColumnWidth === "number" && Number.isFinite(minColumnWidth) && minColumnWidth > 0 ? minColumnWidth : void 0;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The count the policy asks for at a container width, before the floor.
|
|
10
|
+
* Counts are rounded down and never below one; a count that is not a
|
|
11
|
+
* finite number, or a step that is neither a count nor `'auto'`, is
|
|
12
|
+
* ignored.
|
|
13
|
+
*
|
|
14
|
+
* @param policy Column policy.
|
|
15
|
+
* @param containerWidth Measured inline size of the surface in pixels.
|
|
16
|
+
* @return The count, or `undefined` when none applies: no count was set, or
|
|
17
|
+
* an `'auto'` step lifts the cap over a floor.
|
|
18
|
+
*/
|
|
19
|
+
function resolveColumnCap(policy, containerWidth) {
|
|
20
|
+
const { columns } = policy;
|
|
21
|
+
if (typeof columns !== "object" || columns === null) return normalizeCount(columns);
|
|
22
|
+
const hasFloor = resolveFloor(policy) !== void 0;
|
|
23
|
+
const widths = Object.keys(columns).map(Number).filter(Number.isFinite).filter((width) => {
|
|
24
|
+
const step = columns[width];
|
|
25
|
+
return step === "auto" ? hasFloor : normalizeCount(step) !== void 0;
|
|
26
|
+
}).sort((a, b) => b - a);
|
|
27
|
+
if (widths.length === 0) return;
|
|
28
|
+
const step = columns[widths.find((width) => containerWidth >= width) ?? widths[widths.length - 1]];
|
|
29
|
+
return step === "auto" ? void 0 : normalizeCount(step);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Resolves the count a surface renders: the policy's count at the container
|
|
33
|
+
* width, lowered by the floor so no column is narrower than
|
|
34
|
+
* `minColumnWidth`. A floor with no count fits as many columns as the
|
|
35
|
+
* container allows.
|
|
36
|
+
*
|
|
37
|
+
* @param policy Column policy.
|
|
38
|
+
* @param containerWidth Measured inline size of the surface in pixels.
|
|
39
|
+
* @param gapPx Column gap in pixels.
|
|
40
|
+
* @param defaultColumns Count used when the policy sets neither field.
|
|
41
|
+
* @return Column count, at least one.
|
|
42
|
+
*/
|
|
43
|
+
function resolveColumnCount(policy, containerWidth, gapPx, defaultColumns) {
|
|
44
|
+
const cap = resolveColumnCap(policy, containerWidth);
|
|
45
|
+
const floor = resolveFloor(policy);
|
|
46
|
+
if (floor === void 0) return Math.max(1, cap ?? defaultColumns);
|
|
47
|
+
const maxFit = Math.max(1, Math.floor((containerWidth + gapPx) / (floor + gapPx)));
|
|
48
|
+
return cap === void 0 ? maxFit : Math.max(1, Math.min(cap, maxFit));
|
|
49
|
+
}
|
|
50
|
+
export { resolveColumnCap, resolveColumnCount };
|
|
51
|
+
|
|
52
|
+
//# sourceMappingURL=column-policy.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"column-policy.mjs","names":[],"sources":["../../src/shared/column-policy.ts"],"sourcesContent":["/**\n * Column count per container width. Each key is the inline size in pixels\n * from which its count applies: `{ 0: 1, 600: 2, 960: 4 }` is one column\n * from 0px, two from 600px, four from 960px, and below the smallest key\n * that step's count applies. An `'auto'` step lifts the cap from its\n * width up and leaves the count to `minColumnWidth`; with no floor set,\n * the step is skipped and the count below it holds.\n */\nexport type ColumnSteps = Record< number, number | 'auto' >;\n\n/**\n * Column policy shared by the surfaces: a count at every width, or a count\n * per container width, with an optional per-column floor in pixels.\n */\nexport interface ColumnPolicy {\n\tcolumns?: number | ColumnSteps;\n\tminColumnWidth?: number;\n}\n\nfunction normalizeCount( count: unknown ): number | undefined {\n\treturn typeof count === 'number' && Number.isFinite( count )\n\t\t? Math.max( 1, Math.floor( count ) )\n\t\t: undefined;\n}\n\nfunction resolveFloor( { minColumnWidth }: ColumnPolicy ): number | undefined {\n\treturn typeof minColumnWidth === 'number' &&\n\t\tNumber.isFinite( minColumnWidth ) &&\n\t\tminColumnWidth > 0\n\t\t? minColumnWidth\n\t\t: undefined;\n}\n\n/**\n * The count the policy asks for at a container width, before the floor.\n * Counts are rounded down and never below one; a count that is not a\n * finite number, or a step that is neither a count nor `'auto'`, is\n * ignored.\n *\n * @param policy Column policy.\n * @param containerWidth Measured inline size of the surface in pixels.\n * @return The count, or `undefined` when none applies: no count was set, or\n * an `'auto'` step lifts the cap over a floor.\n */\nexport function resolveColumnCap(\n\tpolicy: ColumnPolicy,\n\tcontainerWidth: number\n): number | undefined {\n\tconst { columns } = policy;\n\tif ( typeof columns !== 'object' || columns === null ) {\n\t\treturn normalizeCount( columns );\n\t}\n\tconst hasFloor = resolveFloor( policy ) !== undefined;\n\tconst widths = Object.keys( columns )\n\t\t.map( Number )\n\t\t.filter( Number.isFinite )\n\t\t.filter( ( width ) => {\n\t\t\tconst step = columns[ width ];\n\t\t\treturn step === 'auto'\n\t\t\t\t? hasFloor\n\t\t\t\t: normalizeCount( step ) !== undefined;\n\t\t} )\n\t\t.sort( ( a, b ) => b - a );\n\tif ( widths.length === 0 ) {\n\t\treturn undefined;\n\t}\n\tconst reached = widths.find( ( width ) => containerWidth >= width );\n\tconst step = columns[ reached ?? widths[ widths.length - 1 ] ];\n\treturn step === 'auto' ? undefined : normalizeCount( step );\n}\n\n/**\n * Resolves the count a surface renders: the policy's count at the container\n * width, lowered by the floor so no column is narrower than\n * `minColumnWidth`. A floor with no count fits as many columns as the\n * container allows.\n *\n * @param policy Column policy.\n * @param containerWidth Measured inline size of the surface in pixels.\n * @param gapPx Column gap in pixels.\n * @param defaultColumns Count used when the policy sets neither field.\n * @return Column count, at least one.\n */\nexport function resolveColumnCount(\n\tpolicy: ColumnPolicy,\n\tcontainerWidth: number,\n\tgapPx: number,\n\tdefaultColumns: number\n): number {\n\tconst cap = resolveColumnCap( policy, containerWidth );\n\tconst floor = resolveFloor( policy );\n\tif ( floor === undefined ) {\n\t\treturn Math.max( 1, cap ?? defaultColumns );\n\t}\n\tconst maxFit = Math.max(\n\t\t1,\n\t\tMath.floor( ( containerWidth + gapPx ) / ( floor + gapPx ) )\n\t);\n\treturn cap === undefined ? maxFit : Math.max( 1, Math.min( cap, maxFit ) );\n}\n"],"mappings":";AAmBA,SAAS,eAAgB,OAAqC;CAC7D,OAAO,OAAO,UAAU,YAAY,OAAO,SAAU,KAAM,IACxD,KA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
1
2
|
// packages/grid/src/shared/drag-overlay-drop-animation.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
/** Matches `--wpds-motion-duration-md` on the drag preview frame exit. */
|
|
4
|
+
const DROP_ANIMATION_DURATION_MS = 200;
|
|
5
|
+
/** Matches `--wpds-motion-easing-balanced` on the drag preview frame exit. */
|
|
6
|
+
const DROP_ANIMATION_EASING = "cubic-bezier(0.4, 0, 0.2, 1)";
|
|
7
|
+
/**
|
|
8
|
+
* Composes `@dnd-kit/core`’s default overlay drop translation with preview
|
|
9
|
+
* exit keyframes (via side effects). When the pointer never moves, `@dnd-kit`
|
|
10
|
+
* skips the drop animation and these side effects do not run.
|
|
11
|
+
*
|
|
12
|
+
* @param dragPreviewFrameClassName Hashed class for `.drag-preview-frame`.
|
|
13
|
+
* @param exitingFrameClassName Hashed class for the exit state.
|
|
14
|
+
*/
|
|
8
15
|
function createDashboardDragDropAnimation(dragPreviewFrameClassName, exitingFrameClassName) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
lift.getAnimations().forEach(
|
|
29
|
-
(animation) => animation.cancel()
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
frame.classList.add(exitingFrameClassName);
|
|
33
|
-
}
|
|
34
|
-
return () => {
|
|
35
|
-
cleanupDefault?.();
|
|
36
|
-
if (frame) {
|
|
37
|
-
frame.classList.remove(exitingFrameClassName);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
};
|
|
16
|
+
return {
|
|
17
|
+
...defaultDropAnimation,
|
|
18
|
+
duration: 200,
|
|
19
|
+
easing: DROP_ANIMATION_EASING,
|
|
20
|
+
sideEffects(args) {
|
|
21
|
+
const cleanupDefault = defaultDropAnimationSideEffects({ styles: { active: { opacity: "0" } } })(args);
|
|
22
|
+
const frame = args.dragOverlay.node.getElementsByClassName(dragPreviewFrameClassName)[0];
|
|
23
|
+
if (frame) {
|
|
24
|
+
frame.getAnimations().forEach((animation) => animation.cancel());
|
|
25
|
+
const lift = frame.firstElementChild;
|
|
26
|
+
if (lift instanceof HTMLElement) lift.getAnimations().forEach((animation) => animation.cancel());
|
|
27
|
+
frame.classList.add(exitingFrameClassName);
|
|
28
|
+
}
|
|
29
|
+
return () => {
|
|
30
|
+
cleanupDefault?.();
|
|
31
|
+
if (frame) frame.classList.remove(exitingFrameClassName);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
};
|
|
42
35
|
}
|
|
43
|
-
export {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
};
|
|
47
|
-
//# sourceMappingURL=drag-overlay-drop-animation.mjs.map
|
|
36
|
+
export { DROP_ANIMATION_DURATION_MS, createDashboardDragDropAnimation };
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=drag-overlay-drop-animation.mjs.map
|