@wordpress/grid 0.6.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 +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 +7 -8
- 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.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 +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.test.tsx +63 -0
- package/src/dashboard-lanes/test/item-limits.test.tsx +140 -0
- 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
|
@@ -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,39 @@
|
|
|
1
|
+
import { arrayMoveWithPinned } from '../array-move-with-pinned';
|
|
2
|
+
|
|
3
|
+
const pinned = new Set( [ 'p' ] );
|
|
4
|
+
const isPinned = ( key: string ) => pinned.has( key );
|
|
5
|
+
|
|
6
|
+
describe( 'arrayMoveWithPinned', () => {
|
|
7
|
+
it( 'moves like arrayMove when nothing is pinned', () => {
|
|
8
|
+
expect(
|
|
9
|
+
arrayMoveWithPinned( [ 'a', 'b', 'c' ], 2, 0, () => false )
|
|
10
|
+
).toEqual( [ 'c', 'a', 'b' ] );
|
|
11
|
+
} );
|
|
12
|
+
|
|
13
|
+
it( 'keeps a pinned item at its index when another crosses it', () => {
|
|
14
|
+
expect(
|
|
15
|
+
arrayMoveWithPinned( [ 'a', 'p', 'b' ], 2, 0, isPinned )
|
|
16
|
+
).toEqual( [ 'b', 'p', 'a' ] );
|
|
17
|
+
} );
|
|
18
|
+
|
|
19
|
+
it( 'keeps a pinned first item first', () => {
|
|
20
|
+
expect(
|
|
21
|
+
arrayMoveWithPinned( [ 'p', 'a', 'b' ], 2, 0, isPinned )
|
|
22
|
+
).toEqual( [ 'p', 'b', 'a' ] );
|
|
23
|
+
} );
|
|
24
|
+
|
|
25
|
+
it( 'does not move a pinned item', () => {
|
|
26
|
+
expect(
|
|
27
|
+
arrayMoveWithPinned( [ 'a', 'p', 'b' ], 1, 2, isPinned )
|
|
28
|
+
).toEqual( [ 'a', 'p', 'b' ] );
|
|
29
|
+
} );
|
|
30
|
+
|
|
31
|
+
it( 'holds several pinned items at once', () => {
|
|
32
|
+
const many = new Set( [ 'p', 'q' ] );
|
|
33
|
+
expect(
|
|
34
|
+
arrayMoveWithPinned( [ 'p', 'a', 'q', 'b', 'c' ], 4, 1, ( key ) =>
|
|
35
|
+
many.has( key )
|
|
36
|
+
)
|
|
37
|
+
).toEqual( [ 'p', 'c', 'q', 'a', 'b' ] );
|
|
38
|
+
} );
|
|
39
|
+
} );
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
clampResizeDelta,
|
|
3
|
+
clampSpan,
|
|
4
|
+
gridSpanToPixelSize,
|
|
5
|
+
pixelLimitsToSpanBounds,
|
|
6
|
+
} from '../resize-snap';
|
|
2
7
|
|
|
3
8
|
describe( 'gridSpanToPixelSize', () => {
|
|
4
9
|
it( 'returns one column track width for a single-column span', () => {
|
|
@@ -8,6 +13,139 @@ describe( 'gridSpanToPixelSize', () => {
|
|
|
8
13
|
} );
|
|
9
14
|
} );
|
|
10
15
|
|
|
16
|
+
describe( 'pixelLimitsToSpanBounds', () => {
|
|
17
|
+
it( 'rounds minimums up to whole tracks', () => {
|
|
18
|
+
expect(
|
|
19
|
+
pixelLimitsToSpanBounds( { minWidth: 480 }, 292, 24, 300, 4 )
|
|
20
|
+
).toEqual( {
|
|
21
|
+
minWidth: 2,
|
|
22
|
+
minHeight: 1,
|
|
23
|
+
maxWidth: 4,
|
|
24
|
+
maxHeight: Infinity,
|
|
25
|
+
} );
|
|
26
|
+
} );
|
|
27
|
+
|
|
28
|
+
it( 'covers both minimum axes when a row height is known', () => {
|
|
29
|
+
expect(
|
|
30
|
+
pixelLimitsToSpanBounds(
|
|
31
|
+
{ minWidth: 480, minHeight: 360 },
|
|
32
|
+
292,
|
|
33
|
+
24,
|
|
34
|
+
300,
|
|
35
|
+
4
|
|
36
|
+
)
|
|
37
|
+
).toEqual( {
|
|
38
|
+
minWidth: 2,
|
|
39
|
+
minHeight: 2,
|
|
40
|
+
maxWidth: 4,
|
|
41
|
+
maxHeight: Infinity,
|
|
42
|
+
} );
|
|
43
|
+
} );
|
|
44
|
+
|
|
45
|
+
it( 'rounds maximums down to whole tracks', () => {
|
|
46
|
+
expect(
|
|
47
|
+
pixelLimitsToSpanBounds( { maxWidth: 700 }, 292, 24, null, 4 )
|
|
48
|
+
).toEqual( {
|
|
49
|
+
minWidth: 1,
|
|
50
|
+
minHeight: 1,
|
|
51
|
+
maxWidth: 2,
|
|
52
|
+
maxHeight: Infinity,
|
|
53
|
+
} );
|
|
54
|
+
} );
|
|
55
|
+
|
|
56
|
+
it( 'keeps a maximum at one track or more', () => {
|
|
57
|
+
expect(
|
|
58
|
+
pixelLimitsToSpanBounds( { maxWidth: 100 }, 292, 24, null, 4 )
|
|
59
|
+
.maxWidth
|
|
60
|
+
).toBe( 1 );
|
|
61
|
+
} );
|
|
62
|
+
|
|
63
|
+
it( 'lets the minimum win over a tighter maximum', () => {
|
|
64
|
+
expect(
|
|
65
|
+
pixelLimitsToSpanBounds(
|
|
66
|
+
{ minWidth: 480, maxWidth: 300 },
|
|
67
|
+
292,
|
|
68
|
+
24,
|
|
69
|
+
null,
|
|
70
|
+
4
|
|
71
|
+
)
|
|
72
|
+
).toEqual( {
|
|
73
|
+
minWidth: 2,
|
|
74
|
+
minHeight: 1,
|
|
75
|
+
maxWidth: 2,
|
|
76
|
+
maxHeight: Infinity,
|
|
77
|
+
} );
|
|
78
|
+
} );
|
|
79
|
+
|
|
80
|
+
it( 'saturates width bounds at the column count', () => {
|
|
81
|
+
expect(
|
|
82
|
+
pixelLimitsToSpanBounds(
|
|
83
|
+
{ minWidth: 4000, maxWidth: 9000 },
|
|
84
|
+
292,
|
|
85
|
+
24,
|
|
86
|
+
null,
|
|
87
|
+
4
|
|
88
|
+
)
|
|
89
|
+
).toEqual( {
|
|
90
|
+
minWidth: 4,
|
|
91
|
+
minHeight: 1,
|
|
92
|
+
maxWidth: 4,
|
|
93
|
+
maxHeight: Infinity,
|
|
94
|
+
} );
|
|
95
|
+
} );
|
|
96
|
+
|
|
97
|
+
it( 'bounds heights against the row track', () => {
|
|
98
|
+
expect(
|
|
99
|
+
pixelLimitsToSpanBounds(
|
|
100
|
+
{ minHeight: 360, maxHeight: 800 },
|
|
101
|
+
292,
|
|
102
|
+
24,
|
|
103
|
+
300,
|
|
104
|
+
4
|
|
105
|
+
)
|
|
106
|
+
).toEqual( {
|
|
107
|
+
minWidth: 1,
|
|
108
|
+
minHeight: 2,
|
|
109
|
+
maxWidth: 4,
|
|
110
|
+
maxHeight: 2,
|
|
111
|
+
} );
|
|
112
|
+
} );
|
|
113
|
+
|
|
114
|
+
it( 'leaves height bounds open when rows are content-sized', () => {
|
|
115
|
+
expect(
|
|
116
|
+
pixelLimitsToSpanBounds(
|
|
117
|
+
{ minHeight: 360, maxHeight: 600 },
|
|
118
|
+
292,
|
|
119
|
+
24,
|
|
120
|
+
null,
|
|
121
|
+
4
|
|
122
|
+
)
|
|
123
|
+
).toEqual( {
|
|
124
|
+
minWidth: 1,
|
|
125
|
+
minHeight: 1,
|
|
126
|
+
maxWidth: 4,
|
|
127
|
+
maxHeight: Infinity,
|
|
128
|
+
} );
|
|
129
|
+
} );
|
|
130
|
+
|
|
131
|
+
it( 'falls back to the full range while the surface is unmeasured', () => {
|
|
132
|
+
expect(
|
|
133
|
+
pixelLimitsToSpanBounds(
|
|
134
|
+
{ minWidth: 480, maxWidth: 600 },
|
|
135
|
+
0,
|
|
136
|
+
24,
|
|
137
|
+
null,
|
|
138
|
+
4
|
|
139
|
+
)
|
|
140
|
+
).toEqual( {
|
|
141
|
+
minWidth: 1,
|
|
142
|
+
minHeight: 1,
|
|
143
|
+
maxWidth: 4,
|
|
144
|
+
maxHeight: Infinity,
|
|
145
|
+
} );
|
|
146
|
+
} );
|
|
147
|
+
} );
|
|
148
|
+
|
|
11
149
|
describe( 'clampResizeDelta', () => {
|
|
12
150
|
it( 'does not shrink width below one column', () => {
|
|
13
151
|
const initial = { width: 216, height: 120 };
|
|
@@ -25,11 +163,41 @@ describe( 'clampResizeDelta', () => {
|
|
|
25
163
|
).toEqual( { width: 0, height: -96 } );
|
|
26
164
|
} );
|
|
27
165
|
|
|
28
|
-
it( 'leaves growth deltas unchanged', () => {
|
|
166
|
+
it( 'leaves growth deltas unchanged without a maximum', () => {
|
|
29
167
|
const initial = { width: 100, height: 48 };
|
|
30
168
|
const min = { width: 100, height: 48 };
|
|
31
169
|
expect(
|
|
32
170
|
clampResizeDelta( { width: 80, height: 40 }, initial, min )
|
|
33
171
|
).toEqual( { width: 80, height: 40 } );
|
|
34
172
|
} );
|
|
173
|
+
|
|
174
|
+
it( 'does not grow width past the maximum', () => {
|
|
175
|
+
const initial = { width: 216, height: 120 };
|
|
176
|
+
const min = { width: 100 };
|
|
177
|
+
const max = { width: 300 };
|
|
178
|
+
expect(
|
|
179
|
+
clampResizeDelta( { width: 200, height: 0 }, initial, min, max )
|
|
180
|
+
).toEqual( { width: 84, height: 0 } );
|
|
181
|
+
} );
|
|
182
|
+
|
|
183
|
+
it( 'does not grow height past the maximum when one applies', () => {
|
|
184
|
+
const initial = { width: 200, height: 144 };
|
|
185
|
+
const min = { width: 100, height: 48 };
|
|
186
|
+
const max = { width: 400, height: 200 };
|
|
187
|
+
expect(
|
|
188
|
+
clampResizeDelta( { width: 0, height: 120 }, initial, min, max )
|
|
189
|
+
).toEqual( { width: 0, height: 56 } );
|
|
190
|
+
} );
|
|
191
|
+
} );
|
|
192
|
+
|
|
193
|
+
describe( 'clampSpan', () => {
|
|
194
|
+
it( 'keeps a span inside the inclusive range', () => {
|
|
195
|
+
expect( clampSpan( 0, 1, 4 ) ).toBe( 1 );
|
|
196
|
+
expect( clampSpan( 3, 1, 4 ) ).toBe( 3 );
|
|
197
|
+
expect( clampSpan( 9, 1, 4 ) ).toBe( 4 );
|
|
198
|
+
} );
|
|
199
|
+
|
|
200
|
+
it( 'treats an infinite maximum as open', () => {
|
|
201
|
+
expect( clampSpan( 40, 1, Infinity ) ).toBe( 40 );
|
|
202
|
+
} );
|
|
35
203
|
} );
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react';
|
|
2
|
+
import { useResizePixelLimits, useSpanBounds } from '../use-span-bounds';
|
|
3
|
+
import type { GridItemLimits } from '../types';
|
|
4
|
+
|
|
5
|
+
type Props = {
|
|
6
|
+
itemLimits?: Record< string, GridItemLimits >;
|
|
7
|
+
columnWidth: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
function renderSpanBounds( initialProps: Props ) {
|
|
11
|
+
return renderHook(
|
|
12
|
+
( { itemLimits, columnWidth }: Props ) =>
|
|
13
|
+
useSpanBounds( itemLimits, columnWidth, 16, 80, 6 ),
|
|
14
|
+
{ initialProps }
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe( 'useSpanBounds', () => {
|
|
19
|
+
const itemLimits = { a: { minWidth: 300, maxHeight: 250 } };
|
|
20
|
+
|
|
21
|
+
it( 'quantizes limits to whole tracks', () => {
|
|
22
|
+
const { result } = renderSpanBounds( { itemLimits, columnWidth: 100 } );
|
|
23
|
+
expect( result.current.get( 'a' ) ).toEqual( {
|
|
24
|
+
minWidth: 3,
|
|
25
|
+
minHeight: 1,
|
|
26
|
+
maxWidth: 6,
|
|
27
|
+
maxHeight: 2,
|
|
28
|
+
} );
|
|
29
|
+
} );
|
|
30
|
+
|
|
31
|
+
it( 'keeps the map identity across geometry ticks that leave the bounds unchanged', () => {
|
|
32
|
+
const { result, rerender } = renderSpanBounds( {
|
|
33
|
+
itemLimits,
|
|
34
|
+
columnWidth: 100,
|
|
35
|
+
} );
|
|
36
|
+
const first = result.current;
|
|
37
|
+
rerender( { itemLimits, columnWidth: 101 } );
|
|
38
|
+
expect( result.current ).toBe( first );
|
|
39
|
+
} );
|
|
40
|
+
|
|
41
|
+
it( 'keeps the map identity when an equal itemLimits object is passed', () => {
|
|
42
|
+
const { result, rerender } = renderSpanBounds( {
|
|
43
|
+
itemLimits,
|
|
44
|
+
columnWidth: 100,
|
|
45
|
+
} );
|
|
46
|
+
const first = result.current;
|
|
47
|
+
rerender( { itemLimits: { ...itemLimits }, columnWidth: 100 } );
|
|
48
|
+
expect( result.current ).toBe( first );
|
|
49
|
+
} );
|
|
50
|
+
|
|
51
|
+
it( 'returns a new map once a bound crosses a track threshold', () => {
|
|
52
|
+
const { result, rerender } = renderSpanBounds( {
|
|
53
|
+
itemLimits,
|
|
54
|
+
columnWidth: 100,
|
|
55
|
+
} );
|
|
56
|
+
const first = result.current;
|
|
57
|
+
rerender( { itemLimits, columnWidth: 150 } );
|
|
58
|
+
expect( result.current ).not.toBe( first );
|
|
59
|
+
expect( result.current.get( 'a' )?.minWidth ).toBe( 2 );
|
|
60
|
+
} );
|
|
61
|
+
|
|
62
|
+
it( 'returns a stable empty map without limits', () => {
|
|
63
|
+
const { result, rerender } = renderSpanBounds( { columnWidth: 100 } );
|
|
64
|
+
const first = result.current;
|
|
65
|
+
expect( first.size ).toBe( 0 );
|
|
66
|
+
rerender( { columnWidth: 120 } );
|
|
67
|
+
expect( result.current ).toBe( first );
|
|
68
|
+
} );
|
|
69
|
+
} );
|
|
70
|
+
|
|
71
|
+
describe( 'useResizePixelLimits', () => {
|
|
72
|
+
it( 'converts bounds to pixel limits for the resize gesture', () => {
|
|
73
|
+
const bounds = new Map( [
|
|
74
|
+
[ 'a', { minWidth: 2, minHeight: 1, maxWidth: 4, maxHeight: 3 } ],
|
|
75
|
+
] );
|
|
76
|
+
const { result } = renderHook( () =>
|
|
77
|
+
useResizePixelLimits( bounds, 100, 16, 80 )
|
|
78
|
+
);
|
|
79
|
+
expect( result.current.get( 'a' ) ).toEqual( {
|
|
80
|
+
minWidthPx: 216,
|
|
81
|
+
minHeightPx: 80,
|
|
82
|
+
maxWidthPx: 448,
|
|
83
|
+
maxHeightPx: 272,
|
|
84
|
+
} );
|
|
85
|
+
} );
|
|
86
|
+
|
|
87
|
+
it( 'leaves the height open when the height bound is open', () => {
|
|
88
|
+
const bounds = new Map( [
|
|
89
|
+
[
|
|
90
|
+
'a',
|
|
91
|
+
{
|
|
92
|
+
minWidth: 1,
|
|
93
|
+
minHeight: 1,
|
|
94
|
+
maxWidth: 2,
|
|
95
|
+
maxHeight: Infinity,
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
] );
|
|
99
|
+
const { result } = renderHook( () =>
|
|
100
|
+
useResizePixelLimits( bounds, 100, 16, 80 )
|
|
101
|
+
);
|
|
102
|
+
expect( result.current.get( 'a' )?.maxHeightPx ).toBeNull();
|
|
103
|
+
} );
|
|
104
|
+
} );
|
package/src/shared/types.ts
CHANGED
|
@@ -159,3 +159,38 @@ export interface ResizeHandleProps {
|
|
|
159
159
|
*/
|
|
160
160
|
renderResizeHandle?: React.ComponentType< ResizeHandleRenderProps >;
|
|
161
161
|
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Per-item width limits, in pixels. Surfaces quantize each limit to
|
|
165
|
+
* whole tracks (minimums round up, maximums round down), bound rendered
|
|
166
|
+
* spans and resize gestures at the result, and never write it into the
|
|
167
|
+
* layout. A quantized minimum wins over a smaller quantized maximum.
|
|
168
|
+
*/
|
|
169
|
+
export type GridItemWidthLimits = {
|
|
170
|
+
/**
|
|
171
|
+
* Minimum tile width in pixels.
|
|
172
|
+
*/
|
|
173
|
+
minWidth?: number;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Maximum tile width in pixels.
|
|
177
|
+
*/
|
|
178
|
+
maxWidth?: number;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Per-item size limits on both axes, in pixels, for surfaces with row
|
|
183
|
+
* tracks. Height limits quantize against the row track when `rowHeight`
|
|
184
|
+
* is numeric and stay open otherwise.
|
|
185
|
+
*/
|
|
186
|
+
export type GridItemLimits = GridItemWidthLimits & {
|
|
187
|
+
/**
|
|
188
|
+
* Minimum tile height in pixels.
|
|
189
|
+
*/
|
|
190
|
+
minHeight?: number;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Maximum tile height in pixels.
|
|
194
|
+
*/
|
|
195
|
+
maxHeight?: number;
|
|
196
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { useMemo } from '@wordpress/element';
|
|
2
|
+
import {
|
|
3
|
+
pixelLimitsToSpanBounds,
|
|
4
|
+
spanBoundsToPixelLimits,
|
|
5
|
+
} from './resize-snap';
|
|
6
|
+
import type { ResizePixelLimits, SpanBounds } from './resize-snap';
|
|
7
|
+
import type { GridItemLimits } from './types';
|
|
8
|
+
|
|
9
|
+
const NO_BOUNDS: ReadonlyMap< string, SpanBounds > = new Map();
|
|
10
|
+
const NO_LIMITS: ReadonlyMap< string, ResizePixelLimits > = new Map();
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Quantizes per-item pixel limits to span bounds for the current
|
|
14
|
+
* geometry. The map keeps its identity while every bound is unchanged,
|
|
15
|
+
* so container width ticks and fresh `itemLimits` objects do not
|
|
16
|
+
* invalidate the layout memos or the sortable context downstream.
|
|
17
|
+
*
|
|
18
|
+
* @param itemLimits Per-item pixel limits, keyed by layout item key.
|
|
19
|
+
* @param columnWidth Width of one column track in pixels.
|
|
20
|
+
* @param gapPx Gap between tracks in pixels.
|
|
21
|
+
* @param rowHeightPx Row track height in pixels, or `null` when rows
|
|
22
|
+
* are content-sized.
|
|
23
|
+
* @param maxColumns Number of column tracks in the surface.
|
|
24
|
+
*/
|
|
25
|
+
export function useSpanBounds(
|
|
26
|
+
itemLimits: Record< string, GridItemLimits > | undefined,
|
|
27
|
+
columnWidth: number,
|
|
28
|
+
gapPx: number,
|
|
29
|
+
rowHeightPx: number | null,
|
|
30
|
+
maxColumns: number
|
|
31
|
+
): ReadonlyMap< string, SpanBounds > {
|
|
32
|
+
const computed = useMemo( () => {
|
|
33
|
+
if ( ! itemLimits ) {
|
|
34
|
+
return NO_BOUNDS;
|
|
35
|
+
}
|
|
36
|
+
const map = new Map< string, SpanBounds >();
|
|
37
|
+
for ( const [ key, limits ] of Object.entries( itemLimits ) ) {
|
|
38
|
+
map.set(
|
|
39
|
+
key,
|
|
40
|
+
pixelLimitsToSpanBounds(
|
|
41
|
+
limits,
|
|
42
|
+
columnWidth,
|
|
43
|
+
gapPx,
|
|
44
|
+
rowHeightPx,
|
|
45
|
+
maxColumns
|
|
46
|
+
)
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
return map;
|
|
50
|
+
}, [ itemLimits, columnWidth, gapPx, rowHeightPx, maxColumns ] );
|
|
51
|
+
|
|
52
|
+
const signature = useMemo( () => {
|
|
53
|
+
let value = '';
|
|
54
|
+
for ( const [ key, bounds ] of computed ) {
|
|
55
|
+
value += `${ key }:${ bounds.minWidth }:${ bounds.minHeight }:${ bounds.maxWidth }:${ bounds.maxHeight }|`;
|
|
56
|
+
}
|
|
57
|
+
return value;
|
|
58
|
+
}, [ computed ] );
|
|
59
|
+
|
|
60
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `signature` encodes every bound; `computed` changes identity on geometry ticks that leave the bounds unchanged.
|
|
61
|
+
return useMemo( () => computed, [ signature ] );
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Pixel limits for the resize gesture of every bounded item. Tracks
|
|
66
|
+
* the continuous geometry, but only recomputes when it changes, not on
|
|
67
|
+
* every gesture frame.
|
|
68
|
+
*
|
|
69
|
+
* @param spanBounds Span bounds per item key.
|
|
70
|
+
* @param columnWidth Width of one column track in pixels.
|
|
71
|
+
* @param gapPx Gap between tracks in pixels.
|
|
72
|
+
* @param rowHeightPx Row track height in pixels, or `null` when rows
|
|
73
|
+
* are content-sized.
|
|
74
|
+
*/
|
|
75
|
+
export function useResizePixelLimits(
|
|
76
|
+
spanBounds: ReadonlyMap< string, SpanBounds >,
|
|
77
|
+
columnWidth: number,
|
|
78
|
+
gapPx: number,
|
|
79
|
+
rowHeightPx: number | null
|
|
80
|
+
): ReadonlyMap< string, ResizePixelLimits > {
|
|
81
|
+
return useMemo( () => {
|
|
82
|
+
if ( spanBounds.size === 0 ) {
|
|
83
|
+
return NO_LIMITS;
|
|
84
|
+
}
|
|
85
|
+
const map = new Map< string, ResizePixelLimits >();
|
|
86
|
+
for ( const [ key, bounds ] of spanBounds ) {
|
|
87
|
+
map.set(
|
|
88
|
+
key,
|
|
89
|
+
spanBoundsToPixelLimits(
|
|
90
|
+
bounds,
|
|
91
|
+
columnWidth,
|
|
92
|
+
gapPx,
|
|
93
|
+
rowHeightPx
|
|
94
|
+
)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
return map;
|
|
98
|
+
}, [ spanBounds, columnWidth, gapPx, rowHeightPx ] );
|
|
99
|
+
}
|