@wordpress/grid 0.6.0 → 0.7.1-next.v.202609031004.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.
Files changed (88) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +44 -1
  3. package/build/dashboard-grid/grid-item.cjs +21 -7
  4. package/build/dashboard-grid/grid-item.cjs.map +2 -2
  5. package/build/dashboard-grid/index.cjs +107 -45
  6. package/build/dashboard-grid/index.cjs.map +2 -2
  7. package/build/dashboard-grid/resolve-fill-widths.cjs +68 -50
  8. package/build/dashboard-grid/resolve-fill-widths.cjs.map +2 -2
  9. package/build/dashboard-grid/types.cjs.map +1 -1
  10. package/build/dashboard-lanes/index.cjs +59 -17
  11. package/build/dashboard-lanes/index.cjs.map +2 -2
  12. package/build/dashboard-lanes/lanes-item.cjs +14 -6
  13. package/build/dashboard-lanes/lanes-item.cjs.map +2 -2
  14. package/build/dashboard-lanes/types.cjs.map +1 -1
  15. package/build/index.cjs.map +1 -1
  16. package/build/shared/array-move-with-pinned.cjs +49 -0
  17. package/build/shared/array-move-with-pinned.cjs.map +7 -0
  18. package/build/shared/resize-snap.cjs +91 -5
  19. package/build/shared/resize-snap.cjs.map +2 -2
  20. package/build/shared/types.cjs.map +1 -1
  21. package/build/shared/use-span-bounds.cjs +85 -0
  22. package/build/shared/use-span-bounds.cjs.map +7 -0
  23. package/build-module/dashboard-grid/grid-item.mjs +21 -7
  24. package/build-module/dashboard-grid/grid-item.mjs.map +2 -2
  25. package/build-module/dashboard-grid/index.mjs +108 -47
  26. package/build-module/dashboard-grid/index.mjs.map +2 -2
  27. package/build-module/dashboard-grid/resolve-fill-widths.mjs +68 -50
  28. package/build-module/dashboard-grid/resolve-fill-widths.mjs.map +2 -2
  29. package/build-module/dashboard-lanes/index.mjs +60 -19
  30. package/build-module/dashboard-lanes/index.mjs.map +2 -2
  31. package/build-module/dashboard-lanes/lanes-item.mjs +14 -6
  32. package/build-module/dashboard-lanes/lanes-item.mjs.map +2 -2
  33. package/build-module/index.mjs.map +1 -1
  34. package/build-module/shared/array-move-with-pinned.mjs +24 -0
  35. package/build-module/shared/array-move-with-pinned.mjs.map +7 -0
  36. package/build-module/shared/resize-snap.mjs +87 -4
  37. package/build-module/shared/resize-snap.mjs.map +2 -2
  38. package/build-module/shared/use-span-bounds.mjs +62 -0
  39. package/build-module/shared/use-span-bounds.mjs.map +7 -0
  40. package/build-types/dashboard-grid/grid-item.d.ts +1 -1
  41. package/build-types/dashboard-grid/grid-item.d.ts.map +1 -1
  42. package/build-types/dashboard-grid/index.d.ts.map +1 -1
  43. package/build-types/dashboard-grid/resolve-fill-widths.d.ts +12 -1
  44. package/build-types/dashboard-grid/resolve-fill-widths.d.ts.map +1 -1
  45. package/build-types/dashboard-grid/types.d.ts +43 -1
  46. package/build-types/dashboard-grid/types.d.ts.map +1 -1
  47. package/build-types/dashboard-lanes/index.d.ts.map +1 -1
  48. package/build-types/dashboard-lanes/lanes-item.d.ts +18 -1
  49. package/build-types/dashboard-lanes/lanes-item.d.ts.map +1 -1
  50. package/build-types/dashboard-lanes/types.d.ts +20 -1
  51. package/build-types/dashboard-lanes/types.d.ts.map +1 -1
  52. package/build-types/index.d.ts +1 -1
  53. package/build-types/index.d.ts.map +1 -1
  54. package/build-types/shared/array-move-with-pinned.d.ts +13 -0
  55. package/build-types/shared/array-move-with-pinned.d.ts.map +1 -0
  56. package/build-types/shared/resize-snap.d.ts +65 -2
  57. package/build-types/shared/resize-snap.d.ts.map +1 -1
  58. package/build-types/shared/types.d.ts +31 -0
  59. package/build-types/shared/types.d.ts.map +1 -1
  60. package/build-types/shared/use-span-bounds.d.ts +29 -0
  61. package/build-types/shared/use-span-bounds.d.ts.map +1 -0
  62. package/package.json +9 -9
  63. package/src/dashboard-grid/grid-item.tsx +21 -7
  64. package/src/dashboard-grid/index.tsx +154 -57
  65. package/src/dashboard-grid/resolve-fill-widths.ts +85 -62
  66. package/src/dashboard-grid/stories/index.story.tsx +154 -0
  67. package/src/dashboard-grid/test/item-interactions.jsdom.test.tsx +63 -0
  68. package/src/dashboard-grid/test/item-limits.jsdom.test.tsx +147 -0
  69. package/src/dashboard-grid/test/resolve-fill-widths.test.ts +159 -0
  70. package/src/dashboard-grid/types.ts +50 -0
  71. package/src/dashboard-lanes/index.tsx +86 -24
  72. package/src/dashboard-lanes/lanes-item.tsx +34 -6
  73. package/src/dashboard-lanes/stories/index.story.tsx +119 -0
  74. package/src/dashboard-lanes/test/item-interactions.jsdom.test.tsx +63 -0
  75. package/src/dashboard-lanes/test/item-limits.jsdom.test.tsx +140 -0
  76. package/src/dashboard-lanes/test/lane-placement.test.ts +1 -0
  77. package/src/dashboard-lanes/types.ts +23 -0
  78. package/src/index.ts +2 -0
  79. package/src/shared/array-move-with-pinned.ts +36 -0
  80. package/src/shared/resize-snap.ts +161 -5
  81. package/src/shared/test/array-move-with-pinned.test.ts +40 -0
  82. package/src/shared/test/resize-snap.test.ts +171 -2
  83. package/src/shared/test/use-span-bounds.jsdom.test.ts +104 -0
  84. package/src/shared/types.ts +35 -0
  85. package/src/shared/use-span-bounds.ts +99 -0
  86. /package/src/dashboard-grid/test/{keyboard-activation.test.tsx → keyboard-activation.jsdom.test.tsx} +0 -0
  87. /package/src/dashboard-lanes/test/{keyboard-activation.test.tsx → keyboard-activation.jsdom.test.tsx} +0 -0
  88. /package/src/dashboard-lanes/test/{use-lane-placement.test.tsx → use-lane-placement.jsdom.test.tsx} +0 -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 width (and height when provided).
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
- const width = Math.max( delta.width, -maxShrinkWidth );
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
- const height = Math.max( delta.height, -maxShrinkHeight );
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 { clampResizeDelta, gridSpanToPixelSize } from '../resize-snap';
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,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
+ } );
@@ -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
+ }