@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
package/src/dashboard-lanes/test/{use-lane-placement.test.tsx → use-lane-placement.jsdom.test.tsx}
RENAMED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { render, act, screen } from '@testing-library/react';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
3
|
import { useState } from '@wordpress/element';
|
|
3
4
|
import { GRID_ITEM_DATA_KEY } from '../../shared/grid-item-key';
|
|
4
5
|
import { useLanePlacement } from '../use-lane-placement';
|
|
@@ -54,20 +55,20 @@ let originalSupports: typeof CSS.supports | undefined;
|
|
|
54
55
|
let originalRaf: typeof requestAnimationFrame;
|
|
55
56
|
|
|
56
57
|
function installMockObserver() {
|
|
57
|
-
originalResizeObserver =
|
|
58
|
+
originalResizeObserver = globalThis.ResizeObserver;
|
|
58
59
|
MockResizeObserver.reset();
|
|
59
|
-
(
|
|
60
|
+
( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
60
61
|
MockResizeObserver;
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
function restoreObserver() {
|
|
64
|
-
(
|
|
65
|
+
( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
65
66
|
originalResizeObserver;
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
function setNativeSupport( supported: boolean ) {
|
|
69
70
|
if ( typeof CSS === 'undefined' ) {
|
|
70
|
-
(
|
|
71
|
+
( globalThis as unknown as { CSS: unknown } ).CSS = {
|
|
71
72
|
supports: () => supported,
|
|
72
73
|
};
|
|
73
74
|
return;
|
|
@@ -100,20 +101,20 @@ function restoreSupport() {
|
|
|
100
101
|
function flushRaf() {
|
|
101
102
|
// jsdom polyfills `requestAnimationFrame` via `setTimeout`; an
|
|
102
103
|
// `act` boundary lets React commit any state set inside the rAF.
|
|
103
|
-
|
|
104
|
+
vi.runAllTimers();
|
|
104
105
|
}
|
|
105
106
|
|
|
106
107
|
beforeEach( () => {
|
|
107
108
|
installMockObserver();
|
|
108
|
-
originalRaf =
|
|
109
|
-
|
|
109
|
+
originalRaf = globalThis.requestAnimationFrame;
|
|
110
|
+
globalThis.requestAnimationFrame = ( cb ) =>
|
|
110
111
|
setTimeout( () => cb( performance.now() ), 0 ) as unknown as number;
|
|
111
|
-
|
|
112
|
+
vi.useFakeTimers();
|
|
112
113
|
} );
|
|
113
114
|
|
|
114
115
|
afterEach( () => {
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
vi.useRealTimers();
|
|
117
|
+
globalThis.requestAnimationFrame = originalRaf;
|
|
117
118
|
restoreObserver();
|
|
118
119
|
restoreSupport();
|
|
119
120
|
} );
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
DragPreviewRenderProps,
|
|
3
|
+
GridItemWidthLimits,
|
|
3
4
|
GridOverlayRenderProps,
|
|
4
5
|
ResizeHandleRenderProps,
|
|
5
6
|
} from '../shared/types';
|
|
@@ -35,6 +36,22 @@ export type DashboardLanesLayoutItem = {
|
|
|
35
36
|
*/
|
|
36
37
|
lane?: number;
|
|
37
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Whether the item can be dragged while the surface is in edit mode.
|
|
41
|
+
* When `false`, the item is pinned: it also holds its index while the
|
|
42
|
+
* other items reorder around it.
|
|
43
|
+
*
|
|
44
|
+
* @default true
|
|
45
|
+
*/
|
|
46
|
+
draggable?: boolean;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Whether the item can be resized while the surface is in edit mode.
|
|
50
|
+
*
|
|
51
|
+
* @default true
|
|
52
|
+
*/
|
|
53
|
+
resizable?: boolean;
|
|
54
|
+
|
|
38
55
|
/**
|
|
39
56
|
* Display order. Lower values render first. When omitted, the
|
|
40
57
|
* item falls back to its index in the `layout` array.
|
|
@@ -170,4 +187,10 @@ export interface DashboardLanesProps
|
|
|
170
187
|
* down to 1.
|
|
171
188
|
*/
|
|
172
189
|
minColumnWidth?: number;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Per-item width limits in pixels, keyed by layout item key. Lane
|
|
193
|
+
* heights are content-driven, so there is no height axis.
|
|
194
|
+
*/
|
|
195
|
+
itemLimits?: Record< string, GridItemWidthLimits >;
|
|
173
196
|
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { arrayMove } from '@dnd-kit/sortable';
|
|
2
|
+
|
|
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
|
+
*/
|
|
14
|
+
export function arrayMoveWithPinned< T >(
|
|
15
|
+
items: T[],
|
|
16
|
+
from: number,
|
|
17
|
+
to: number,
|
|
18
|
+
isPinned: ( item: T ) => boolean
|
|
19
|
+
): T[] {
|
|
20
|
+
const pinned = new Set( items.filter( isPinned ) );
|
|
21
|
+
if ( pinned.size === 0 ) {
|
|
22
|
+
return arrayMove( items, from, to );
|
|
23
|
+
}
|
|
24
|
+
const free = arrayMove( items, from, to ).filter(
|
|
25
|
+
( item ) => ! pinned.has( item )
|
|
26
|
+
);
|
|
27
|
+
let nextFree = 0;
|
|
28
|
+
return items.map( ( item ) => {
|
|
29
|
+
if ( pinned.has( item ) ) {
|
|
30
|
+
return item;
|
|
31
|
+
}
|
|
32
|
+
const next = free[ nextFree ];
|
|
33
|
+
nextFree += 1;
|
|
34
|
+
return next;
|
|
35
|
+
} );
|
|
36
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ResizeDelta } from './types';
|
|
1
|
+
import type { GridItemLimits, ResizeDelta } from './types';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Pixel dimensions for the snapped resize preview outline.
|
|
@@ -11,7 +11,7 @@ export type ResizeSnapSize = {
|
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Clamps a resize delta so the tile cannot shrink below the given
|
|
14
|
-
* minimum
|
|
14
|
+
* minimum size, nor grow past the given maximum when one applies.
|
|
15
15
|
*
|
|
16
16
|
* @param delta Cursor offset from the gesture start in pixels.
|
|
17
17
|
* @param initialSize Size captured at gesture start.
|
|
@@ -20,19 +20,29 @@ export type ResizeSnapSize = {
|
|
|
20
20
|
* @param minSize Minimum tile size in pixels.
|
|
21
21
|
* @param minSize.width Minimum width in pixels.
|
|
22
22
|
* @param minSize.height Minimum height in pixels, when vertical resize applies.
|
|
23
|
+
* @param maxSize Maximum tile size in pixels, when one applies.
|
|
24
|
+
* @param maxSize.width Maximum width in pixels.
|
|
25
|
+
* @param maxSize.height Maximum height in pixels, when vertical resize applies.
|
|
23
26
|
*/
|
|
24
27
|
export function clampResizeDelta(
|
|
25
28
|
delta: ResizeDelta,
|
|
26
29
|
initialSize: { width: number; height: number },
|
|
27
|
-
minSize: { width: number; height?: number }
|
|
30
|
+
minSize: { width: number; height?: number },
|
|
31
|
+
maxSize?: { width?: number; height?: number }
|
|
28
32
|
): ResizeDelta {
|
|
29
33
|
const maxShrinkWidth = initialSize.width - minSize.width;
|
|
30
|
-
|
|
34
|
+
let width = Math.max( delta.width, -maxShrinkWidth );
|
|
35
|
+
if ( maxSize?.width !== undefined ) {
|
|
36
|
+
width = Math.min( width, maxSize.width - initialSize.width );
|
|
37
|
+
}
|
|
31
38
|
if ( minSize.height === undefined ) {
|
|
32
39
|
return { ...delta, width };
|
|
33
40
|
}
|
|
34
41
|
const maxShrinkHeight = initialSize.height - minSize.height;
|
|
35
|
-
|
|
42
|
+
let height = Math.max( delta.height, -maxShrinkHeight );
|
|
43
|
+
if ( maxSize?.height !== undefined ) {
|
|
44
|
+
height = Math.min( height, maxSize.height - initialSize.height );
|
|
45
|
+
}
|
|
36
46
|
return { width, height };
|
|
37
47
|
}
|
|
38
48
|
|
|
@@ -61,3 +71,149 @@ export function gridSpanToPixelSize(
|
|
|
61
71
|
: rowSpan * rowHeightPx + ( rowSpan - 1 ) * gapPx;
|
|
62
72
|
return { widthPx, heightPx };
|
|
63
73
|
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Span bounds derived from per-item pixel limits, in whole tracks.
|
|
77
|
+
* Maximums are concrete: an undeclared width maximum resolves to the
|
|
78
|
+
* column count, an undeclared or inapplicable height maximum to
|
|
79
|
+
* `Infinity`.
|
|
80
|
+
*/
|
|
81
|
+
export type SpanBounds = {
|
|
82
|
+
minWidth: number;
|
|
83
|
+
minHeight: number;
|
|
84
|
+
maxWidth: number;
|
|
85
|
+
maxHeight: number;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Converts per-item pixel limits to span bounds with the track math of
|
|
90
|
+
* `gridSpanToPixelSize`. Minimums round up, maximums round down, every
|
|
91
|
+
* bound stays at one track or more, and a minimum wins over a smaller
|
|
92
|
+
* maximum. Width bounds saturate at the column count; height bounds
|
|
93
|
+
* resolve to `1`/`Infinity` when rows are content-sized.
|
|
94
|
+
*
|
|
95
|
+
* @param limits Size limits in pixels.
|
|
96
|
+
* @param columnWidth Width of one column track in pixels.
|
|
97
|
+
* @param gapPx Gap between tracks in pixels.
|
|
98
|
+
* @param rowHeightPx Row track height in pixels, or `null` when rows
|
|
99
|
+
* are content-sized.
|
|
100
|
+
* @param maxColumns Number of column tracks in the surface.
|
|
101
|
+
*/
|
|
102
|
+
export function pixelLimitsToSpanBounds(
|
|
103
|
+
limits: GridItemLimits,
|
|
104
|
+
columnWidth: number,
|
|
105
|
+
gapPx: number,
|
|
106
|
+
rowHeightPx: number | null,
|
|
107
|
+
maxColumns: number
|
|
108
|
+
): SpanBounds {
|
|
109
|
+
let minWidth = 1;
|
|
110
|
+
let maxWidth = maxColumns;
|
|
111
|
+
if ( columnWidth > 0 ) {
|
|
112
|
+
const columnTrack = columnWidth + gapPx;
|
|
113
|
+
if ( limits.minWidth ) {
|
|
114
|
+
minWidth = Math.min(
|
|
115
|
+
Math.max(
|
|
116
|
+
1,
|
|
117
|
+
Math.ceil( ( limits.minWidth + gapPx ) / columnTrack )
|
|
118
|
+
),
|
|
119
|
+
maxColumns
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
if ( limits.maxWidth ) {
|
|
123
|
+
maxWidth = Math.min(
|
|
124
|
+
Math.max(
|
|
125
|
+
1,
|
|
126
|
+
Math.floor( ( limits.maxWidth + gapPx ) / columnTrack )
|
|
127
|
+
),
|
|
128
|
+
maxColumns
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
let minHeight = 1;
|
|
134
|
+
let maxHeight = Infinity;
|
|
135
|
+
if ( rowHeightPx !== null && rowHeightPx > 0 ) {
|
|
136
|
+
const rowTrack = rowHeightPx + gapPx;
|
|
137
|
+
if ( limits.minHeight ) {
|
|
138
|
+
minHeight = Math.max(
|
|
139
|
+
1,
|
|
140
|
+
Math.ceil( ( limits.minHeight + gapPx ) / rowTrack )
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
if ( limits.maxHeight ) {
|
|
144
|
+
maxHeight = Math.max(
|
|
145
|
+
1,
|
|
146
|
+
Math.floor( ( limits.maxHeight + gapPx ) / rowTrack )
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
minWidth,
|
|
153
|
+
minHeight,
|
|
154
|
+
maxWidth: Math.max( maxWidth, minWidth ),
|
|
155
|
+
maxHeight: Math.max( maxHeight, minHeight ),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Clamps a span to the inclusive `[ min, max ]` range.
|
|
161
|
+
*
|
|
162
|
+
* @param span Span in tracks.
|
|
163
|
+
* @param min Lowest allowed span.
|
|
164
|
+
* @param max Highest allowed span.
|
|
165
|
+
*/
|
|
166
|
+
export function clampSpan( span: number, min: number, max: number ): number {
|
|
167
|
+
return Math.min( Math.max( span, min ), max );
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Pixel limits for the resize gesture, derived from span bounds.
|
|
172
|
+
* Heights are `null` when rows are content-sized or the height
|
|
173
|
+
* bound is open.
|
|
174
|
+
*/
|
|
175
|
+
export type ResizePixelLimits = {
|
|
176
|
+
minWidthPx: number;
|
|
177
|
+
minHeightPx: number | null;
|
|
178
|
+
maxWidthPx: number;
|
|
179
|
+
maxHeightPx: number | null;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Converts span bounds back to pixel limits for the resize gesture,
|
|
184
|
+
* using the same track math as `gridSpanToPixelSize`.
|
|
185
|
+
*
|
|
186
|
+
* @param bounds Span bounds in whole tracks.
|
|
187
|
+
* @param columnWidth Width of one column track in pixels.
|
|
188
|
+
* @param gapPx Gap between tracks in pixels.
|
|
189
|
+
* @param rowHeightPx Row track height in pixels, or `null` when rows
|
|
190
|
+
* are content-sized.
|
|
191
|
+
*/
|
|
192
|
+
export function spanBoundsToPixelLimits(
|
|
193
|
+
bounds: SpanBounds,
|
|
194
|
+
columnWidth: number,
|
|
195
|
+
gapPx: number,
|
|
196
|
+
rowHeightPx: number | null
|
|
197
|
+
): ResizePixelLimits {
|
|
198
|
+
const min = gridSpanToPixelSize(
|
|
199
|
+
bounds.minWidth,
|
|
200
|
+
bounds.minHeight,
|
|
201
|
+
columnWidth,
|
|
202
|
+
gapPx,
|
|
203
|
+
rowHeightPx
|
|
204
|
+
);
|
|
205
|
+
const hasMaxHeight = Number.isFinite( bounds.maxHeight );
|
|
206
|
+
const max = gridSpanToPixelSize(
|
|
207
|
+
bounds.maxWidth,
|
|
208
|
+
hasMaxHeight ? bounds.maxHeight : 1,
|
|
209
|
+
columnWidth,
|
|
210
|
+
gapPx,
|
|
211
|
+
hasMaxHeight ? rowHeightPx : null
|
|
212
|
+
);
|
|
213
|
+
return {
|
|
214
|
+
minWidthPx: min.widthPx,
|
|
215
|
+
minHeightPx: min.heightPx,
|
|
216
|
+
maxWidthPx: max.widthPx,
|
|
217
|
+
maxHeightPx: max.heightPx,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { arrayMoveWithPinned } from '../array-move-with-pinned';
|
|
3
|
+
|
|
4
|
+
const pinned = new Set( [ 'p' ] );
|
|
5
|
+
const isPinned = ( key: string ) => pinned.has( key );
|
|
6
|
+
|
|
7
|
+
describe( 'arrayMoveWithPinned', () => {
|
|
8
|
+
it( 'moves like arrayMove when nothing is pinned', () => {
|
|
9
|
+
expect(
|
|
10
|
+
arrayMoveWithPinned( [ 'a', 'b', 'c' ], 2, 0, () => false )
|
|
11
|
+
).toEqual( [ 'c', 'a', 'b' ] );
|
|
12
|
+
} );
|
|
13
|
+
|
|
14
|
+
it( 'keeps a pinned item at its index when another crosses it', () => {
|
|
15
|
+
expect(
|
|
16
|
+
arrayMoveWithPinned( [ 'a', 'p', 'b' ], 2, 0, isPinned )
|
|
17
|
+
).toEqual( [ 'b', 'p', 'a' ] );
|
|
18
|
+
} );
|
|
19
|
+
|
|
20
|
+
it( 'keeps a pinned first item first', () => {
|
|
21
|
+
expect(
|
|
22
|
+
arrayMoveWithPinned( [ 'p', 'a', 'b' ], 2, 0, isPinned )
|
|
23
|
+
).toEqual( [ 'p', 'b', 'a' ] );
|
|
24
|
+
} );
|
|
25
|
+
|
|
26
|
+
it( 'does not move a pinned item', () => {
|
|
27
|
+
expect(
|
|
28
|
+
arrayMoveWithPinned( [ 'a', 'p', 'b' ], 1, 2, isPinned )
|
|
29
|
+
).toEqual( [ 'a', 'p', 'b' ] );
|
|
30
|
+
} );
|
|
31
|
+
|
|
32
|
+
it( 'holds several pinned items at once', () => {
|
|
33
|
+
const many = new Set( [ 'p', 'q' ] );
|
|
34
|
+
expect(
|
|
35
|
+
arrayMoveWithPinned( [ 'p', 'a', 'q', 'b', 'c' ], 4, 1, ( key ) =>
|
|
36
|
+
many.has( key )
|
|
37
|
+
)
|
|
38
|
+
).toEqual( [ 'p', 'c', 'q', 'a', 'b' ] );
|
|
39
|
+
} );
|
|
40
|
+
} );
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
clampResizeDelta,
|
|
4
|
+
clampSpan,
|
|
5
|
+
gridSpanToPixelSize,
|
|
6
|
+
pixelLimitsToSpanBounds,
|
|
7
|
+
} from '../resize-snap';
|
|
2
8
|
|
|
3
9
|
describe( 'gridSpanToPixelSize', () => {
|
|
4
10
|
it( 'returns one column track width for a single-column span', () => {
|
|
@@ -8,6 +14,139 @@ describe( 'gridSpanToPixelSize', () => {
|
|
|
8
14
|
} );
|
|
9
15
|
} );
|
|
10
16
|
|
|
17
|
+
describe( 'pixelLimitsToSpanBounds', () => {
|
|
18
|
+
it( 'rounds minimums up to whole tracks', () => {
|
|
19
|
+
expect(
|
|
20
|
+
pixelLimitsToSpanBounds( { minWidth: 480 }, 292, 24, 300, 4 )
|
|
21
|
+
).toEqual( {
|
|
22
|
+
minWidth: 2,
|
|
23
|
+
minHeight: 1,
|
|
24
|
+
maxWidth: 4,
|
|
25
|
+
maxHeight: Infinity,
|
|
26
|
+
} );
|
|
27
|
+
} );
|
|
28
|
+
|
|
29
|
+
it( 'covers both minimum axes when a row height is known', () => {
|
|
30
|
+
expect(
|
|
31
|
+
pixelLimitsToSpanBounds(
|
|
32
|
+
{ minWidth: 480, minHeight: 360 },
|
|
33
|
+
292,
|
|
34
|
+
24,
|
|
35
|
+
300,
|
|
36
|
+
4
|
|
37
|
+
)
|
|
38
|
+
).toEqual( {
|
|
39
|
+
minWidth: 2,
|
|
40
|
+
minHeight: 2,
|
|
41
|
+
maxWidth: 4,
|
|
42
|
+
maxHeight: Infinity,
|
|
43
|
+
} );
|
|
44
|
+
} );
|
|
45
|
+
|
|
46
|
+
it( 'rounds maximums down to whole tracks', () => {
|
|
47
|
+
expect(
|
|
48
|
+
pixelLimitsToSpanBounds( { maxWidth: 700 }, 292, 24, null, 4 )
|
|
49
|
+
).toEqual( {
|
|
50
|
+
minWidth: 1,
|
|
51
|
+
minHeight: 1,
|
|
52
|
+
maxWidth: 2,
|
|
53
|
+
maxHeight: Infinity,
|
|
54
|
+
} );
|
|
55
|
+
} );
|
|
56
|
+
|
|
57
|
+
it( 'keeps a maximum at one track or more', () => {
|
|
58
|
+
expect(
|
|
59
|
+
pixelLimitsToSpanBounds( { maxWidth: 100 }, 292, 24, null, 4 )
|
|
60
|
+
.maxWidth
|
|
61
|
+
).toBe( 1 );
|
|
62
|
+
} );
|
|
63
|
+
|
|
64
|
+
it( 'lets the minimum win over a tighter maximum', () => {
|
|
65
|
+
expect(
|
|
66
|
+
pixelLimitsToSpanBounds(
|
|
67
|
+
{ minWidth: 480, maxWidth: 300 },
|
|
68
|
+
292,
|
|
69
|
+
24,
|
|
70
|
+
null,
|
|
71
|
+
4
|
|
72
|
+
)
|
|
73
|
+
).toEqual( {
|
|
74
|
+
minWidth: 2,
|
|
75
|
+
minHeight: 1,
|
|
76
|
+
maxWidth: 2,
|
|
77
|
+
maxHeight: Infinity,
|
|
78
|
+
} );
|
|
79
|
+
} );
|
|
80
|
+
|
|
81
|
+
it( 'saturates width bounds at the column count', () => {
|
|
82
|
+
expect(
|
|
83
|
+
pixelLimitsToSpanBounds(
|
|
84
|
+
{ minWidth: 4000, maxWidth: 9000 },
|
|
85
|
+
292,
|
|
86
|
+
24,
|
|
87
|
+
null,
|
|
88
|
+
4
|
|
89
|
+
)
|
|
90
|
+
).toEqual( {
|
|
91
|
+
minWidth: 4,
|
|
92
|
+
minHeight: 1,
|
|
93
|
+
maxWidth: 4,
|
|
94
|
+
maxHeight: Infinity,
|
|
95
|
+
} );
|
|
96
|
+
} );
|
|
97
|
+
|
|
98
|
+
it( 'bounds heights against the row track', () => {
|
|
99
|
+
expect(
|
|
100
|
+
pixelLimitsToSpanBounds(
|
|
101
|
+
{ minHeight: 360, maxHeight: 800 },
|
|
102
|
+
292,
|
|
103
|
+
24,
|
|
104
|
+
300,
|
|
105
|
+
4
|
|
106
|
+
)
|
|
107
|
+
).toEqual( {
|
|
108
|
+
minWidth: 1,
|
|
109
|
+
minHeight: 2,
|
|
110
|
+
maxWidth: 4,
|
|
111
|
+
maxHeight: 2,
|
|
112
|
+
} );
|
|
113
|
+
} );
|
|
114
|
+
|
|
115
|
+
it( 'leaves height bounds open when rows are content-sized', () => {
|
|
116
|
+
expect(
|
|
117
|
+
pixelLimitsToSpanBounds(
|
|
118
|
+
{ minHeight: 360, maxHeight: 600 },
|
|
119
|
+
292,
|
|
120
|
+
24,
|
|
121
|
+
null,
|
|
122
|
+
4
|
|
123
|
+
)
|
|
124
|
+
).toEqual( {
|
|
125
|
+
minWidth: 1,
|
|
126
|
+
minHeight: 1,
|
|
127
|
+
maxWidth: 4,
|
|
128
|
+
maxHeight: Infinity,
|
|
129
|
+
} );
|
|
130
|
+
} );
|
|
131
|
+
|
|
132
|
+
it( 'falls back to the full range while the surface is unmeasured', () => {
|
|
133
|
+
expect(
|
|
134
|
+
pixelLimitsToSpanBounds(
|
|
135
|
+
{ minWidth: 480, maxWidth: 600 },
|
|
136
|
+
0,
|
|
137
|
+
24,
|
|
138
|
+
null,
|
|
139
|
+
4
|
|
140
|
+
)
|
|
141
|
+
).toEqual( {
|
|
142
|
+
minWidth: 1,
|
|
143
|
+
minHeight: 1,
|
|
144
|
+
maxWidth: 4,
|
|
145
|
+
maxHeight: Infinity,
|
|
146
|
+
} );
|
|
147
|
+
} );
|
|
148
|
+
} );
|
|
149
|
+
|
|
11
150
|
describe( 'clampResizeDelta', () => {
|
|
12
151
|
it( 'does not shrink width below one column', () => {
|
|
13
152
|
const initial = { width: 216, height: 120 };
|
|
@@ -25,11 +164,41 @@ describe( 'clampResizeDelta', () => {
|
|
|
25
164
|
).toEqual( { width: 0, height: -96 } );
|
|
26
165
|
} );
|
|
27
166
|
|
|
28
|
-
it( 'leaves growth deltas unchanged', () => {
|
|
167
|
+
it( 'leaves growth deltas unchanged without a maximum', () => {
|
|
29
168
|
const initial = { width: 100, height: 48 };
|
|
30
169
|
const min = { width: 100, height: 48 };
|
|
31
170
|
expect(
|
|
32
171
|
clampResizeDelta( { width: 80, height: 40 }, initial, min )
|
|
33
172
|
).toEqual( { width: 80, height: 40 } );
|
|
34
173
|
} );
|
|
174
|
+
|
|
175
|
+
it( 'does not grow width past the maximum', () => {
|
|
176
|
+
const initial = { width: 216, height: 120 };
|
|
177
|
+
const min = { width: 100 };
|
|
178
|
+
const max = { width: 300 };
|
|
179
|
+
expect(
|
|
180
|
+
clampResizeDelta( { width: 200, height: 0 }, initial, min, max )
|
|
181
|
+
).toEqual( { width: 84, height: 0 } );
|
|
182
|
+
} );
|
|
183
|
+
|
|
184
|
+
it( 'does not grow height past the maximum when one applies', () => {
|
|
185
|
+
const initial = { width: 200, height: 144 };
|
|
186
|
+
const min = { width: 100, height: 48 };
|
|
187
|
+
const max = { width: 400, height: 200 };
|
|
188
|
+
expect(
|
|
189
|
+
clampResizeDelta( { width: 0, height: 120 }, initial, min, max )
|
|
190
|
+
).toEqual( { width: 0, height: 56 } );
|
|
191
|
+
} );
|
|
192
|
+
} );
|
|
193
|
+
|
|
194
|
+
describe( 'clampSpan', () => {
|
|
195
|
+
it( 'keeps a span inside the inclusive range', () => {
|
|
196
|
+
expect( clampSpan( 0, 1, 4 ) ).toBe( 1 );
|
|
197
|
+
expect( clampSpan( 3, 1, 4 ) ).toBe( 3 );
|
|
198
|
+
expect( clampSpan( 9, 1, 4 ) ).toBe( 4 );
|
|
199
|
+
} );
|
|
200
|
+
|
|
201
|
+
it( 'treats an infinite maximum as open', () => {
|
|
202
|
+
expect( clampSpan( 40, 1, Infinity ) ).toBe( 40 );
|
|
203
|
+
} );
|
|
35
204
|
} );
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { renderHook } from '@testing-library/react';
|
|
3
|
+
import { useResizePixelLimits, useSpanBounds } from '../use-span-bounds';
|
|
4
|
+
import type { GridItemLimits } from '../types';
|
|
5
|
+
|
|
6
|
+
type Props = {
|
|
7
|
+
itemLimits?: Record< string, GridItemLimits >;
|
|
8
|
+
columnWidth: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
function renderSpanBounds( initialProps: Props ) {
|
|
12
|
+
return renderHook(
|
|
13
|
+
( { itemLimits, columnWidth }: Props ) =>
|
|
14
|
+
useSpanBounds( itemLimits, columnWidth, 16, 80, 6 ),
|
|
15
|
+
{ initialProps }
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe( 'useSpanBounds', () => {
|
|
20
|
+
const itemLimits = { a: { minWidth: 300, maxHeight: 250 } };
|
|
21
|
+
|
|
22
|
+
it( 'quantizes limits to whole tracks', () => {
|
|
23
|
+
const { result } = renderSpanBounds( { itemLimits, columnWidth: 100 } );
|
|
24
|
+
expect( result.current.get( 'a' ) ).toEqual( {
|
|
25
|
+
minWidth: 3,
|
|
26
|
+
minHeight: 1,
|
|
27
|
+
maxWidth: 6,
|
|
28
|
+
maxHeight: 2,
|
|
29
|
+
} );
|
|
30
|
+
} );
|
|
31
|
+
|
|
32
|
+
it( 'keeps the map identity across geometry ticks that leave the bounds unchanged', () => {
|
|
33
|
+
const { result, rerender } = renderSpanBounds( {
|
|
34
|
+
itemLimits,
|
|
35
|
+
columnWidth: 100,
|
|
36
|
+
} );
|
|
37
|
+
const first = result.current;
|
|
38
|
+
rerender( { itemLimits, columnWidth: 101 } );
|
|
39
|
+
expect( result.current ).toBe( first );
|
|
40
|
+
} );
|
|
41
|
+
|
|
42
|
+
it( 'keeps the map identity when an equal itemLimits object is passed', () => {
|
|
43
|
+
const { result, rerender } = renderSpanBounds( {
|
|
44
|
+
itemLimits,
|
|
45
|
+
columnWidth: 100,
|
|
46
|
+
} );
|
|
47
|
+
const first = result.current;
|
|
48
|
+
rerender( { itemLimits: { ...itemLimits }, columnWidth: 100 } );
|
|
49
|
+
expect( result.current ).toBe( first );
|
|
50
|
+
} );
|
|
51
|
+
|
|
52
|
+
it( 'returns a new map once a bound crosses a track threshold', () => {
|
|
53
|
+
const { result, rerender } = renderSpanBounds( {
|
|
54
|
+
itemLimits,
|
|
55
|
+
columnWidth: 100,
|
|
56
|
+
} );
|
|
57
|
+
const first = result.current;
|
|
58
|
+
rerender( { itemLimits, columnWidth: 150 } );
|
|
59
|
+
expect( result.current ).not.toBe( first );
|
|
60
|
+
expect( result.current.get( 'a' )?.minWidth ).toBe( 2 );
|
|
61
|
+
} );
|
|
62
|
+
|
|
63
|
+
it( 'returns a stable empty map without limits', () => {
|
|
64
|
+
const { result, rerender } = renderSpanBounds( { columnWidth: 100 } );
|
|
65
|
+
const first = result.current;
|
|
66
|
+
expect( first.size ).toBe( 0 );
|
|
67
|
+
rerender( { columnWidth: 120 } );
|
|
68
|
+
expect( result.current ).toBe( first );
|
|
69
|
+
} );
|
|
70
|
+
} );
|
|
71
|
+
|
|
72
|
+
describe( 'useResizePixelLimits', () => {
|
|
73
|
+
it( 'converts bounds to pixel limits for the resize gesture', () => {
|
|
74
|
+
const bounds = new Map( [
|
|
75
|
+
[ 'a', { minWidth: 2, minHeight: 1, maxWidth: 4, maxHeight: 3 } ],
|
|
76
|
+
] );
|
|
77
|
+
const { result } = renderHook( () =>
|
|
78
|
+
useResizePixelLimits( bounds, 100, 16, 80 )
|
|
79
|
+
);
|
|
80
|
+
expect( result.current.get( 'a' ) ).toEqual( {
|
|
81
|
+
minWidthPx: 216,
|
|
82
|
+
minHeightPx: 80,
|
|
83
|
+
maxWidthPx: 448,
|
|
84
|
+
maxHeightPx: 272,
|
|
85
|
+
} );
|
|
86
|
+
} );
|
|
87
|
+
|
|
88
|
+
it( 'leaves the height open when the height bound is open', () => {
|
|
89
|
+
const bounds = new Map( [
|
|
90
|
+
[
|
|
91
|
+
'a',
|
|
92
|
+
{
|
|
93
|
+
minWidth: 1,
|
|
94
|
+
minHeight: 1,
|
|
95
|
+
maxWidth: 2,
|
|
96
|
+
maxHeight: Infinity,
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
] );
|
|
100
|
+
const { result } = renderHook( () =>
|
|
101
|
+
useResizePixelLimits( bounds, 100, 16, 80 )
|
|
102
|
+
);
|
|
103
|
+
expect( result.current.get( 'a' )?.maxHeightPx ).toBeNull();
|
|
104
|
+
} );
|
|
105
|
+
} );
|