@wordpress/grid 0.10.0 → 0.11.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/README.md +35 -14
- package/build/dashboard-grid/grid-item.cjs +232 -295
- package/build/dashboard-grid/grid-item.cjs.map +1 -7
- package/build/dashboard-grid/index.cjs +525 -632
- package/build/dashboard-grid/index.cjs.map +1 -7
- package/build/dashboard-grid/resolve-fill-widths.cjs +159 -203
- package/build/dashboard-grid/resolve-fill-widths.cjs.map +1 -7
- package/build/dashboard-grid/types.cjs +4 -18
- package/build/dashboard-lanes/index.cjs +494 -576
- package/build/dashboard-lanes/index.cjs.map +1 -7
- package/build/dashboard-lanes/lane-placement.cjs +77 -100
- package/build/dashboard-lanes/lane-placement.cjs.map +1 -7
- package/build/dashboard-lanes/lanes-item.cjs +222 -278
- package/build/dashboard-lanes/lanes-item.cjs.map +1 -7
- package/build/dashboard-lanes/types.cjs +4 -18
- package/build/dashboard-lanes/use-lane-placement.cjs +172 -198
- package/build/dashboard-lanes/use-lane-placement.cjs.map +1 -7
- package/build/index.cjs +16 -31
- package/build/shared/array-move-with-pinned.cjs +29 -45
- package/build/shared/array-move-with-pinned.cjs.map +1 -7
- package/build/shared/column-policy.cjs +58 -0
- package/build/shared/column-policy.cjs.map +1 -0
- package/build/shared/drag-overlay-drop-animation.cjs +40 -66
- package/build/shared/drag-overlay-drop-animation.cjs.map +1 -7
- package/build/shared/grid-item-key.cjs +12 -29
- package/build/shared/grid-item-key.cjs.map +1 -7
- package/build/shared/grid-overlay.cjs +163 -164
- package/build/shared/grid-overlay.cjs.map +1 -7
- package/build/shared/item-exit-overlay.cjs +132 -134
- package/build/shared/item-exit-overlay.cjs.map +1 -7
- package/build/shared/resize-handle.cjs +213 -197
- package/build/shared/resize-handle.cjs.map +1 -7
- package/build/shared/resize-snap.cjs +116 -121
- package/build/shared/resize-snap.cjs.map +1 -7
- package/build/shared/types.cjs +4 -18
- package/build/shared/use-item-exit-animation.cjs +96 -143
- package/build/shared/use-item-exit-animation.cjs.map +1 -7
- package/build/shared/use-layout-shift-animation.cjs +121 -150
- package/build/shared/use-layout-shift-animation.cjs.map +1 -7
- package/build/shared/use-span-bounds.cjs +65 -79
- package/build/shared/use-span-bounds.cjs.map +1 -7
- package/build-module/dashboard-grid/grid-item.mjs +209 -265
- package/build-module/dashboard-grid/grid-item.mjs.map +1 -7
- package/build-module/dashboard-grid/index.mjs +502 -623
- package/build-module/dashboard-grid/index.mjs.map +1 -7
- package/build-module/dashboard-grid/resolve-fill-widths.mjs +155 -179
- package/build-module/dashboard-grid/resolve-fill-widths.mjs.map +1 -7
- package/build-module/dashboard-grid/types.mjs +0 -1
- package/build-module/dashboard-lanes/index.mjs +470 -567
- package/build-module/dashboard-lanes/index.mjs.map +1 -7
- package/build-module/dashboard-lanes/lane-placement.mjs +73 -76
- package/build-module/dashboard-lanes/lane-placement.mjs.map +1 -7
- package/build-module/dashboard-lanes/lanes-item.mjs +199 -248
- package/build-module/dashboard-lanes/lanes-item.mjs.map +1 -7
- package/build-module/dashboard-lanes/types.mjs +0 -1
- package/build-module/dashboard-lanes/use-lane-placement.mjs +167 -173
- package/build-module/dashboard-lanes/use-lane-placement.mjs.map +1 -7
- package/build-module/index.mjs +1 -6
- package/build-module/shared/array-move-with-pinned.mjs +25 -21
- package/build-module/shared/array-move-with-pinned.mjs.map +1 -7
- package/build-module/shared/column-policy.mjs +52 -0
- package/build-module/shared/column-policy.mjs.map +1 -0
- package/build-module/shared/drag-overlay-drop-animation.mjs +35 -44
- package/build-module/shared/drag-overlay-drop-animation.mjs.map +1 -7
- package/build-module/shared/grid-item-key.mjs +8 -5
- package/build-module/shared/grid-item-key.mjs.map +1 -7
- package/build-module/shared/grid-overlay.mjs +143 -136
- package/build-module/shared/grid-overlay.mjs.map +1 -7
- package/build-module/shared/item-exit-overlay.mjs +128 -110
- package/build-module/shared/item-exit-overlay.mjs.map +1 -7
- package/build-module/shared/resize-handle.mjs +191 -171
- package/build-module/shared/resize-handle.mjs.map +1 -7
- package/build-module/shared/resize-snap.mjs +108 -93
- package/build-module/shared/resize-snap.mjs.map +1 -7
- package/build-module/shared/types.mjs +0 -1
- package/build-module/shared/use-item-exit-animation.mjs +92 -124
- package/build-module/shared/use-item-exit-animation.mjs.map +1 -7
- package/build-module/shared/use-layout-shift-animation.mjs +114 -123
- package/build-module/shared/use-layout-shift-animation.mjs.map +1 -7
- package/build-module/shared/use-span-bounds.mjs +60 -57
- package/build-module/shared/use-span-bounds.mjs.map +1 -7
- package/build-types/dashboard-grid/index.d.ts.map +1 -1
- package/build-types/dashboard-grid/types.d.ts +9 -5
- 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/types.d.ts +9 -5
- package/build-types/dashboard-lanes/types.d.ts.map +1 -1
- package/build-types/index.d.ts +1 -0
- package/build-types/index.d.ts.map +1 -1
- package/build-types/shared/column-policy.d.ts +43 -0
- package/build-types/shared/column-policy.d.ts.map +1 -0
- package/package.json +11 -11
- package/src/dashboard-grid/grid-item.tsx +1 -1
- package/src/dashboard-grid/index.tsx +11 -12
- package/src/dashboard-grid/stories/index.story.tsx +129 -50
- package/src/dashboard-grid/test/item-interactions.browser.test.tsx +1 -1
- package/src/dashboard-grid/types.ts +9 -5
- package/src/dashboard-lanes/index.tsx +11 -11
- package/src/dashboard-lanes/lanes-item.tsx +1 -1
- package/src/dashboard-lanes/stories/index.story.tsx +124 -28
- package/src/dashboard-lanes/test/item-interactions.browser.test.tsx +1 -1
- package/src/dashboard-lanes/types.ts +9 -5
- package/src/index.ts +1 -0
- package/src/shared/column-policy.ts +100 -0
- package/src/shared/test/column-policy.test.ts +215 -0
- package/src/shared/test/sortable-semantics.browser.test.tsx +122 -0
- package/build/dashboard-grid/types.cjs.map +0 -7
- package/build/dashboard-lanes/types.cjs.map +0 -7
- package/build/index.cjs.map +0 -7
- package/build/shared/types.cjs.map +0 -7
- package/build-module/dashboard-grid/types.mjs.map +0 -7
- package/build-module/dashboard-lanes/types.mjs.map +0 -7
- package/build-module/index.mjs.map +0 -7
- package/build-module/shared/types.mjs.map +0 -7
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
2
|
-
import { useState, useMemo } from '@wordpress/element';
|
|
2
|
+
import { useId, useState, useMemo } from '@wordpress/element';
|
|
3
3
|
import { DashboardLanes } from '..';
|
|
4
4
|
import type { DashboardLanesLayoutItem } from '../types';
|
|
5
5
|
import type { GridOverlayRenderProps } from '../../shared/types';
|
|
@@ -19,12 +19,12 @@ const meta: Meta< typeof DashboardLanes > = {
|
|
|
19
19
|
children: { control: false },
|
|
20
20
|
columns: {
|
|
21
21
|
control: { type: 'number', min: 1, max: 12, step: 1 },
|
|
22
|
-
description: '
|
|
22
|
+
description: 'Lane count, or the cap when `minColumnWidth` is set.',
|
|
23
23
|
},
|
|
24
24
|
minColumnWidth: {
|
|
25
25
|
control: { type: 'number', min: 80, max: 600, step: 8 },
|
|
26
26
|
description:
|
|
27
|
-
'
|
|
27
|
+
'Per-lane floor in pixels. Lowers the count on narrow containers, down to one.',
|
|
28
28
|
},
|
|
29
29
|
flowTolerance: {
|
|
30
30
|
control: { type: 'number', min: 0, max: 64, step: 1 },
|
|
@@ -182,18 +182,71 @@ export const Default: Story = {
|
|
|
182
182
|
},
|
|
183
183
|
};
|
|
184
184
|
|
|
185
|
+
function TileControls( { title }: { title: string } ) {
|
|
186
|
+
const metricId = useId();
|
|
187
|
+
return (
|
|
188
|
+
<span
|
|
189
|
+
style={ {
|
|
190
|
+
display: 'flex',
|
|
191
|
+
flexDirection: 'column',
|
|
192
|
+
alignItems: 'flex-start',
|
|
193
|
+
gap: 8,
|
|
194
|
+
} }
|
|
195
|
+
>
|
|
196
|
+
<strong>{ title }</strong>
|
|
197
|
+
<a href="#details">Details</a>
|
|
198
|
+
<button type="button">Refresh</button>
|
|
199
|
+
<label htmlFor={ metricId }>
|
|
200
|
+
Metric{ ' ' }
|
|
201
|
+
<select id={ metricId }>
|
|
202
|
+
<option>Views</option>
|
|
203
|
+
<option>Clicks</option>
|
|
204
|
+
</select>
|
|
205
|
+
</label>
|
|
206
|
+
</span>
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
185
210
|
/**
|
|
186
|
-
*
|
|
187
|
-
* `minColumnWidth` as the per-lane lower bound. Resize the preview
|
|
188
|
-
* frame to see the lane count adapt.
|
|
211
|
+
* Tiles with their own links, buttons and form controls. The a11y addon checks that the lanes do not wrap them in drag semantics. In edit mode, the first tile is pinned (`draggable: false`) and its controls keep their semantics.
|
|
189
212
|
*/
|
|
190
|
-
export const
|
|
213
|
+
export const InteractiveContent: Story = {
|
|
214
|
+
args: {
|
|
215
|
+
columns: 3,
|
|
216
|
+
layout: [
|
|
217
|
+
{ key: 'pinned', draggable: false },
|
|
218
|
+
{ key: 'a' },
|
|
219
|
+
{ key: 'b' },
|
|
220
|
+
],
|
|
221
|
+
children: [
|
|
222
|
+
<Tile key="pinned" tone="neutral" height={ 180 }>
|
|
223
|
+
<TileControls title="Pinned" />
|
|
224
|
+
</Tile>,
|
|
225
|
+
<Tile key="a" tone="info" height={ 180 }>
|
|
226
|
+
<TileControls title="Tile" />
|
|
227
|
+
</Tile>,
|
|
228
|
+
<Tile key="b" tone="success" height={ 180 }>
|
|
229
|
+
<TileControls title="Tile" />
|
|
230
|
+
</Tile>,
|
|
231
|
+
],
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* `columns` and `minColumnWidth` compose. `columns` alone fixes the lane
|
|
237
|
+
* count. `minColumnWidth` alone fits as many lanes of at least that width
|
|
238
|
+
* as the container allows. Together, `columns` caps what the floor fits:
|
|
239
|
+
* up to four lanes here, none narrower than 200px, down to one. Open the
|
|
240
|
+
* story full screen and resize the browser to walk the counts.
|
|
241
|
+
*/
|
|
242
|
+
export const Columns: Story = {
|
|
191
243
|
parameters: {
|
|
192
244
|
// FIXME: Resize handles are nameless aria commands; edit mode nests interactive controls (aria-command-name, nested-interactive).
|
|
193
245
|
// See: https://github.com/WordPress/gutenberg/issues/81596
|
|
194
246
|
a11y: { test: 'todo' },
|
|
195
247
|
},
|
|
196
248
|
args: {
|
|
249
|
+
columns: 4,
|
|
197
250
|
minColumnWidth: 200,
|
|
198
251
|
layout: [
|
|
199
252
|
{ key: 'a' },
|
|
@@ -215,40 +268,77 @@ export const Responsive: Story = {
|
|
|
215
268
|
};
|
|
216
269
|
|
|
217
270
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
* count on narrow ones whenever fitting all of them would push
|
|
222
|
-
* tiles below `minColumnWidth`.
|
|
271
|
+
* `columns` as a map: one lane from 0px, two from 600px, four from
|
|
272
|
+
* 960px. The count steps at those container widths regardless of how
|
|
273
|
+
* wide each lane ends up; add `minColumnWidth` to lower it further.
|
|
223
274
|
*/
|
|
224
|
-
export const
|
|
275
|
+
export const ColumnSteps: Story = {
|
|
225
276
|
parameters: {
|
|
226
277
|
// FIXME: Resize handles are nameless aria commands; edit mode nests interactive controls (aria-command-name, nested-interactive).
|
|
227
278
|
// See: https://github.com/WordPress/gutenberg/issues/81596
|
|
228
279
|
a11y: { test: 'todo' },
|
|
229
280
|
},
|
|
281
|
+
argTypes: {
|
|
282
|
+
columns: { control: 'object' },
|
|
283
|
+
},
|
|
230
284
|
args: {
|
|
231
|
-
columns: 4,
|
|
232
|
-
minColumnWidth: 200,
|
|
285
|
+
columns: { 0: 1, 600: 2, 960: 4 },
|
|
233
286
|
layout: [
|
|
234
|
-
{ key: '
|
|
235
|
-
{ key: '
|
|
236
|
-
{ key: '
|
|
237
|
-
{ key: '
|
|
238
|
-
{ key: '
|
|
239
|
-
{ key: '
|
|
287
|
+
{ key: 'fixed-1', width: 1 },
|
|
288
|
+
{ key: 'fixed-2', width: 2 },
|
|
289
|
+
{ key: 'fill-1', width: 2 },
|
|
290
|
+
{ key: 'fixed-3', width: 1 },
|
|
291
|
+
{ key: 'fixed-4', width: 1 },
|
|
292
|
+
{ key: 'fixed-5', width: 2 },
|
|
293
|
+
{ key: 'fill-2', width: 3 },
|
|
294
|
+
{ key: 'full-1', width: 1 },
|
|
240
295
|
],
|
|
241
296
|
children: [
|
|
242
|
-
<Tile key="
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
<Tile key="
|
|
246
|
-
|
|
247
|
-
|
|
297
|
+
<Tile key="fixed-1" tone="brand" height={ 120 } index={ 1 }>
|
|
298
|
+
width: <strong>1</strong>
|
|
299
|
+
</Tile>,
|
|
300
|
+
<Tile key="fixed-2" tone="info" height={ 200 } index={ 2 }>
|
|
301
|
+
width: <strong>2</strong>
|
|
302
|
+
</Tile>,
|
|
303
|
+
<Tile key="fill-1" tone="success" height={ 80 } index={ 3 }>
|
|
304
|
+
width: <strong>2</strong>
|
|
305
|
+
</Tile>,
|
|
306
|
+
<Tile key="fixed-3" tone="warning" height={ 160 } index={ 4 }>
|
|
307
|
+
width: <strong>1</strong>
|
|
308
|
+
</Tile>,
|
|
309
|
+
<Tile key="fixed-4" tone="error" height={ 100 } index={ 5 }>
|
|
310
|
+
width: <strong>1</strong>
|
|
311
|
+
</Tile>,
|
|
312
|
+
<Tile key="fixed-5" tone="neutral" height={ 240 } index={ 6 }>
|
|
313
|
+
width: <strong>2</strong>
|
|
314
|
+
</Tile>,
|
|
315
|
+
<Tile key="fill-2" tone="neutral" height={ 240 } index={ 7 }>
|
|
316
|
+
width: <strong>3</strong>
|
|
317
|
+
</Tile>,
|
|
318
|
+
<Tile key="full-1" tone="neutral" height={ 240 } index={ 8 }>
|
|
319
|
+
width: <strong>1</strong>
|
|
320
|
+
</Tile>,
|
|
248
321
|
],
|
|
249
322
|
},
|
|
250
323
|
};
|
|
251
324
|
|
|
325
|
+
/**
|
|
326
|
+
* A step can be `'auto'`: from that width up there is no cap and the
|
|
327
|
+
* floor alone decides the count. Here the steps fix one and two lanes
|
|
328
|
+
* below 960px, and 200px lanes fill the container above it.
|
|
329
|
+
*/
|
|
330
|
+
export const ColumnStepsWithFloor: Story = {
|
|
331
|
+
parameters: ColumnSteps.parameters,
|
|
332
|
+
argTypes: {
|
|
333
|
+
columns: { control: 'object' },
|
|
334
|
+
},
|
|
335
|
+
args: {
|
|
336
|
+
...ColumnSteps.args,
|
|
337
|
+
columns: { 0: 1, 600: 2, 960: 'auto' },
|
|
338
|
+
minColumnWidth: 200,
|
|
339
|
+
},
|
|
340
|
+
};
|
|
341
|
+
|
|
252
342
|
/**
|
|
253
343
|
* Items with `width: 2` span two lanes. The skyline picks a span
|
|
254
344
|
* position that minimizes the resulting baseline across spanned
|
|
@@ -318,7 +408,13 @@ export const EditMode: Story = {
|
|
|
318
408
|
height: number;
|
|
319
409
|
label: string;
|
|
320
410
|
} )[] = [
|
|
321
|
-
{
|
|
411
|
+
{
|
|
412
|
+
key: 'a',
|
|
413
|
+
draggable: false,
|
|
414
|
+
tone: 'brand',
|
|
415
|
+
height: 120,
|
|
416
|
+
label: 'pinned',
|
|
417
|
+
},
|
|
322
418
|
{ key: 'b', tone: 'info', height: 200, label: '200px' },
|
|
323
419
|
{
|
|
324
420
|
key: 'wide',
|
|
@@ -36,7 +36,7 @@ describe( 'DashboardLanes item interactions', () => {
|
|
|
36
36
|
const { activator, resizeHandle } = await renderItem( {
|
|
37
37
|
draggable: false,
|
|
38
38
|
} );
|
|
39
|
-
expect( activator ).
|
|
39
|
+
expect( activator ).toBeNull();
|
|
40
40
|
expect( resizeHandle ).not.toBeNull();
|
|
41
41
|
} );
|
|
42
42
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ColumnSteps } from '../shared/column-policy';
|
|
1
2
|
import type {
|
|
2
3
|
DragPreviewRenderProps,
|
|
3
4
|
GridItemWidthLimits,
|
|
@@ -63,7 +64,8 @@ export type DashboardLanesLayoutItem = {
|
|
|
63
64
|
* Props for `DashboardLanes`.
|
|
64
65
|
*
|
|
65
66
|
* `columns` and `minColumnWidth` compose as a layered model:
|
|
66
|
-
* - `columns` alone:
|
|
67
|
+
* - `columns` alone: the count at every width, or a count per container
|
|
68
|
+
* width when given as `ColumnSteps`; tiles scale with the container.
|
|
67
69
|
* - `minColumnWidth` alone: lane count derives from container width,
|
|
68
70
|
* floored by the per-tile minimum, down to 1.
|
|
69
71
|
* - Both together: `columns` caps the count, `minColumnWidth` enforces
|
|
@@ -174,11 +176,13 @@ export interface DashboardLanesProps extends Omit<
|
|
|
174
176
|
renderGridOverlay?: React.ComponentType< GridOverlayRenderProps >;
|
|
175
177
|
|
|
176
178
|
/**
|
|
177
|
-
*
|
|
178
|
-
* `
|
|
179
|
-
*
|
|
179
|
+
* Lane count, used as a cap. A number applies at every width; a
|
|
180
|
+
* `ColumnSteps` map applies a count from each container width up, and
|
|
181
|
+
* an `'auto'` step lifts the cap from its width up.
|
|
182
|
+
* Defaults to six when neither `columns` nor `minColumnWidth` is set;
|
|
183
|
+
* with `minColumnWidth` set it can resolve lower on narrow containers.
|
|
180
184
|
*/
|
|
181
|
-
columns?: number;
|
|
185
|
+
columns?: number | ColumnSteps;
|
|
182
186
|
|
|
183
187
|
/**
|
|
184
188
|
* Per-tile minimum width in pixels. Enables responsive mode: the
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Column count per container width. Each key is the inline size in pixels
|
|
3
|
+
* from which its count applies: `{ 0: 1, 600: 2, 960: 4 }` is one column
|
|
4
|
+
* from 0px, two from 600px, four from 960px, and below the smallest key
|
|
5
|
+
* that step's count applies. An `'auto'` step lifts the cap from its
|
|
6
|
+
* width up and leaves the count to `minColumnWidth`; with no floor set,
|
|
7
|
+
* the step is skipped and the count below it holds.
|
|
8
|
+
*/
|
|
9
|
+
export type ColumnSteps = Record< number, number | 'auto' >;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Column policy shared by the surfaces: a count at every width, or a count
|
|
13
|
+
* per container width, with an optional per-column floor in pixels.
|
|
14
|
+
*/
|
|
15
|
+
export interface ColumnPolicy {
|
|
16
|
+
columns?: number | ColumnSteps;
|
|
17
|
+
minColumnWidth?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function normalizeCount( count: unknown ): number | undefined {
|
|
21
|
+
return typeof count === 'number' && Number.isFinite( count )
|
|
22
|
+
? Math.max( 1, Math.floor( count ) )
|
|
23
|
+
: undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function resolveFloor( { minColumnWidth }: ColumnPolicy ): number | undefined {
|
|
27
|
+
return typeof minColumnWidth === 'number' &&
|
|
28
|
+
Number.isFinite( minColumnWidth ) &&
|
|
29
|
+
minColumnWidth > 0
|
|
30
|
+
? minColumnWidth
|
|
31
|
+
: undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The count the policy asks for at a container width, before the floor.
|
|
36
|
+
* Counts are rounded down and never below one; a count that is not a
|
|
37
|
+
* finite number, or a step that is neither a count nor `'auto'`, is
|
|
38
|
+
* ignored.
|
|
39
|
+
*
|
|
40
|
+
* @param policy Column policy.
|
|
41
|
+
* @param containerWidth Measured inline size of the surface in pixels.
|
|
42
|
+
* @return The count, or `undefined` when none applies: no count was set, or
|
|
43
|
+
* an `'auto'` step lifts the cap over a floor.
|
|
44
|
+
*/
|
|
45
|
+
export function resolveColumnCap(
|
|
46
|
+
policy: ColumnPolicy,
|
|
47
|
+
containerWidth: number
|
|
48
|
+
): number | undefined {
|
|
49
|
+
const { columns } = policy;
|
|
50
|
+
if ( typeof columns !== 'object' || columns === null ) {
|
|
51
|
+
return normalizeCount( columns );
|
|
52
|
+
}
|
|
53
|
+
const hasFloor = resolveFloor( policy ) !== undefined;
|
|
54
|
+
const widths = Object.keys( columns )
|
|
55
|
+
.map( Number )
|
|
56
|
+
.filter( Number.isFinite )
|
|
57
|
+
.filter( ( width ) => {
|
|
58
|
+
const step = columns[ width ];
|
|
59
|
+
return step === 'auto'
|
|
60
|
+
? hasFloor
|
|
61
|
+
: normalizeCount( step ) !== undefined;
|
|
62
|
+
} )
|
|
63
|
+
.sort( ( a, b ) => b - a );
|
|
64
|
+
if ( widths.length === 0 ) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
const reached = widths.find( ( width ) => containerWidth >= width );
|
|
68
|
+
const step = columns[ reached ?? widths[ widths.length - 1 ] ];
|
|
69
|
+
return step === 'auto' ? undefined : normalizeCount( step );
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Resolves the count a surface renders: the policy's count at the container
|
|
74
|
+
* width, lowered by the floor so no column is narrower than
|
|
75
|
+
* `minColumnWidth`. A floor with no count fits as many columns as the
|
|
76
|
+
* container allows.
|
|
77
|
+
*
|
|
78
|
+
* @param policy Column policy.
|
|
79
|
+
* @param containerWidth Measured inline size of the surface in pixels.
|
|
80
|
+
* @param gapPx Column gap in pixels.
|
|
81
|
+
* @param defaultColumns Count used when the policy sets neither field.
|
|
82
|
+
* @return Column count, at least one.
|
|
83
|
+
*/
|
|
84
|
+
export function resolveColumnCount(
|
|
85
|
+
policy: ColumnPolicy,
|
|
86
|
+
containerWidth: number,
|
|
87
|
+
gapPx: number,
|
|
88
|
+
defaultColumns: number
|
|
89
|
+
): number {
|
|
90
|
+
const cap = resolveColumnCap( policy, containerWidth );
|
|
91
|
+
const floor = resolveFloor( policy );
|
|
92
|
+
if ( floor === undefined ) {
|
|
93
|
+
return Math.max( 1, cap ?? defaultColumns );
|
|
94
|
+
}
|
|
95
|
+
const maxFit = Math.max(
|
|
96
|
+
1,
|
|
97
|
+
Math.floor( ( containerWidth + gapPx ) / ( floor + gapPx ) )
|
|
98
|
+
);
|
|
99
|
+
return cap === undefined ? maxFit : Math.max( 1, Math.min( cap, maxFit ) );
|
|
100
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { resolveColumnCap, resolveColumnCount } from '../column-policy';
|
|
3
|
+
import type { ColumnSteps } from '../column-policy';
|
|
4
|
+
|
|
5
|
+
const STEPS = { 0: 1, 600: 2, 960: 4 };
|
|
6
|
+
const AUTO_STEPS: ColumnSteps = { 0: 1, 600: 2, 960: 'auto' };
|
|
7
|
+
const GAP = 24;
|
|
8
|
+
|
|
9
|
+
describe( 'resolveColumnCap', () => {
|
|
10
|
+
it( 'passes a count and an absent policy through', () => {
|
|
11
|
+
expect( resolveColumnCap( { columns: 3 }, 1000 ) ).toBe( 3 );
|
|
12
|
+
expect( resolveColumnCap( {}, 1000 ) ).toBeUndefined();
|
|
13
|
+
} );
|
|
14
|
+
|
|
15
|
+
it( 'picks the widest step the container has reached', () => {
|
|
16
|
+
expect( resolveColumnCap( { columns: STEPS }, 0 ) ).toBe( 1 );
|
|
17
|
+
expect( resolveColumnCap( { columns: STEPS }, 599 ) ).toBe( 1 );
|
|
18
|
+
expect( resolveColumnCap( { columns: STEPS }, 600 ) ).toBe( 2 );
|
|
19
|
+
expect( resolveColumnCap( { columns: STEPS }, 959 ) ).toBe( 2 );
|
|
20
|
+
expect( resolveColumnCap( { columns: STEPS }, 960 ) ).toBe( 4 );
|
|
21
|
+
expect( resolveColumnCap( { columns: STEPS }, 5000 ) ).toBe( 4 );
|
|
22
|
+
} );
|
|
23
|
+
|
|
24
|
+
it( 'uses the narrowest step below its own threshold', () => {
|
|
25
|
+
expect( resolveColumnCap( { columns: { 600: 2, 960: 4 } }, 100 ) ).toBe(
|
|
26
|
+
2
|
|
27
|
+
);
|
|
28
|
+
} );
|
|
29
|
+
|
|
30
|
+
it( 'lifts the cap on an auto step over a floor', () => {
|
|
31
|
+
const policy = { columns: AUTO_STEPS, minColumnWidth: 240 };
|
|
32
|
+
expect( resolveColumnCap( policy, 800 ) ).toBe( 2 );
|
|
33
|
+
expect( resolveColumnCap( policy, 960 ) ).toBeUndefined();
|
|
34
|
+
} );
|
|
35
|
+
|
|
36
|
+
it( 'skips an auto step with no floor', () => {
|
|
37
|
+
expect( resolveColumnCap( { columns: AUTO_STEPS }, 960 ) ).toBe( 2 );
|
|
38
|
+
expect(
|
|
39
|
+
resolveColumnCap( { columns: { 0: 8, 960: 'auto' } }, 1000 )
|
|
40
|
+
).toBe( 8 );
|
|
41
|
+
expect(
|
|
42
|
+
resolveColumnCap( { columns: { 0: 'auto' } }, 1000 )
|
|
43
|
+
).toBeUndefined();
|
|
44
|
+
} );
|
|
45
|
+
|
|
46
|
+
it( 'treats a map with no numeric keys as unset', () => {
|
|
47
|
+
expect( resolveColumnCap( { columns: {} }, 1000 ) ).toBeUndefined();
|
|
48
|
+
expect(
|
|
49
|
+
resolveColumnCap(
|
|
50
|
+
{ columns: { wide: 4 } as unknown as ColumnSteps },
|
|
51
|
+
1000
|
|
52
|
+
)
|
|
53
|
+
).toBeUndefined();
|
|
54
|
+
} );
|
|
55
|
+
|
|
56
|
+
it( 'rounds a count down and never below one', () => {
|
|
57
|
+
expect( resolveColumnCap( { columns: 2.5 }, 1000 ) ).toBe( 2 );
|
|
58
|
+
expect( resolveColumnCap( { columns: 0.2 }, 1000 ) ).toBe( 1 );
|
|
59
|
+
expect( resolveColumnCap( { columns: { 0: 2.5 } }, 1000 ) ).toBe( 2 );
|
|
60
|
+
} );
|
|
61
|
+
|
|
62
|
+
it( 'ignores a count that is not a finite number', () => {
|
|
63
|
+
expect( resolveColumnCap( { columns: NaN }, 1000 ) ).toBeUndefined();
|
|
64
|
+
expect(
|
|
65
|
+
resolveColumnCap( { columns: Infinity }, 1000 )
|
|
66
|
+
).toBeUndefined();
|
|
67
|
+
expect(
|
|
68
|
+
resolveColumnCap( { columns: null as unknown as number }, 1000 )
|
|
69
|
+
).toBeUndefined();
|
|
70
|
+
} );
|
|
71
|
+
|
|
72
|
+
it( 'skips a step that is neither a count nor auto', () => {
|
|
73
|
+
const steps = { 0: 2, 600: '3', 960: NaN } as unknown as ColumnSteps;
|
|
74
|
+
expect( resolveColumnCap( { columns: steps }, 1000 ) ).toBe( 2 );
|
|
75
|
+
expect(
|
|
76
|
+
resolveColumnCap(
|
|
77
|
+
{ columns: { 0: '3' } as unknown as ColumnSteps },
|
|
78
|
+
1000
|
|
79
|
+
)
|
|
80
|
+
).toBeUndefined();
|
|
81
|
+
} );
|
|
82
|
+
} );
|
|
83
|
+
|
|
84
|
+
describe( 'resolveColumnCount', () => {
|
|
85
|
+
it( 'falls back to the default when the policy sets nothing', () => {
|
|
86
|
+
expect( resolveColumnCount( {}, 1000, GAP, 6 ) ).toBe( 6 );
|
|
87
|
+
} );
|
|
88
|
+
|
|
89
|
+
it( 'renders a plain count at every width', () => {
|
|
90
|
+
expect( resolveColumnCount( { columns: 3 }, 300, GAP, 6 ) ).toBe( 3 );
|
|
91
|
+
expect( resolveColumnCount( { columns: 3 }, 3000, GAP, 6 ) ).toBe( 3 );
|
|
92
|
+
} );
|
|
93
|
+
|
|
94
|
+
it( 'fits as many columns as the floor allows when no count is set', () => {
|
|
95
|
+
// Three 240px columns and two gaps need 768px; four need 1032px.
|
|
96
|
+
expect(
|
|
97
|
+
resolveColumnCount( { minColumnWidth: 240 }, 1000, GAP, 6 )
|
|
98
|
+
).toBe( 3 );
|
|
99
|
+
expect(
|
|
100
|
+
resolveColumnCount( { minColumnWidth: 240 }, 1032, GAP, 6 )
|
|
101
|
+
).toBe( 4 );
|
|
102
|
+
} );
|
|
103
|
+
|
|
104
|
+
it( 'caps the floor count with the policy count', () => {
|
|
105
|
+
expect(
|
|
106
|
+
resolveColumnCount(
|
|
107
|
+
{ columns: 2, minColumnWidth: 240 },
|
|
108
|
+
1000,
|
|
109
|
+
GAP,
|
|
110
|
+
6
|
|
111
|
+
)
|
|
112
|
+
).toBe( 2 );
|
|
113
|
+
expect(
|
|
114
|
+
resolveColumnCount(
|
|
115
|
+
{ columns: 4, minColumnWidth: 240 },
|
|
116
|
+
1000,
|
|
117
|
+
GAP,
|
|
118
|
+
6
|
|
119
|
+
)
|
|
120
|
+
).toBe( 3 );
|
|
121
|
+
} );
|
|
122
|
+
|
|
123
|
+
it( 'lowers a step count with the floor', () => {
|
|
124
|
+
// The 960px step asks for four, but four 300px columns need 1272px.
|
|
125
|
+
expect(
|
|
126
|
+
resolveColumnCount(
|
|
127
|
+
{ columns: STEPS, minColumnWidth: 300 },
|
|
128
|
+
1000,
|
|
129
|
+
GAP,
|
|
130
|
+
6
|
|
131
|
+
)
|
|
132
|
+
).toBe( 3 );
|
|
133
|
+
expect(
|
|
134
|
+
resolveColumnCount(
|
|
135
|
+
{ columns: STEPS, minColumnWidth: 300 },
|
|
136
|
+
1280,
|
|
137
|
+
GAP,
|
|
138
|
+
6
|
|
139
|
+
)
|
|
140
|
+
).toBe( 4 );
|
|
141
|
+
} );
|
|
142
|
+
|
|
143
|
+
it( 'lets the floor decide on an auto step', () => {
|
|
144
|
+
// Five 240px columns and four gaps need 1296px.
|
|
145
|
+
expect(
|
|
146
|
+
resolveColumnCount(
|
|
147
|
+
{ columns: AUTO_STEPS, minColumnWidth: 240 },
|
|
148
|
+
1300,
|
|
149
|
+
GAP,
|
|
150
|
+
6
|
|
151
|
+
)
|
|
152
|
+
).toBe( 5 );
|
|
153
|
+
expect(
|
|
154
|
+
resolveColumnCount(
|
|
155
|
+
{ columns: AUTO_STEPS, minColumnWidth: 240 },
|
|
156
|
+
800,
|
|
157
|
+
GAP,
|
|
158
|
+
6
|
|
159
|
+
)
|
|
160
|
+
).toBe( 2 );
|
|
161
|
+
} );
|
|
162
|
+
|
|
163
|
+
it( 'keeps the count below an auto step with no floor', () => {
|
|
164
|
+
expect(
|
|
165
|
+
resolveColumnCount(
|
|
166
|
+
{ columns: { 0: 8, 960: 'auto' } },
|
|
167
|
+
1000,
|
|
168
|
+
GAP,
|
|
169
|
+
6
|
|
170
|
+
)
|
|
171
|
+
).toBe( 8 );
|
|
172
|
+
expect(
|
|
173
|
+
resolveColumnCount( { columns: AUTO_STEPS }, 1000, GAP, 6 )
|
|
174
|
+
).toBe( 2 );
|
|
175
|
+
} );
|
|
176
|
+
|
|
177
|
+
it( 'renders the default on a lone auto step with no floor', () => {
|
|
178
|
+
expect(
|
|
179
|
+
resolveColumnCount( { columns: { 0: 'auto' } }, 1000, GAP, 6 )
|
|
180
|
+
).toBe( 6 );
|
|
181
|
+
} );
|
|
182
|
+
|
|
183
|
+
it( 'treats a count that is not a finite number as unset', () => {
|
|
184
|
+
expect( resolveColumnCount( { columns: NaN }, 1000, GAP, 6 ) ).toBe(
|
|
185
|
+
6
|
|
186
|
+
);
|
|
187
|
+
expect(
|
|
188
|
+
resolveColumnCount(
|
|
189
|
+
{ columns: NaN, minColumnWidth: 240 },
|
|
190
|
+
1000,
|
|
191
|
+
GAP,
|
|
192
|
+
6
|
|
193
|
+
)
|
|
194
|
+
).toBe( 3 );
|
|
195
|
+
} );
|
|
196
|
+
|
|
197
|
+
it( 'ignores a floor that is not a finite positive number', () => {
|
|
198
|
+
expect(
|
|
199
|
+
resolveColumnCount( { minColumnWidth: Infinity }, 1000, GAP, 6 )
|
|
200
|
+
).toBe( 6 );
|
|
201
|
+
expect(
|
|
202
|
+
resolveColumnCount( { minColumnWidth: NaN }, 1000, GAP, 6 )
|
|
203
|
+
).toBe( 6 );
|
|
204
|
+
expect(
|
|
205
|
+
resolveColumnCount( { minColumnWidth: -1 }, 1000, GAP, 6 )
|
|
206
|
+
).toBe( 6 );
|
|
207
|
+
} );
|
|
208
|
+
|
|
209
|
+
it( 'never renders fewer than one column', () => {
|
|
210
|
+
expect( resolveColumnCount( { columns: 0 }, 1000, GAP, 6 ) ).toBe( 1 );
|
|
211
|
+
expect( resolveColumnCount( { minColumnWidth: 240 }, 0, GAP, 6 ) ).toBe(
|
|
212
|
+
1
|
|
213
|
+
);
|
|
214
|
+
} );
|
|
215
|
+
} );
|