@wordpress/grid 0.6.0 → 0.7.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 +14 -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 +107 -45
- 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 +59 -17
- 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 +108 -47
- 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 +60 -19
- 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 +11 -11
- package/src/dashboard-grid/grid-item.tsx +21 -7
- package/src/dashboard-grid/index.tsx +154 -57
- package/src/dashboard-grid/resolve-fill-widths.ts +85 -62
- package/src/dashboard-grid/stories/index.story.tsx +154 -0
- package/src/dashboard-grid/test/item-interactions.jsdom.test.tsx +64 -0
- package/src/dashboard-grid/test/item-limits.jsdom.test.tsx +147 -0
- package/src/dashboard-grid/test/{keyboard-activation.test.tsx → keyboard-activation.jsdom.test.tsx} +4 -3
- package/src/dashboard-grid/test/resolve-fill-widths.test.ts +159 -0
- package/src/dashboard-grid/types.ts +50 -0
- package/src/dashboard-lanes/index.tsx +86 -24
- package/src/dashboard-lanes/lanes-item.tsx +34 -6
- package/src/dashboard-lanes/stories/index.story.tsx +119 -0
- package/src/dashboard-lanes/test/item-interactions.jsdom.test.tsx +64 -0
- package/src/dashboard-lanes/test/item-limits.jsdom.test.tsx +140 -0
- package/src/dashboard-lanes/test/{keyboard-activation.test.tsx → keyboard-activation.jsdom.test.tsx} +4 -3
- package/src/dashboard-lanes/test/lane-placement.test.ts +1 -0
- package/src/dashboard-lanes/test/{use-lane-placement.test.tsx → use-lane-placement.jsdom.test.tsx} +11 -10
- 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 +40 -0
- package/src/shared/test/resize-snap.test.ts +171 -2
- package/src/shared/test/use-span-bounds.jsdom.test.ts +105 -0
- package/src/shared/types.ts +35 -0
- package/src/shared/use-span-bounds.ts +99 -0
|
@@ -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';
|
|
@@ -26,8 +25,10 @@ import {
|
|
|
26
25
|
} from '@wordpress/element';
|
|
27
26
|
import { LanesItem } from './lanes-item';
|
|
28
27
|
import { useLanePlacement } from './use-lane-placement';
|
|
28
|
+
import { arrayMoveWithPinned } from '../shared/array-move-with-pinned';
|
|
29
29
|
import { GridOverlay } from '../shared/grid-overlay';
|
|
30
|
-
import { gridSpanToPixelSize } from '../shared/resize-snap';
|
|
30
|
+
import { clampSpan, gridSpanToPixelSize } from '../shared/resize-snap';
|
|
31
|
+
import { useResizePixelLimits, useSpanBounds } from '../shared/use-span-bounds';
|
|
31
32
|
import layoutAnimationStyles from '../shared/layout-shift-animation.module.css';
|
|
32
33
|
import { ItemExitOverlay } from '../shared/item-exit-overlay';
|
|
33
34
|
import {
|
|
@@ -109,6 +110,7 @@ export const DashboardLanes = forwardRef< HTMLDivElement, DashboardLanesProps >(
|
|
|
109
110
|
flowTolerance = 16,
|
|
110
111
|
rowUnit = 4,
|
|
111
112
|
minColumnWidth,
|
|
113
|
+
itemLimits,
|
|
112
114
|
editMode = false,
|
|
113
115
|
onChangeLayout,
|
|
114
116
|
onPreviewLayout,
|
|
@@ -197,6 +199,21 @@ export const DashboardLanes = forwardRef< HTMLDivElement, DashboardLanesProps >(
|
|
|
197
199
|
null
|
|
198
200
|
).widthPx;
|
|
199
201
|
|
|
202
|
+
// Height limits do not apply: lane heights are content-driven.
|
|
203
|
+
const widthBoundsByKey = useSpanBounds(
|
|
204
|
+
itemLimits,
|
|
205
|
+
columnWidth,
|
|
206
|
+
gapPx,
|
|
207
|
+
null,
|
|
208
|
+
effectiveColumns
|
|
209
|
+
);
|
|
210
|
+
const resizeLimitsByKey = useResizePixelLimits(
|
|
211
|
+
widthBoundsByKey,
|
|
212
|
+
columnWidth,
|
|
213
|
+
gapPx,
|
|
214
|
+
null
|
|
215
|
+
);
|
|
216
|
+
|
|
200
217
|
const layoutMap = useMemo( () => {
|
|
201
218
|
const map = new Map< string, DashboardLanesLayoutItem >();
|
|
202
219
|
activeLayout.forEach( ( item ) => map.set( item.key, item ) );
|
|
@@ -225,21 +242,42 @@ export const DashboardLanes = forwardRef< HTMLDivElement, DashboardLanesProps >(
|
|
|
225
242
|
);
|
|
226
243
|
const items = sortedItems;
|
|
227
244
|
|
|
228
|
-
//
|
|
245
|
+
// Span each item renders at: the stored width clamped to the lane
|
|
246
|
+
// count and to the item's width bounds. Placement, the resize
|
|
247
|
+
// baseline, and the commit check all read it, so a gesture starts
|
|
248
|
+
// from the span on screen and commits nothing until it leaves it.
|
|
249
|
+
const renderedSpanByKey = useMemo( () => {
|
|
250
|
+
const map = new Map< string, number >();
|
|
251
|
+
for ( const [ key, item ] of layoutMap ) {
|
|
252
|
+
const span =
|
|
253
|
+
typeof item.width === 'number'
|
|
254
|
+
? Math.max(
|
|
255
|
+
1,
|
|
256
|
+
Math.min( item.width, effectiveColumns )
|
|
257
|
+
)
|
|
258
|
+
: 1;
|
|
259
|
+
const bounds = widthBoundsByKey.get( key );
|
|
260
|
+
map.set(
|
|
261
|
+
key,
|
|
262
|
+
bounds
|
|
263
|
+
? clampSpan( span, bounds.minWidth, bounds.maxWidth )
|
|
264
|
+
: span
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
return map;
|
|
268
|
+
}, [ layoutMap, effectiveColumns, widthBoundsByKey ] );
|
|
269
|
+
|
|
270
|
+
// Placement input for the hook: each item with its rendered span
|
|
229
271
|
// in source (sorted) order. `lane` forwards the optional explicit
|
|
230
272
|
// pin from the layout item; the algorithm clamps out-of-range
|
|
231
273
|
// values, so no surface-level guard is needed.
|
|
232
274
|
const placementItems = useMemo( () => {
|
|
233
|
-
return items.map( ( key ) => {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
: 1;
|
|
240
|
-
return { key, span, lane: item?.lane };
|
|
241
|
-
} );
|
|
242
|
-
}, [ items, layoutMap, effectiveColumns ] );
|
|
275
|
+
return items.map( ( key ) => ( {
|
|
276
|
+
key,
|
|
277
|
+
span: renderedSpanByKey.get( key ) ?? 1,
|
|
278
|
+
lane: layoutMap.get( key )?.lane,
|
|
279
|
+
} ) );
|
|
280
|
+
}, [ items, layoutMap, renderedSpanByKey ] );
|
|
243
281
|
|
|
244
282
|
const { itemStyles } = useLanePlacement( container, {
|
|
245
283
|
items: placementItems,
|
|
@@ -366,7 +404,19 @@ export const DashboardLanes = forwardRef< HTMLDivElement, DashboardLanesProps >(
|
|
|
366
404
|
return;
|
|
367
405
|
}
|
|
368
406
|
|
|
369
|
-
|
|
407
|
+
// Non-draggable items are pinned: they hold their index while
|
|
408
|
+
// the others reorder around them.
|
|
409
|
+
const updatedItems = arrayMoveWithPinned(
|
|
410
|
+
items,
|
|
411
|
+
currentIndex,
|
|
412
|
+
newIndex,
|
|
413
|
+
( key ) => layoutMap.get( key )?.draggable === false
|
|
414
|
+
);
|
|
415
|
+
if (
|
|
416
|
+
updatedItems.every( ( key, index ) => key === items[ index ] )
|
|
417
|
+
) {
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
370
420
|
// Build a key→index lookup so the .map below is O(n)
|
|
371
421
|
// instead of O(n²) from per-item `indexOf` calls.
|
|
372
422
|
const orderByKey = new Map< string, number >();
|
|
@@ -416,15 +466,14 @@ export const DashboardLanes = forwardRef< HTMLDivElement, DashboardLanesProps >(
|
|
|
416
466
|
);
|
|
417
467
|
|
|
418
468
|
if ( resizeBaselineRef.current === null ) {
|
|
419
|
-
|
|
420
|
-
const baseWidth =
|
|
421
|
-
typeof baseItem?.width === 'number' ? baseItem.width : 1;
|
|
422
|
-
resizeBaselineRef.current = baseWidth;
|
|
469
|
+
resizeBaselineRef.current = renderedSpanByKey.get( id ) ?? 1;
|
|
423
470
|
}
|
|
424
471
|
const baseline = resizeBaselineRef.current;
|
|
425
|
-
const
|
|
426
|
-
|
|
427
|
-
|
|
472
|
+
const bounds = widthBoundsByKey.get( id );
|
|
473
|
+
const newWidth = clampSpan(
|
|
474
|
+
baseline + relativeDelta,
|
|
475
|
+
bounds?.minWidth ?? 1,
|
|
476
|
+
bounds?.maxWidth ?? effectiveColumns
|
|
428
477
|
);
|
|
429
478
|
|
|
430
479
|
setResizeSnapPreview( {
|
|
@@ -438,11 +487,14 @@ export const DashboardLanes = forwardRef< HTMLDivElement, DashboardLanesProps >(
|
|
|
438
487
|
),
|
|
439
488
|
} );
|
|
440
489
|
|
|
490
|
+
// Bail when the snap target matches the span staged for commit,
|
|
491
|
+
// or the rendered span while nothing is staged.
|
|
441
492
|
const pendingItem = latestLayoutRef.current?.find(
|
|
442
493
|
( item ) => item.key === id
|
|
443
494
|
);
|
|
444
|
-
const
|
|
445
|
-
|
|
495
|
+
const currentWidth =
|
|
496
|
+
pendingItem?.width ?? renderedSpanByKey.get( id );
|
|
497
|
+
if ( currentWidth === newWidth ) {
|
|
446
498
|
return;
|
|
447
499
|
}
|
|
448
500
|
|
|
@@ -569,6 +621,7 @@ export const DashboardLanes = forwardRef< HTMLDivElement, DashboardLanesProps >(
|
|
|
569
621
|
if ( ! child ) {
|
|
570
622
|
return null;
|
|
571
623
|
}
|
|
624
|
+
const limitsPx = resizeLimitsByKey.get( id );
|
|
572
625
|
return (
|
|
573
626
|
<LanesItem
|
|
574
627
|
key={ id }
|
|
@@ -577,6 +630,12 @@ export const DashboardLanes = forwardRef< HTMLDivElement, DashboardLanesProps >(
|
|
|
577
630
|
itemStyles.get( id ) ?? {}
|
|
578
631
|
}
|
|
579
632
|
disabled={ ! editMode }
|
|
633
|
+
draggable={
|
|
634
|
+
layoutMap.get( id )?.draggable !== false
|
|
635
|
+
}
|
|
636
|
+
resizable={
|
|
637
|
+
layoutMap.get( id )?.resizable !== false
|
|
638
|
+
}
|
|
580
639
|
interacting={ interacting }
|
|
581
640
|
dragging={ activeId !== null }
|
|
582
641
|
onResize={ handleResize }
|
|
@@ -586,7 +645,10 @@ export const DashboardLanes = forwardRef< HTMLDivElement, DashboardLanesProps >(
|
|
|
586
645
|
? resizeSnapPreview.snap
|
|
587
646
|
: null
|
|
588
647
|
}
|
|
589
|
-
minResizeWidthPx={
|
|
648
|
+
minResizeWidthPx={
|
|
649
|
+
limitsPx?.minWidthPx ?? minResizeWidthPx
|
|
650
|
+
}
|
|
651
|
+
maxResizeWidthPx={ limitsPx?.maxWidthPx }
|
|
590
652
|
actionableArea={ actionableAreaMap.get(
|
|
591
653
|
id
|
|
592
654
|
) }
|
|
@@ -46,6 +46,20 @@ export type LanesItemProps = {
|
|
|
46
46
|
*/
|
|
47
47
|
disabled?: boolean;
|
|
48
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Whether the item can be dragged. Combined with `disabled`.
|
|
51
|
+
*
|
|
52
|
+
* @default true
|
|
53
|
+
*/
|
|
54
|
+
draggable?: boolean;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Whether the item can be resized. Combined with `disabled`.
|
|
58
|
+
*
|
|
59
|
+
* @default true
|
|
60
|
+
*/
|
|
61
|
+
resizable?: boolean;
|
|
62
|
+
|
|
49
63
|
/**
|
|
50
64
|
* Whether any tile in the surface is currently being dragged or
|
|
51
65
|
* resized. Drives the drag activator cursor.
|
|
@@ -77,6 +91,12 @@ export type LanesItemProps = {
|
|
|
77
91
|
*/
|
|
78
92
|
minResizeWidthPx: number;
|
|
79
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Maximum tile width while resizing, in pixels. Omitted when the
|
|
96
|
+
* item declares no width limit.
|
|
97
|
+
*/
|
|
98
|
+
maxResizeWidthPx?: number;
|
|
99
|
+
|
|
80
100
|
onResizeEnd: () => void;
|
|
81
101
|
|
|
82
102
|
renderResizeHandle?: React.ComponentType< ResizeHandleRenderProps >;
|
|
@@ -86,6 +106,8 @@ export function LanesItem( {
|
|
|
86
106
|
itemKey,
|
|
87
107
|
placementStyle,
|
|
88
108
|
disabled = false,
|
|
109
|
+
draggable = true,
|
|
110
|
+
resizable = true,
|
|
89
111
|
interacting = false,
|
|
90
112
|
children,
|
|
91
113
|
actionableArea = null,
|
|
@@ -93,6 +115,7 @@ export function LanesItem( {
|
|
|
93
115
|
onResizeEnd,
|
|
94
116
|
resizeSnapPreview = null,
|
|
95
117
|
minResizeWidthPx,
|
|
118
|
+
maxResizeWidthPx,
|
|
96
119
|
renderResizeHandle,
|
|
97
120
|
dragging = false,
|
|
98
121
|
}: LanesItemProps ) {
|
|
@@ -106,6 +129,8 @@ export function LanesItem( {
|
|
|
106
129
|
const itemRef = useRef< HTMLDivElement >( null );
|
|
107
130
|
const contentRef = useRef< HTMLDivElement >( null );
|
|
108
131
|
|
|
132
|
+
const dragDisabled = disabled || ! draggable;
|
|
133
|
+
const resizeDisabled = disabled || ! resizable;
|
|
109
134
|
const {
|
|
110
135
|
attributes,
|
|
111
136
|
listeners,
|
|
@@ -114,7 +139,7 @@ export function LanesItem( {
|
|
|
114
139
|
isDragging,
|
|
115
140
|
} = useSortable( {
|
|
116
141
|
id: itemKey,
|
|
117
|
-
disabled,
|
|
142
|
+
disabled: dragDisabled,
|
|
118
143
|
} );
|
|
119
144
|
const mergedRef = useMergeRefs( [ itemRef, setNodeRef ] );
|
|
120
145
|
const contentMergedRef = useMergeRefs( [ contentRef ] );
|
|
@@ -141,9 +166,12 @@ export function LanesItem( {
|
|
|
141
166
|
}
|
|
142
167
|
let clamped: ResizeDelta = { width: delta.width, height: 0 };
|
|
143
168
|
if ( baselineSize ) {
|
|
144
|
-
clamped = clampResizeDelta(
|
|
145
|
-
|
|
146
|
-
|
|
169
|
+
clamped = clampResizeDelta(
|
|
170
|
+
clamped,
|
|
171
|
+
baselineSize,
|
|
172
|
+
{ width: minResizeWidthPx },
|
|
173
|
+
{ width: maxResizeWidthPx }
|
|
174
|
+
);
|
|
147
175
|
}
|
|
148
176
|
setResizeDelta( clamped );
|
|
149
177
|
onResize( itemKey, clamped );
|
|
@@ -199,7 +227,7 @@ export function LanesItem( {
|
|
|
199
227
|
{ ...listeners }
|
|
200
228
|
style={ {
|
|
201
229
|
height: '100%',
|
|
202
|
-
cursor: getItemCursor(
|
|
230
|
+
cursor: getItemCursor( dragDisabled, interacting ),
|
|
203
231
|
} }
|
|
204
232
|
>
|
|
205
233
|
<div
|
|
@@ -208,7 +236,7 @@ export function LanesItem( {
|
|
|
208
236
|
style={ continuousContentStyle }
|
|
209
237
|
>
|
|
210
238
|
{ children }
|
|
211
|
-
{ !
|
|
239
|
+
{ ! resizeDisabled && (
|
|
212
240
|
<ResizeHandle
|
|
213
241
|
itemId={ itemKey }
|
|
214
242
|
verticalResizable={ false }
|
|
@@ -379,6 +379,125 @@ export const EditMode: Story = {
|
|
|
379
379
|
},
|
|
380
380
|
};
|
|
381
381
|
|
|
382
|
+
/**
|
|
383
|
+
* Per-item width limits via `itemLimits`, in pixels. The wide tile
|
|
384
|
+
* stops shrinking at its minimum, the capped tile stops growing at its
|
|
385
|
+
* maximum, the ranged tile stays between both, and the free tiles keep
|
|
386
|
+
* the full range. Lanes take no height limits; lane heights are
|
|
387
|
+
* content-driven.
|
|
388
|
+
*/
|
|
389
|
+
export const SizeLimits: Story = {
|
|
390
|
+
name: 'Per-Item Size Limits',
|
|
391
|
+
parameters: {
|
|
392
|
+
// FIXME: Resize handles are nameless aria commands; edit mode nests interactive controls (aria-command-name, nested-interactive).
|
|
393
|
+
// See: https://github.com/WordPress/gutenberg/issues/81596
|
|
394
|
+
a11y: { test: 'todo' },
|
|
395
|
+
},
|
|
396
|
+
args: {
|
|
397
|
+
columns: 12,
|
|
398
|
+
editMode: true,
|
|
399
|
+
itemLimits: {
|
|
400
|
+
min: { minWidth: 360 },
|
|
401
|
+
max: { maxWidth: 500 },
|
|
402
|
+
ranged: { minWidth: 300, maxWidth: 500 },
|
|
403
|
+
},
|
|
404
|
+
},
|
|
405
|
+
render: function SizeLimitsStory( args ) {
|
|
406
|
+
const initial: ( DashboardLanesLayoutItem & {
|
|
407
|
+
tone: Tone;
|
|
408
|
+
height: number;
|
|
409
|
+
label: string;
|
|
410
|
+
} )[] = [
|
|
411
|
+
{
|
|
412
|
+
key: 'min',
|
|
413
|
+
width: 2,
|
|
414
|
+
tone: 'info',
|
|
415
|
+
height: 140,
|
|
416
|
+
label: 'min 360px wide',
|
|
417
|
+
},
|
|
418
|
+
{
|
|
419
|
+
key: 'max',
|
|
420
|
+
width: 3,
|
|
421
|
+
tone: 'warning',
|
|
422
|
+
height: 120,
|
|
423
|
+
label: 'max 500px wide',
|
|
424
|
+
},
|
|
425
|
+
{
|
|
426
|
+
key: 'ranged',
|
|
427
|
+
width: 2,
|
|
428
|
+
tone: 'brand',
|
|
429
|
+
height: 150,
|
|
430
|
+
label: 'min 300, max 500px wide',
|
|
431
|
+
},
|
|
432
|
+
{
|
|
433
|
+
key: 'free-a',
|
|
434
|
+
width: 3,
|
|
435
|
+
tone: 'neutral',
|
|
436
|
+
height: 180,
|
|
437
|
+
label: 'no limits',
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
key: 'free-b',
|
|
441
|
+
width: 2,
|
|
442
|
+
tone: 'neutral',
|
|
443
|
+
height: 100,
|
|
444
|
+
label: 'no limits',
|
|
445
|
+
},
|
|
446
|
+
{
|
|
447
|
+
key: 'free-c',
|
|
448
|
+
width: 3,
|
|
449
|
+
tone: 'neutral',
|
|
450
|
+
height: 160,
|
|
451
|
+
label: 'no limits',
|
|
452
|
+
},
|
|
453
|
+
];
|
|
454
|
+
|
|
455
|
+
const [ tiles, setTiles ] = useState( initial );
|
|
456
|
+
|
|
457
|
+
const layout: DashboardLanesLayoutItem[] = tiles.map(
|
|
458
|
+
( { tone: _tone, height: _height, label: _label, ...item } ) => item
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
const onChangeLayout = ( next: DashboardLanesLayoutItem[] ) => {
|
|
462
|
+
setTiles(
|
|
463
|
+
next.map( ( item ) => {
|
|
464
|
+
const existing = tiles.find( ( t ) => t.key === item.key );
|
|
465
|
+
return {
|
|
466
|
+
...item,
|
|
467
|
+
tone: existing?.tone ?? 'neutral',
|
|
468
|
+
height: existing?.height ?? 100,
|
|
469
|
+
label: existing?.label ?? item.key,
|
|
470
|
+
};
|
|
471
|
+
} )
|
|
472
|
+
);
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
const tileElements = useMemo(
|
|
476
|
+
() =>
|
|
477
|
+
tiles.map( ( tile ) => (
|
|
478
|
+
<Tile
|
|
479
|
+
key={ tile.key }
|
|
480
|
+
tone={ tile.tone }
|
|
481
|
+
height={ tile.height }
|
|
482
|
+
>
|
|
483
|
+
{ tile.label }
|
|
484
|
+
</Tile>
|
|
485
|
+
) ),
|
|
486
|
+
[ tiles ]
|
|
487
|
+
);
|
|
488
|
+
|
|
489
|
+
return (
|
|
490
|
+
<DashboardLanes
|
|
491
|
+
{ ...args }
|
|
492
|
+
layout={ layout }
|
|
493
|
+
onChangeLayout={ onChangeLayout }
|
|
494
|
+
>
|
|
495
|
+
{ tileElements }
|
|
496
|
+
</DashboardLanes>
|
|
497
|
+
);
|
|
498
|
+
},
|
|
499
|
+
};
|
|
500
|
+
|
|
382
501
|
/**
|
|
383
502
|
* Example custom overlay supplied to `<DashboardLanes />` through the
|
|
384
503
|
* `renderGridOverlay` prop. Receives `{ columns, isActive }` from the
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import { DashboardLanes } from '..';
|
|
4
|
+
|
|
5
|
+
class MockResizeObserver {
|
|
6
|
+
observe() {}
|
|
7
|
+
unobserve() {}
|
|
8
|
+
disconnect() {}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let originalResizeObserver: typeof ResizeObserver;
|
|
12
|
+
|
|
13
|
+
beforeEach( () => {
|
|
14
|
+
originalResizeObserver = globalThis.ResizeObserver;
|
|
15
|
+
( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
16
|
+
MockResizeObserver;
|
|
17
|
+
} );
|
|
18
|
+
|
|
19
|
+
afterEach( () => {
|
|
20
|
+
( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
21
|
+
originalResizeObserver;
|
|
22
|
+
} );
|
|
23
|
+
|
|
24
|
+
/* eslint-disable testing-library/no-container */
|
|
25
|
+
function renderItem( flags: { draggable?: boolean; resizable?: boolean } ) {
|
|
26
|
+
const { container } = render(
|
|
27
|
+
<DashboardLanes
|
|
28
|
+
layout={ [ { key: 'a', width: 1, ...flags } ] }
|
|
29
|
+
columns={ 2 }
|
|
30
|
+
editMode
|
|
31
|
+
>
|
|
32
|
+
<div key="a">A</div>
|
|
33
|
+
</DashboardLanes>
|
|
34
|
+
);
|
|
35
|
+
return {
|
|
36
|
+
activator: container.querySelector(
|
|
37
|
+
'[aria-roledescription="sortable"]'
|
|
38
|
+
),
|
|
39
|
+
resizeHandle: container.querySelector(
|
|
40
|
+
'[aria-roledescription="draggable"]'
|
|
41
|
+
),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/* eslint-enable testing-library/no-container */
|
|
45
|
+
|
|
46
|
+
describe( 'DashboardLanes item interactions', () => {
|
|
47
|
+
it( 'drags and resizes by default in edit mode', () => {
|
|
48
|
+
const { activator, resizeHandle } = renderItem( {} );
|
|
49
|
+
expect( activator ).toHaveAttribute( 'aria-disabled', 'false' );
|
|
50
|
+
expect( resizeHandle ).not.toBeNull();
|
|
51
|
+
} );
|
|
52
|
+
|
|
53
|
+
it( 'keeps an item in place when draggable is false', () => {
|
|
54
|
+
const { activator, resizeHandle } = renderItem( { draggable: false } );
|
|
55
|
+
expect( activator ).toHaveAttribute( 'aria-disabled', 'true' );
|
|
56
|
+
expect( resizeHandle ).not.toBeNull();
|
|
57
|
+
} );
|
|
58
|
+
|
|
59
|
+
it( 'keeps an item at its size when resizable is false', () => {
|
|
60
|
+
const { activator, resizeHandle } = renderItem( { resizable: false } );
|
|
61
|
+
expect( activator ).toHaveAttribute( 'aria-disabled', 'false' );
|
|
62
|
+
expect( resizeHandle ).toBeNull();
|
|
63
|
+
} );
|
|
64
|
+
} );
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { act, fireEvent, render } from '@testing-library/react';
|
|
2
|
+
import { DashboardLanes } from '..';
|
|
3
|
+
import type { DashboardLanesLayoutItem } from '../types';
|
|
4
|
+
|
|
5
|
+
class MockResizeObserver {
|
|
6
|
+
observe() {}
|
|
7
|
+
unobserve() {}
|
|
8
|
+
disconnect() {}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let originalResizeObserver: typeof ResizeObserver;
|
|
12
|
+
let originalGetBoundingClientRect: typeof HTMLElement.prototype.getBoundingClientRect;
|
|
13
|
+
|
|
14
|
+
function rect( left: number, top: number, width: number, height: number ) {
|
|
15
|
+
return {
|
|
16
|
+
left,
|
|
17
|
+
top,
|
|
18
|
+
width,
|
|
19
|
+
height,
|
|
20
|
+
right: left + width,
|
|
21
|
+
bottom: top + height,
|
|
22
|
+
x: left,
|
|
23
|
+
y: top,
|
|
24
|
+
toJSON() {},
|
|
25
|
+
} as DOMRect;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Six lanes with the fallback 24px gap. `a` stores one lane and
|
|
29
|
+
// declares a 60px floor that quantizes to two lanes in both geometries
|
|
30
|
+
// below; its rect mirrors that rendered width so the pixel clamp
|
|
31
|
+
// measures the tile on screen. A keyboard step is 25px: one lane on
|
|
32
|
+
// 44px tracks (240px container), none on 64px tracks (360px container).
|
|
33
|
+
const LANES = 6;
|
|
34
|
+
const GAP = 24;
|
|
35
|
+
let containerRect = rect( 0, 0, 0, 0 );
|
|
36
|
+
let itemRects: Record< string, DOMRect > = {};
|
|
37
|
+
|
|
38
|
+
function setGeometry( containerWidth: number ) {
|
|
39
|
+
const laneWidth = ( containerWidth - ( LANES - 1 ) * GAP ) / LANES;
|
|
40
|
+
containerRect = rect( 0, 0, containerWidth, 600 );
|
|
41
|
+
itemRects = { a: rect( 0, 0, 2 * laneWidth + GAP, 100 ) };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Space picks the handle up, one arrow step moves 25px, Space drops.
|
|
45
|
+
// dnd-kit binds its keyboard listener on a timeout after pickup and
|
|
46
|
+
// settles the drop through a microtask and timers, so both flushes run
|
|
47
|
+
// inside `act`.
|
|
48
|
+
async function activateAndStepRight( activator: Element ) {
|
|
49
|
+
fireEvent.keyDown( activator, { code: 'Space' } );
|
|
50
|
+
act( () => {
|
|
51
|
+
jest.runOnlyPendingTimers();
|
|
52
|
+
} );
|
|
53
|
+
fireEvent.keyDown( activator, { code: 'ArrowRight' } );
|
|
54
|
+
fireEvent.keyDown( activator, { code: 'Space' } );
|
|
55
|
+
await act( async () => {
|
|
56
|
+
jest.runOnlyPendingTimers();
|
|
57
|
+
} );
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
beforeEach( () => {
|
|
61
|
+
jest.useFakeTimers();
|
|
62
|
+
originalResizeObserver = global.ResizeObserver;
|
|
63
|
+
( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
64
|
+
MockResizeObserver;
|
|
65
|
+
originalGetBoundingClientRect = HTMLElement.prototype.getBoundingClientRect;
|
|
66
|
+
HTMLElement.prototype.getBoundingClientRect = function () {
|
|
67
|
+
// eslint-disable-next-line testing-library/no-node-access
|
|
68
|
+
const key = this.closest( '[data-wp-grid-item-key]' )?.getAttribute(
|
|
69
|
+
'data-wp-grid-item-key'
|
|
70
|
+
);
|
|
71
|
+
return ( key && itemRects[ key ] ) || containerRect;
|
|
72
|
+
};
|
|
73
|
+
} );
|
|
74
|
+
|
|
75
|
+
afterEach( () => {
|
|
76
|
+
( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
77
|
+
originalResizeObserver;
|
|
78
|
+
HTMLElement.prototype.getBoundingClientRect = originalGetBoundingClientRect;
|
|
79
|
+
jest.useRealTimers();
|
|
80
|
+
} );
|
|
81
|
+
|
|
82
|
+
function renderLanes( onChangeLayout: jest.Mock ) {
|
|
83
|
+
const layout: DashboardLanesLayoutItem[] = [ { key: 'a', width: 1 } ];
|
|
84
|
+
return render(
|
|
85
|
+
<DashboardLanes
|
|
86
|
+
layout={ layout }
|
|
87
|
+
columns={ LANES }
|
|
88
|
+
itemLimits={ { a: { minWidth: 60 } } }
|
|
89
|
+
editMode
|
|
90
|
+
onChangeLayout={ onChangeLayout }
|
|
91
|
+
>
|
|
92
|
+
<div key="a">A</div>
|
|
93
|
+
</DashboardLanes>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/* eslint-disable testing-library/no-container, testing-library/no-node-access */
|
|
98
|
+
function getResizeHandle( container: HTMLElement, key: string ) {
|
|
99
|
+
const handle = container.querySelector(
|
|
100
|
+
`[data-wp-grid-item-key="${ key }"] [aria-roledescription="draggable"]`
|
|
101
|
+
);
|
|
102
|
+
expect( handle ).not.toBeNull();
|
|
103
|
+
return handle!;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
describe( 'DashboardLanes item limits', () => {
|
|
107
|
+
it( 'renders a stored span below the floor lifted to it', () => {
|
|
108
|
+
setGeometry( 240 );
|
|
109
|
+
const { container } = renderLanes( jest.fn() );
|
|
110
|
+
const itemA = container.querySelector( '[data-wp-grid-item-key="a"]' );
|
|
111
|
+
expect( itemA ).toHaveStyle( { gridColumn: 'span 2' } );
|
|
112
|
+
} );
|
|
113
|
+
|
|
114
|
+
it( 'resizes from the rendered span, not the stored one', async () => {
|
|
115
|
+
setGeometry( 240 );
|
|
116
|
+
const onChangeLayout = jest.fn();
|
|
117
|
+
const { container } = renderLanes( onChangeLayout );
|
|
118
|
+
|
|
119
|
+
// One lane to the right of the rendered two-lane span.
|
|
120
|
+
await activateAndStepRight( getResizeHandle( container, 'a' ) );
|
|
121
|
+
|
|
122
|
+
expect( onChangeLayout ).toHaveBeenCalledTimes( 1 );
|
|
123
|
+
const committed: DashboardLanesLayoutItem[] =
|
|
124
|
+
onChangeLayout.mock.calls[ 0 ][ 0 ];
|
|
125
|
+
expect( committed.find( ( item ) => item.key === 'a' )?.width ).toBe(
|
|
126
|
+
3
|
|
127
|
+
);
|
|
128
|
+
} );
|
|
129
|
+
|
|
130
|
+
it( 'commits nothing while the gesture stays inside the rendered span', async () => {
|
|
131
|
+
setGeometry( 360 );
|
|
132
|
+
const onChangeLayout = jest.fn();
|
|
133
|
+
const { container } = renderLanes( onChangeLayout );
|
|
134
|
+
|
|
135
|
+
await activateAndStepRight( getResizeHandle( container, 'a' ) );
|
|
136
|
+
|
|
137
|
+
expect( onChangeLayout ).not.toHaveBeenCalled();
|
|
138
|
+
} );
|
|
139
|
+
} );
|
|
140
|
+
/* eslint-enable testing-library/no-container, testing-library/no-node-access */
|
package/src/dashboard-lanes/test/{keyboard-activation.test.tsx → keyboard-activation.jsdom.test.tsx}
RENAMED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { render } from '@testing-library/react';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
3
|
import { DashboardLanes } from '..';
|
|
3
4
|
|
|
4
5
|
class MockResizeObserver {
|
|
@@ -17,13 +18,13 @@ class MockResizeObserver {
|
|
|
17
18
|
let originalResizeObserver: typeof ResizeObserver;
|
|
18
19
|
|
|
19
20
|
beforeEach( () => {
|
|
20
|
-
originalResizeObserver =
|
|
21
|
-
(
|
|
21
|
+
originalResizeObserver = globalThis.ResizeObserver;
|
|
22
|
+
( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
22
23
|
MockResizeObserver;
|
|
23
24
|
} );
|
|
24
25
|
|
|
25
26
|
afterEach( () => {
|
|
26
|
-
(
|
|
27
|
+
( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
27
28
|
originalResizeObserver;
|
|
28
29
|
} );
|
|
29
30
|
|