@wordpress/grid 0.5.0 → 0.7.1-next.v.202608281122.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/CHANGELOG.md +22 -0
- package/README.md +44 -1
- package/build/dashboard-grid/grid-item.cjs +21 -7
- package/build/dashboard-grid/grid-item.cjs.map +2 -2
- package/build/dashboard-grid/index.cjs +110 -48
- package/build/dashboard-grid/index.cjs.map +2 -2
- package/build/dashboard-grid/resolve-fill-widths.cjs +68 -50
- package/build/dashboard-grid/resolve-fill-widths.cjs.map +2 -2
- package/build/dashboard-grid/types.cjs.map +1 -1
- package/build/dashboard-lanes/index.cjs +62 -20
- package/build/dashboard-lanes/index.cjs.map +2 -2
- package/build/dashboard-lanes/lanes-item.cjs +14 -6
- package/build/dashboard-lanes/lanes-item.cjs.map +2 -2
- package/build/dashboard-lanes/types.cjs.map +1 -1
- package/build/index.cjs.map +1 -1
- package/build/shared/array-move-with-pinned.cjs +49 -0
- package/build/shared/array-move-with-pinned.cjs.map +7 -0
- package/build/shared/resize-snap.cjs +91 -5
- package/build/shared/resize-snap.cjs.map +2 -2
- package/build/shared/types.cjs.map +1 -1
- package/build/shared/use-span-bounds.cjs +85 -0
- package/build/shared/use-span-bounds.cjs.map +7 -0
- package/build-module/dashboard-grid/grid-item.mjs +21 -7
- package/build-module/dashboard-grid/grid-item.mjs.map +2 -2
- package/build-module/dashboard-grid/index.mjs +111 -50
- package/build-module/dashboard-grid/index.mjs.map +2 -2
- package/build-module/dashboard-grid/resolve-fill-widths.mjs +68 -50
- package/build-module/dashboard-grid/resolve-fill-widths.mjs.map +2 -2
- package/build-module/dashboard-lanes/index.mjs +63 -22
- package/build-module/dashboard-lanes/index.mjs.map +2 -2
- package/build-module/dashboard-lanes/lanes-item.mjs +14 -6
- package/build-module/dashboard-lanes/lanes-item.mjs.map +2 -2
- package/build-module/index.mjs.map +1 -1
- package/build-module/shared/array-move-with-pinned.mjs +24 -0
- package/build-module/shared/array-move-with-pinned.mjs.map +7 -0
- package/build-module/shared/resize-snap.mjs +87 -4
- package/build-module/shared/resize-snap.mjs.map +2 -2
- package/build-module/shared/use-span-bounds.mjs +62 -0
- package/build-module/shared/use-span-bounds.mjs.map +7 -0
- package/build-types/dashboard-grid/grid-item.d.ts +1 -1
- package/build-types/dashboard-grid/grid-item.d.ts.map +1 -1
- package/build-types/dashboard-grid/index.d.ts.map +1 -1
- package/build-types/dashboard-grid/resolve-fill-widths.d.ts +12 -1
- package/build-types/dashboard-grid/resolve-fill-widths.d.ts.map +1 -1
- package/build-types/dashboard-grid/types.d.ts +43 -1
- 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/lanes-item.d.ts +18 -1
- package/build-types/dashboard-lanes/lanes-item.d.ts.map +1 -1
- package/build-types/dashboard-lanes/types.d.ts +20 -1
- package/build-types/dashboard-lanes/types.d.ts.map +1 -1
- package/build-types/index.d.ts +1 -1
- package/build-types/index.d.ts.map +1 -1
- package/build-types/shared/array-move-with-pinned.d.ts +13 -0
- package/build-types/shared/array-move-with-pinned.d.ts.map +1 -0
- package/build-types/shared/resize-snap.d.ts +65 -2
- package/build-types/shared/resize-snap.d.ts.map +1 -1
- package/build-types/shared/types.d.ts +31 -0
- package/build-types/shared/types.d.ts.map +1 -1
- package/build-types/shared/use-span-bounds.d.ts +29 -0
- package/build-types/shared/use-span-bounds.d.ts.map +1 -0
- package/package.json +10 -10
- package/src/dashboard-grid/grid-item.tsx +21 -7
- package/src/dashboard-grid/grid.module.css +6 -6
- package/src/dashboard-grid/index.tsx +155 -58
- package/src/dashboard-grid/resolve-fill-widths.ts +85 -62
- package/src/dashboard-grid/stories/index.story.tsx +169 -0
- package/src/dashboard-grid/test/item-interactions.test.tsx +63 -0
- package/src/dashboard-grid/test/item-limits.test.tsx +147 -0
- package/src/dashboard-grid/test/resolve-fill-widths.test.ts +158 -0
- package/src/dashboard-grid/types.ts +50 -0
- package/src/dashboard-lanes/index.tsx +87 -25
- package/src/dashboard-lanes/lanes-item.tsx +34 -6
- package/src/dashboard-lanes/lanes.module.css +6 -6
- package/src/dashboard-lanes/stories/index.story.tsx +144 -0
- package/src/dashboard-lanes/test/item-interactions.test.tsx +63 -0
- package/src/dashboard-lanes/test/item-limits.test.tsx +140 -0
- package/src/dashboard-lanes/test/use-lane-placement.test.tsx +10 -3
- package/src/dashboard-lanes/types.ts +23 -0
- package/src/index.ts +2 -0
- package/src/shared/array-move-with-pinned.ts +36 -0
- package/src/shared/resize-snap.ts +161 -5
- package/src/shared/test/array-move-with-pinned.test.ts +39 -0
- package/src/shared/test/resize-snap.test.ts +170 -2
- package/src/shared/test/use-span-bounds.test.ts +104 -0
- package/src/shared/types.ts +35 -0
- package/src/shared/use-span-bounds.ts +99 -0
- package/build-types/dashboard-grid/stories/index.story.d.ts +0 -92
- package/build-types/dashboard-grid/stories/index.story.d.ts.map +0 -1
- package/build-types/dashboard-lanes/stories/index.story.d.ts +0 -58
- package/build-types/dashboard-lanes/stories/index.story.d.ts.map +0 -1
|
@@ -29,6 +29,8 @@ export function GridItem( {
|
|
|
29
29
|
item,
|
|
30
30
|
maxColumns,
|
|
31
31
|
disabled = false,
|
|
32
|
+
draggable = true,
|
|
33
|
+
resizable = true,
|
|
32
34
|
verticalResizable = true,
|
|
33
35
|
interacting = false,
|
|
34
36
|
dragging = false,
|
|
@@ -39,6 +41,8 @@ export function GridItem( {
|
|
|
39
41
|
resizeSnapPreview = null,
|
|
40
42
|
minResizeWidthPx,
|
|
41
43
|
minResizeHeightPx,
|
|
44
|
+
maxResizeWidthPx,
|
|
45
|
+
maxResizeHeightPx,
|
|
42
46
|
renderResizeHandle,
|
|
43
47
|
}: GridItemProps ) {
|
|
44
48
|
const [ resizeDelta, setResizeDelta ] = useState< ResizeDelta | null >(
|
|
@@ -50,6 +54,8 @@ export function GridItem( {
|
|
|
50
54
|
} | null >( null );
|
|
51
55
|
const itemRef = useRef< HTMLDivElement >( null );
|
|
52
56
|
const contentRef = useRef< HTMLDivElement >( null );
|
|
57
|
+
const dragDisabled = disabled || ! draggable;
|
|
58
|
+
const resizeDisabled = disabled || ! resizable;
|
|
53
59
|
const {
|
|
54
60
|
attributes,
|
|
55
61
|
listeners,
|
|
@@ -58,7 +64,7 @@ export function GridItem( {
|
|
|
58
64
|
isDragging,
|
|
59
65
|
} = useSortable( {
|
|
60
66
|
id: item.key,
|
|
61
|
-
disabled,
|
|
67
|
+
disabled: dragDisabled,
|
|
62
68
|
} );
|
|
63
69
|
const mergedRef = useMergeRefs( [ itemRef, setNodeRef ] );
|
|
64
70
|
const contentMergedRef = useMergeRefs( [ contentRef ] );
|
|
@@ -100,10 +106,18 @@ export function GridItem( {
|
|
|
100
106
|
height: verticalResizable ? delta.height : 0,
|
|
101
107
|
};
|
|
102
108
|
if ( baselineSize ) {
|
|
103
|
-
clamped = clampResizeDelta(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
109
|
+
clamped = clampResizeDelta(
|
|
110
|
+
clamped,
|
|
111
|
+
baselineSize,
|
|
112
|
+
{
|
|
113
|
+
width: minResizeWidthPx,
|
|
114
|
+
height: verticalResizable ? minResizeHeightPx : undefined,
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
width: maxResizeWidthPx,
|
|
118
|
+
height: verticalResizable ? maxResizeHeightPx : undefined,
|
|
119
|
+
}
|
|
120
|
+
);
|
|
107
121
|
}
|
|
108
122
|
setResizeDelta( clamped );
|
|
109
123
|
onResize( item.key, clamped );
|
|
@@ -156,7 +170,7 @@ export function GridItem( {
|
|
|
156
170
|
{ ...listeners }
|
|
157
171
|
style={ {
|
|
158
172
|
height: '100%',
|
|
159
|
-
cursor: getItemCursor(
|
|
173
|
+
cursor: getItemCursor( dragDisabled, interacting ),
|
|
160
174
|
} }
|
|
161
175
|
>
|
|
162
176
|
<div
|
|
@@ -165,7 +179,7 @@ export function GridItem( {
|
|
|
165
179
|
style={ continuousContentStyle }
|
|
166
180
|
>
|
|
167
181
|
{ children }
|
|
168
|
-
{ !
|
|
182
|
+
{ ! resizeDisabled && (
|
|
169
183
|
<ResizeHandle
|
|
170
184
|
itemId={ item.key }
|
|
171
185
|
verticalResizable={ verticalResizable }
|
|
@@ -82,14 +82,14 @@
|
|
|
82
82
|
* is replaced.
|
|
83
83
|
*/
|
|
84
84
|
@media not (prefers-reduced-motion: reduce) {
|
|
85
|
-
.drag-preview-frame.
|
|
85
|
+
.drag-preview-frame.is-exiting {
|
|
86
86
|
animation:
|
|
87
87
|
wp-grid-drag-preview-shadow-exit
|
|
88
88
|
var(--wpds-motion-duration-md) var(--wpds-motion-easing-balanced)
|
|
89
89
|
forwards;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
.drag-preview-frame.
|
|
92
|
+
.drag-preview-frame.is-exiting .drag-preview-frame__lift {
|
|
93
93
|
animation:
|
|
94
94
|
wp-grid-drag-preview-scale-exit
|
|
95
95
|
var(--wpds-motion-duration-md) var(--wpds-motion-easing-balanced)
|
|
@@ -117,11 +117,11 @@
|
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
.drag-preview-frame.
|
|
120
|
+
.drag-preview-frame.is-exiting {
|
|
121
121
|
box-shadow: var(--_wp-grid-elevation-xs);
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
.drag-preview-frame.
|
|
124
|
+
.drag-preview-frame.is-exiting .drag-preview-frame__lift {
|
|
125
125
|
transform: scale(1);
|
|
126
126
|
}
|
|
127
127
|
|
|
@@ -134,12 +134,12 @@
|
|
|
134
134
|
transform: none;
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
.drag-preview-frame.
|
|
137
|
+
.drag-preview-frame.is-exiting {
|
|
138
138
|
box-shadow: var(--_wp-grid-elevation-xs);
|
|
139
139
|
transition: none;
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
.drag-preview-frame.
|
|
142
|
+
.drag-preview-frame.is-exiting .drag-preview-frame__lift {
|
|
143
143
|
transition: none;
|
|
144
144
|
}
|
|
145
145
|
}
|
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
useSensors,
|
|
8
8
|
} from '@dnd-kit/core';
|
|
9
9
|
import {
|
|
10
|
-
arrayMove,
|
|
11
10
|
SortableContext,
|
|
12
11
|
sortableKeyboardCoordinates,
|
|
13
12
|
} from '@dnd-kit/sortable';
|
|
@@ -25,8 +24,10 @@ import {
|
|
|
25
24
|
useState,
|
|
26
25
|
} from '@wordpress/element';
|
|
27
26
|
import { GridItem } from './grid-item';
|
|
27
|
+
import { arrayMoveWithPinned } from '../shared/array-move-with-pinned';
|
|
28
28
|
import { GridOverlay } from '../shared/grid-overlay';
|
|
29
|
-
import { gridSpanToPixelSize } from '../shared/resize-snap';
|
|
29
|
+
import { clampSpan, gridSpanToPixelSize } from '../shared/resize-snap';
|
|
30
|
+
import { useResizePixelLimits, useSpanBounds } from '../shared/use-span-bounds';
|
|
30
31
|
import layoutAnimationStyles from '../shared/layout-shift-animation.module.css';
|
|
31
32
|
import { ItemExitOverlay } from '../shared/item-exit-overlay';
|
|
32
33
|
import {
|
|
@@ -43,7 +44,7 @@ import styles from './grid.module.css';
|
|
|
43
44
|
|
|
44
45
|
const dashboardDragDropAnimation = createDashboardDragDropAnimation(
|
|
45
46
|
styles[ 'drag-preview-frame' ],
|
|
46
|
-
styles
|
|
47
|
+
styles[ 'is-exiting' ]
|
|
47
48
|
);
|
|
48
49
|
|
|
49
50
|
// Fallback gap in pixels for math that runs before the computed gap
|
|
@@ -107,6 +108,7 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
|
|
|
107
108
|
style,
|
|
108
109
|
rowHeight = 'auto',
|
|
109
110
|
minColumnWidth,
|
|
111
|
+
itemLimits,
|
|
110
112
|
editMode = false,
|
|
111
113
|
onChangeLayout,
|
|
112
114
|
onPreviewLayout,
|
|
@@ -156,7 +158,6 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
|
|
|
156
158
|
const childrenCacheRef = useRef< Map< string, React.ReactElement > >(
|
|
157
159
|
new Map()
|
|
158
160
|
);
|
|
159
|
-
const activeLayout = temporaryLayout ?? layout;
|
|
160
161
|
|
|
161
162
|
const [ gridRoot, setGridRoot ] = useState< HTMLDivElement | null >(
|
|
162
163
|
null
|
|
@@ -227,6 +228,58 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
|
|
|
227
228
|
: gridSpanToPixelSize( 1, 1, columnWidth, gapPx, rowHeightPx )
|
|
228
229
|
.heightPx ?? undefined;
|
|
229
230
|
|
|
231
|
+
const spanBoundsByKey = useSpanBounds(
|
|
232
|
+
itemLimits,
|
|
233
|
+
columnWidth,
|
|
234
|
+
gapPx,
|
|
235
|
+
rowHeightPx,
|
|
236
|
+
effectiveColumns
|
|
237
|
+
);
|
|
238
|
+
const resizeLimitsByKey = useResizePixelLimits(
|
|
239
|
+
spanBoundsByKey,
|
|
240
|
+
columnWidth,
|
|
241
|
+
gapPx,
|
|
242
|
+
rowHeightPx
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
// Stored and in-gesture layouts may sit outside an item's limits;
|
|
246
|
+
// render them bounded while the data stays untouched. Gestures
|
|
247
|
+
// commit from the stored `layout`, never from this bounded view,
|
|
248
|
+
// so a bounded span cannot reach `onChangeLayout`. Keeps the
|
|
249
|
+
// source identity when no item needs bounding.
|
|
250
|
+
const sourceLayout = temporaryLayout ?? layout;
|
|
251
|
+
const activeLayout = useMemo( () => {
|
|
252
|
+
if ( spanBoundsByKey.size === 0 ) {
|
|
253
|
+
return sourceLayout;
|
|
254
|
+
}
|
|
255
|
+
let changed = false;
|
|
256
|
+
const bounded = sourceLayout.map( ( item ) => {
|
|
257
|
+
const bounds = spanBoundsByKey.get( item.key );
|
|
258
|
+
if ( ! bounds ) {
|
|
259
|
+
return item;
|
|
260
|
+
}
|
|
261
|
+
const width =
|
|
262
|
+
typeof item.width === 'number'
|
|
263
|
+
? clampSpan(
|
|
264
|
+
item.width,
|
|
265
|
+
bounds.minWidth,
|
|
266
|
+
bounds.maxWidth
|
|
267
|
+
)
|
|
268
|
+
: item.width;
|
|
269
|
+
const height = clampSpan(
|
|
270
|
+
item.height ?? 1,
|
|
271
|
+
bounds.minHeight,
|
|
272
|
+
bounds.maxHeight
|
|
273
|
+
);
|
|
274
|
+
if ( width === item.width && height === ( item.height ?? 1 ) ) {
|
|
275
|
+
return item;
|
|
276
|
+
}
|
|
277
|
+
changed = true;
|
|
278
|
+
return { ...item, width, height };
|
|
279
|
+
} );
|
|
280
|
+
return changed ? bounded : sourceLayout;
|
|
281
|
+
}, [ sourceLayout, spanBoundsByKey ] );
|
|
282
|
+
|
|
230
283
|
const layoutMap = useMemo( () => {
|
|
231
284
|
const map = new Map< string, DashboardGridLayoutItem >();
|
|
232
285
|
activeLayout.forEach( ( item ) => map.set( item.key, item ) );
|
|
@@ -262,12 +315,14 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
|
|
|
262
315
|
);
|
|
263
316
|
const items = sortedItems;
|
|
264
317
|
|
|
265
|
-
// Resolve `width: 'fill'` items to concrete column spans
|
|
318
|
+
// Resolve `width: 'fill'` items to concrete column spans; the
|
|
319
|
+
// resolver plans the row around each fill's span bounds.
|
|
266
320
|
const resolvedItemMap = useMemo( () => {
|
|
267
321
|
const fillWidths = resolveFillWidths(
|
|
268
322
|
items,
|
|
269
323
|
layoutMap,
|
|
270
|
-
effectiveColumns
|
|
324
|
+
effectiveColumns,
|
|
325
|
+
spanBoundsByKey
|
|
271
326
|
);
|
|
272
327
|
if ( fillWidths.size === 0 ) {
|
|
273
328
|
return layoutMap;
|
|
@@ -281,7 +336,7 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
|
|
|
281
336
|
);
|
|
282
337
|
}
|
|
283
338
|
return map;
|
|
284
|
-
}, [ items, layoutMap, effectiveColumns ] );
|
|
339
|
+
}, [ items, layoutMap, effectiveColumns, spanBoundsByKey ] );
|
|
285
340
|
|
|
286
341
|
const [ childrenMap, actionableAreaMap, remaining, renderedByKey ] =
|
|
287
342
|
useMemo( () => {
|
|
@@ -403,11 +458,27 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
|
|
|
403
458
|
return;
|
|
404
459
|
}
|
|
405
460
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
461
|
+
// Non-draggable items are pinned: they hold their index while
|
|
462
|
+
// the others reorder around them.
|
|
463
|
+
const updatedItems = arrayMoveWithPinned(
|
|
464
|
+
items,
|
|
465
|
+
currentIndex,
|
|
466
|
+
newIndex,
|
|
467
|
+
( key ) => layoutMap.get( key )?.draggable === false
|
|
468
|
+
);
|
|
469
|
+
if (
|
|
470
|
+
updatedItems.every( ( key, index ) => key === items[ index ] )
|
|
471
|
+
) {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
// Commit from the stored layout (or the pending commit), not
|
|
475
|
+
// the bounded view, so untouched tiles keep their stored spans.
|
|
476
|
+
const updatedLayout = ( latestLayoutRef.current ?? layout ).map(
|
|
477
|
+
( item ) => ( {
|
|
478
|
+
...item,
|
|
479
|
+
order: updatedItems.indexOf( item.key ),
|
|
480
|
+
} )
|
|
481
|
+
);
|
|
411
482
|
|
|
412
483
|
lastReorderCursorRef.current = {
|
|
413
484
|
x: activeCenterX,
|
|
@@ -458,16 +529,15 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
|
|
|
458
529
|
// with the live (already mutated) `activeLayout` width would
|
|
459
530
|
// compound and oscillate — and stepping back through the
|
|
460
531
|
// zero-delta zone would never restore the original size.
|
|
532
|
+
const bounds = spanBoundsByKey.get( id );
|
|
461
533
|
if ( ! resizeBaselineRef.current ) {
|
|
462
|
-
const baseItem =
|
|
463
|
-
( item ) => item.key === id
|
|
464
|
-
);
|
|
534
|
+
const baseItem = layoutMap.get( id );
|
|
465
535
|
const resolvedItem = resolvedItemMap.get( id );
|
|
466
536
|
// `'fill'`/`'full'` resize from the rendered span
|
|
467
537
|
// and convert to a numeric width.
|
|
468
538
|
let baseWidth: number;
|
|
469
539
|
if ( baseItem?.width === 'full' ) {
|
|
470
|
-
baseWidth = effectiveColumns;
|
|
540
|
+
baseWidth = bounds?.maxWidth ?? effectiveColumns;
|
|
471
541
|
} else if ( baseItem?.width === 'fill' ) {
|
|
472
542
|
baseWidth =
|
|
473
543
|
typeof resolvedItem?.width === 'number'
|
|
@@ -482,16 +552,15 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
|
|
|
482
552
|
};
|
|
483
553
|
}
|
|
484
554
|
const baseline = resizeBaselineRef.current;
|
|
485
|
-
const newWidth =
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
effectiveColumns
|
|
490
|
-
)
|
|
555
|
+
const newWidth = clampSpan(
|
|
556
|
+
baseline.width + relativeDelta.width,
|
|
557
|
+
bounds?.minWidth ?? 1,
|
|
558
|
+
bounds?.maxWidth ?? effectiveColumns
|
|
491
559
|
);
|
|
492
|
-
const newHeight =
|
|
493
|
-
|
|
494
|
-
|
|
560
|
+
const newHeight = clampSpan(
|
|
561
|
+
baseline.height + relativeDelta.height,
|
|
562
|
+
bounds?.minHeight ?? 1,
|
|
563
|
+
bounds?.maxHeight ?? Infinity
|
|
495
564
|
);
|
|
496
565
|
|
|
497
566
|
setResizeSnapPreview( {
|
|
@@ -512,8 +581,7 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
|
|
|
512
581
|
const pendingItem = latestLayoutRef.current?.find(
|
|
513
582
|
( item ) => item.key === id
|
|
514
583
|
);
|
|
515
|
-
const currentItem =
|
|
516
|
-
pendingItem ?? activeLayout.find( ( item ) => item.key === id );
|
|
584
|
+
const currentItem = pendingItem ?? layoutMap.get( id );
|
|
517
585
|
if (
|
|
518
586
|
currentItem &&
|
|
519
587
|
currentItem.width === newWidth &&
|
|
@@ -522,10 +590,14 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
|
|
|
522
590
|
return;
|
|
523
591
|
}
|
|
524
592
|
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
593
|
+
// Commit from the stored layout (or the pending commit), not
|
|
594
|
+
// the bounded view: the resized tile takes its new span and
|
|
595
|
+
// every other tile keeps its stored one.
|
|
596
|
+
const updatedLayout = ( latestLayoutRef.current ?? layout ).map(
|
|
597
|
+
( item ) =>
|
|
598
|
+
item.key === id
|
|
599
|
+
? { ...item, width: newWidth, height: newHeight }
|
|
600
|
+
: item
|
|
529
601
|
);
|
|
530
602
|
|
|
531
603
|
latestLayoutRef.current = updatedLayout;
|
|
@@ -652,34 +724,59 @@ export const DashboardGrid = forwardRef< HTMLDivElement, DashboardGridProps >(
|
|
|
652
724
|
} }
|
|
653
725
|
>
|
|
654
726
|
{ gridOverlay }
|
|
655
|
-
{ items.map( ( id ) =>
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
727
|
+
{ items.map( ( id ) => {
|
|
728
|
+
const limitsPx = resizeLimitsByKey.get( id );
|
|
729
|
+
return (
|
|
730
|
+
<GridItem
|
|
731
|
+
key={ id }
|
|
732
|
+
item={
|
|
733
|
+
resolvedItemMap.get(
|
|
734
|
+
id
|
|
735
|
+
) as DashboardGridLayoutItem
|
|
736
|
+
}
|
|
737
|
+
maxColumns={
|
|
738
|
+
spanBoundsByKey.get( id )?.maxWidth ??
|
|
739
|
+
effectiveColumns
|
|
740
|
+
}
|
|
741
|
+
disabled={ ! editMode }
|
|
742
|
+
draggable={
|
|
743
|
+
layoutMap.get( id )?.draggable !== false
|
|
744
|
+
}
|
|
745
|
+
resizable={
|
|
746
|
+
layoutMap.get( id )?.resizable !== false
|
|
747
|
+
}
|
|
748
|
+
verticalResizable={ rowHeight !== 'auto' }
|
|
749
|
+
interacting={
|
|
750
|
+
activeId !== null || isResizing
|
|
751
|
+
}
|
|
752
|
+
dragging={ activeId !== null }
|
|
753
|
+
onResize={ handleResize }
|
|
754
|
+
onResizeEnd={ persistTemporaryLayout }
|
|
755
|
+
resizeSnapPreview={
|
|
756
|
+
resizeSnapPreview?.id === id
|
|
757
|
+
? resizeSnapPreview.snap
|
|
758
|
+
: null
|
|
759
|
+
}
|
|
760
|
+
minResizeWidthPx={
|
|
761
|
+
limitsPx?.minWidthPx ?? minResizeWidthPx
|
|
762
|
+
}
|
|
763
|
+
minResizeHeightPx={
|
|
764
|
+
limitsPx?.minHeightPx ??
|
|
765
|
+
minResizeHeightPx
|
|
766
|
+
}
|
|
767
|
+
maxResizeWidthPx={ limitsPx?.maxWidthPx }
|
|
768
|
+
maxResizeHeightPx={
|
|
769
|
+
limitsPx?.maxHeightPx ?? undefined
|
|
770
|
+
}
|
|
771
|
+
actionableArea={ actionableAreaMap.get(
|
|
660
772
|
id
|
|
661
|
-
)
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
onResize={ handleResize }
|
|
669
|
-
onResizeEnd={ persistTemporaryLayout }
|
|
670
|
-
resizeSnapPreview={
|
|
671
|
-
resizeSnapPreview?.id === id
|
|
672
|
-
? resizeSnapPreview.snap
|
|
673
|
-
: null
|
|
674
|
-
}
|
|
675
|
-
minResizeWidthPx={ minResizeWidthPx }
|
|
676
|
-
minResizeHeightPx={ minResizeHeightPx }
|
|
677
|
-
actionableArea={ actionableAreaMap.get( id ) }
|
|
678
|
-
renderResizeHandle={ renderResizeHandle }
|
|
679
|
-
>
|
|
680
|
-
{ childrenMap.get( id ) }
|
|
681
|
-
</GridItem>
|
|
682
|
-
) ) }
|
|
773
|
+
) }
|
|
774
|
+
renderResizeHandle={ renderResizeHandle }
|
|
775
|
+
>
|
|
776
|
+
{ childrenMap.get( id ) }
|
|
777
|
+
</GridItem>
|
|
778
|
+
);
|
|
779
|
+
} ) }
|
|
683
780
|
{ remaining }
|
|
684
781
|
{ exitingItems.map( ( { key, rect, child } ) => (
|
|
685
782
|
<ItemExitOverlay
|
|
@@ -15,21 +15,31 @@ import type { DashboardGridLayoutItem } from './types';
|
|
|
15
15
|
* scanned (rows bounded by the sum of item heights), not only on
|
|
16
16
|
* `maxColumns`.
|
|
17
17
|
*
|
|
18
|
+
* Fill items honor per-item span bounds: the row plan reserves at least
|
|
19
|
+
* the fill's minimum span (wrapping it to the next free run when the
|
|
20
|
+
* current one is too narrow), and the resolved span never exceeds its
|
|
21
|
+
* maximum. A `'full'` item capped below the column count places like a
|
|
22
|
+
* fixed item of its capped width. Fixed-width items arrive already
|
|
23
|
+
* bounded by the caller.
|
|
24
|
+
*
|
|
18
25
|
* @param sortedKeys - Item keys in display order.
|
|
19
26
|
* @param layoutMap - Map of key to DashboardGridLayoutItem.
|
|
20
27
|
* @param maxColumns - Total columns in the grid.
|
|
28
|
+
* @param itemBounds - Optional per-key span bounds for fill and full items.
|
|
21
29
|
* @return Map of fill item keys to their resolved column spans.
|
|
22
30
|
*/
|
|
23
31
|
export function resolveFillWidths(
|
|
24
32
|
sortedKeys: string[],
|
|
25
33
|
layoutMap: Map< string, DashboardGridLayoutItem >,
|
|
26
|
-
maxColumns: number
|
|
34
|
+
maxColumns: number,
|
|
35
|
+
itemBounds?: ReadonlyMap< string, { minWidth: number; maxWidth: number } >
|
|
27
36
|
): Map< string, number > {
|
|
28
37
|
const resolved = new Map< string, number >();
|
|
29
38
|
const n = sortedKeys.length;
|
|
30
39
|
|
|
31
40
|
// Pre-extract items, clamp widths and heights, detect which path to take.
|
|
32
41
|
const items = new Array< DashboardGridLayoutItem | undefined >( n );
|
|
42
|
+
const kinds = new Array< 'fixed' | 'fill' | 'full' | undefined >( n );
|
|
33
43
|
const widths = new Array< number >( n );
|
|
34
44
|
const heights = new Array< number >( n );
|
|
35
45
|
let hasFill = false;
|
|
@@ -39,17 +49,31 @@ export function resolveFillWidths(
|
|
|
39
49
|
for ( let i = 0; i < n; i++ ) {
|
|
40
50
|
const item = layoutMap.get( sortedKeys[ i ] );
|
|
41
51
|
items[ i ] = item;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
52
|
+
if ( ! item ) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if ( item.width === 'fill' ) {
|
|
56
|
+
kinds[ i ] = 'fill';
|
|
57
|
+
widths[ i ] = 1;
|
|
58
|
+
hasFill = true;
|
|
59
|
+
} else if ( item.width === 'full' ) {
|
|
60
|
+
const cap = Math.min(
|
|
61
|
+
itemBounds?.get( item.key )?.maxWidth ?? maxColumns,
|
|
62
|
+
maxColumns
|
|
63
|
+
);
|
|
64
|
+
kinds[ i ] = cap < maxColumns ? 'fixed' : 'full';
|
|
65
|
+
widths[ i ] = cap;
|
|
66
|
+
} else {
|
|
67
|
+
kinds[ i ] = 'fixed';
|
|
68
|
+
widths[ i ] =
|
|
69
|
+
typeof item.width === 'number'
|
|
70
|
+
? Math.min( item.width, maxColumns )
|
|
71
|
+
: 1;
|
|
72
|
+
}
|
|
46
73
|
// Clamp to a positive integer so `0`, fractional, or negative
|
|
47
74
|
// values match the `|| 1` defaulting used in GridItem styles.
|
|
48
|
-
const h = Math.max( 1, Math.floor( item
|
|
75
|
+
const h = Math.max( 1, Math.floor( item.height ?? 1 ) );
|
|
49
76
|
heights[ i ] = h;
|
|
50
|
-
if ( item?.width === 'fill' ) {
|
|
51
|
-
hasFill = true;
|
|
52
|
-
}
|
|
53
77
|
if ( h > 1 ) {
|
|
54
78
|
hasMultiRow = true;
|
|
55
79
|
}
|
|
@@ -69,32 +93,34 @@ export function resolveFillWidths(
|
|
|
69
93
|
continue;
|
|
70
94
|
}
|
|
71
95
|
|
|
72
|
-
if (
|
|
96
|
+
if ( kinds[ i ] === 'full' ) {
|
|
73
97
|
currentCol = 0;
|
|
74
98
|
continue;
|
|
75
99
|
}
|
|
76
100
|
|
|
77
|
-
if (
|
|
101
|
+
if ( kinds[ i ] === 'fill' ) {
|
|
102
|
+
const bounds = itemBounds?.get( item.key );
|
|
103
|
+
const minW = Math.min( bounds?.minWidth ?? 1, maxColumns );
|
|
104
|
+
const maxW = bounds?.maxWidth ?? maxColumns;
|
|
105
|
+
// Wrap first when the row cannot host the fill's floor.
|
|
106
|
+
if ( currentCol + minW > maxColumns ) {
|
|
107
|
+
currentCol = 0;
|
|
108
|
+
}
|
|
78
109
|
let reserved = 0;
|
|
79
110
|
for ( let j = i + 1; j < n; j++ ) {
|
|
80
|
-
|
|
81
|
-
if (
|
|
82
|
-
! next ||
|
|
83
|
-
next.width === 'full' ||
|
|
84
|
-
next.width === 'fill'
|
|
85
|
-
) {
|
|
111
|
+
if ( kinds[ j ] !== 'fixed' ) {
|
|
86
112
|
break;
|
|
87
113
|
}
|
|
88
114
|
const nextW = widths[ j ];
|
|
89
|
-
if ( currentCol +
|
|
115
|
+
if ( currentCol + minW + reserved + nextW <= maxColumns ) {
|
|
90
116
|
reserved += nextW;
|
|
91
117
|
} else {
|
|
92
118
|
break;
|
|
93
119
|
}
|
|
94
120
|
}
|
|
95
|
-
const fillCols = Math.
|
|
96
|
-
|
|
97
|
-
|
|
121
|
+
const fillCols = Math.min(
|
|
122
|
+
Math.max( minW, maxColumns - currentCol - reserved ),
|
|
123
|
+
maxW
|
|
98
124
|
);
|
|
99
125
|
resolved.set( item.key, fillCols );
|
|
100
126
|
currentCol += fillCols;
|
|
@@ -121,6 +147,30 @@ export function resolveFillWidths(
|
|
|
121
147
|
let cursorRow = 0;
|
|
122
148
|
let cursorCol = 0;
|
|
123
149
|
|
|
150
|
+
// First row and column, in row-major order from the cursor, where
|
|
151
|
+
// `w` consecutive columns are free.
|
|
152
|
+
const findFreeRun = ( w: number ): [ number, number ] => {
|
|
153
|
+
let r = cursorRow;
|
|
154
|
+
let c = cursorCol;
|
|
155
|
+
place: for ( ; r <= totalRows; r++ ) {
|
|
156
|
+
c = r === cursorRow ? cursorCol : 0;
|
|
157
|
+
while ( c + w <= maxColumns ) {
|
|
158
|
+
let blocked = -1;
|
|
159
|
+
for ( let k = 0; k < w; k++ ) {
|
|
160
|
+
if ( rowOccupancy[ c + k ] > r ) {
|
|
161
|
+
blocked = c + k;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if ( blocked === -1 ) {
|
|
166
|
+
break place;
|
|
167
|
+
}
|
|
168
|
+
c = blocked + 1;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return [ r, c ];
|
|
172
|
+
};
|
|
173
|
+
|
|
124
174
|
for ( let i = 0; i < n; i++ ) {
|
|
125
175
|
const item = items[ i ];
|
|
126
176
|
if ( ! item ) {
|
|
@@ -129,7 +179,7 @@ export function resolveFillWidths(
|
|
|
129
179
|
|
|
130
180
|
const h = heights[ i ];
|
|
131
181
|
|
|
132
|
-
if (
|
|
182
|
+
if ( kinds[ i ] === 'full' ) {
|
|
133
183
|
let r = cursorRow;
|
|
134
184
|
for ( let c = 0; c < maxColumns; c++ ) {
|
|
135
185
|
if ( rowOccupancy[ c ] > r ) {
|
|
@@ -144,20 +194,12 @@ export function resolveFillWidths(
|
|
|
144
194
|
continue;
|
|
145
195
|
}
|
|
146
196
|
|
|
147
|
-
if (
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if ( rowOccupancy[ c ] <= r ) {
|
|
154
|
-
break scan;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
const fillStartRow = r;
|
|
159
|
-
const fillStartCol = c;
|
|
160
|
-
let runLength = 0;
|
|
197
|
+
if ( kinds[ i ] === 'fill' ) {
|
|
198
|
+
const bounds = itemBounds?.get( item.key );
|
|
199
|
+
const minW = Math.min( bounds?.minWidth ?? 1, maxColumns );
|
|
200
|
+
const maxW = bounds?.maxWidth ?? maxColumns;
|
|
201
|
+
const [ fillStartRow, fillStartCol ] = findFreeRun( minW );
|
|
202
|
+
let runLength = minW;
|
|
161
203
|
while (
|
|
162
204
|
fillStartCol + runLength < maxColumns &&
|
|
163
205
|
rowOccupancy[ fillStartCol + runLength ] <= fillStartRow
|
|
@@ -166,22 +208,20 @@ export function resolveFillWidths(
|
|
|
166
208
|
}
|
|
167
209
|
let reserved = 0;
|
|
168
210
|
for ( let j = i + 1; j < n; j++ ) {
|
|
169
|
-
|
|
170
|
-
if (
|
|
171
|
-
! next ||
|
|
172
|
-
next.width === 'full' ||
|
|
173
|
-
next.width === 'fill'
|
|
174
|
-
) {
|
|
211
|
+
if ( kinds[ j ] !== 'fixed' ) {
|
|
175
212
|
break;
|
|
176
213
|
}
|
|
177
214
|
const nextW = widths[ j ];
|
|
178
|
-
if (
|
|
215
|
+
if ( minW + reserved + nextW <= runLength ) {
|
|
179
216
|
reserved += nextW;
|
|
180
217
|
} else {
|
|
181
218
|
break;
|
|
182
219
|
}
|
|
183
220
|
}
|
|
184
|
-
const fillCols = Math.
|
|
221
|
+
const fillCols = Math.min(
|
|
222
|
+
Math.max( minW, runLength - reserved ),
|
|
223
|
+
maxW
|
|
224
|
+
);
|
|
185
225
|
resolved.set( item.key, fillCols );
|
|
186
226
|
for ( let k = 0; k < fillCols; k++ ) {
|
|
187
227
|
rowOccupancy[ fillStartCol + k ] = fillStartRow + h;
|
|
@@ -192,24 +232,7 @@ export function resolveFillWidths(
|
|
|
192
232
|
}
|
|
193
233
|
|
|
194
234
|
const w = widths[ i ];
|
|
195
|
-
|
|
196
|
-
let c = cursorCol;
|
|
197
|
-
place: for ( ; r <= totalRows; r++ ) {
|
|
198
|
-
c = r === cursorRow ? cursorCol : 0;
|
|
199
|
-
while ( c + w <= maxColumns ) {
|
|
200
|
-
let blocked = -1;
|
|
201
|
-
for ( let k = 0; k < w; k++ ) {
|
|
202
|
-
if ( rowOccupancy[ c + k ] > r ) {
|
|
203
|
-
blocked = c + k;
|
|
204
|
-
break;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if ( blocked === -1 ) {
|
|
208
|
-
break place;
|
|
209
|
-
}
|
|
210
|
-
c = blocked + 1;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
235
|
+
const [ r, c ] = findFreeRun( w );
|
|
213
236
|
for ( let k = 0; k < w; k++ ) {
|
|
214
237
|
rowOccupancy[ c + k ] = r + h;
|
|
215
238
|
}
|