@wordpress/grid 0.5.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 +22 -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 +110 -48
- 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 +62 -20
- 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 +111 -50
- 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 +63 -22
- 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 +10 -10
- package/src/dashboard-grid/grid-item.tsx +21 -7
- package/src/dashboard-grid/grid.module.css +6 -6
- package/src/dashboard-grid/index.tsx +155 -58
- package/src/dashboard-grid/resolve-fill-widths.ts +85 -62
- package/src/dashboard-grid/stories/index.story.tsx +169 -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 +87 -25
- package/src/dashboard-lanes/lanes-item.tsx +34 -6
- package/src/dashboard-lanes/lanes.module.css +6 -6
- package/src/dashboard-lanes/stories/index.story.tsx +144 -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/test/use-lane-placement.test.tsx +10 -3
- 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
- package/build-types/dashboard-grid/stories/index.story.d.ts +0 -92
- package/build-types/dashboard-grid/stories/index.story.d.ts.map +0 -1
- package/build-types/dashboard-lanes/stories/index.story.d.ts +0 -58
- package/build-types/dashboard-lanes/stories/index.story.d.ts.map +0 -1
|
@@ -483,6 +483,11 @@ export const RowHeight: Story = {
|
|
|
483
483
|
* to drop.
|
|
484
484
|
*/
|
|
485
485
|
export const EditMode: Story = {
|
|
486
|
+
parameters: {
|
|
487
|
+
// FIXME: Resize handles are nameless aria commands; edit mode nests interactive controls (aria-command-name, nested-interactive).
|
|
488
|
+
// See: https://github.com/WordPress/gutenberg/issues/81596
|
|
489
|
+
a11y: { test: 'todo' },
|
|
490
|
+
},
|
|
486
491
|
args: {
|
|
487
492
|
columns: 12,
|
|
488
493
|
rowHeight: 80,
|
|
@@ -642,6 +647,160 @@ export const EditMode: Story = {
|
|
|
642
647
|
},
|
|
643
648
|
};
|
|
644
649
|
|
|
650
|
+
// Human-readable summary of each constrained tile's `itemLimits` entry,
|
|
651
|
+
// rendered inside the tile beside its live width label.
|
|
652
|
+
const LIMIT_LABELS: Record< string, string > = {
|
|
653
|
+
floor: 'min 320 × 200 px',
|
|
654
|
+
ceiling: 'max 500 × 400 px',
|
|
655
|
+
ranged: 'min 200, max 600 wide',
|
|
656
|
+
bounded: 'min 300 × 160, max 600 × 320 px',
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Per-item size limits via `itemLimits`, in pixels: a floor, a ceiling,
|
|
661
|
+
* a width range, and a tile bounded on both axes. Constrained tiles pin
|
|
662
|
+
* at their bounds while the pointer overshoots, and release commits
|
|
663
|
+
* spans inside the limits. `'full'` and `'fill'` stop at the tile's
|
|
664
|
+
* maximum span; the per-tile toggles exercise both.
|
|
665
|
+
*/
|
|
666
|
+
export const SizeLimits: Story = {
|
|
667
|
+
name: 'Per-Item Size Limits',
|
|
668
|
+
parameters: {
|
|
669
|
+
// FIXME: Resize handles are nameless aria commands; edit mode nests interactive controls (aria-command-name, nested-interactive).
|
|
670
|
+
// See: https://github.com/WordPress/gutenberg/issues/81596
|
|
671
|
+
a11y: { test: 'todo' },
|
|
672
|
+
},
|
|
673
|
+
args: {
|
|
674
|
+
columns: 12,
|
|
675
|
+
rowHeight: 80,
|
|
676
|
+
editMode: true,
|
|
677
|
+
itemLimits: {
|
|
678
|
+
floor: { minWidth: 320, minHeight: 200 },
|
|
679
|
+
ceiling: { maxWidth: 500, maxHeight: 400 },
|
|
680
|
+
ranged: { minWidth: 200, maxWidth: 600 },
|
|
681
|
+
bounded: {
|
|
682
|
+
minWidth: 300,
|
|
683
|
+
minHeight: 160,
|
|
684
|
+
maxWidth: 600,
|
|
685
|
+
maxHeight: 320,
|
|
686
|
+
},
|
|
687
|
+
},
|
|
688
|
+
},
|
|
689
|
+
render: function SizeLimitsRender( args ) {
|
|
690
|
+
const initialLayout: ( DashboardGridLayoutItem & {
|
|
691
|
+
tone: Tone;
|
|
692
|
+
} )[] = [
|
|
693
|
+
{ key: 'floor', width: 4, height: 2, order: 1, tone: 'info' },
|
|
694
|
+
{ key: 'ceiling', width: 2, height: 2, order: 2, tone: 'warning' },
|
|
695
|
+
{ key: 'ranged', width: 3, height: 1, order: 3, tone: 'brand' },
|
|
696
|
+
{ key: 'bounded', width: 2, height: 1, order: 4, tone: 'success' },
|
|
697
|
+
{ key: 'free', width: 3, height: 1, order: 5, tone: 'neutral' },
|
|
698
|
+
];
|
|
699
|
+
|
|
700
|
+
const [ tiles, setTiles ] = useState( initialLayout );
|
|
701
|
+
const [ previewLayout, setPreviewLayout ] = useState<
|
|
702
|
+
DashboardGridLayoutItem[] | null
|
|
703
|
+
>( null );
|
|
704
|
+
|
|
705
|
+
const layout: DashboardGridLayoutItem[] = tiles.map(
|
|
706
|
+
( { tone: _tone, ...item } ) => item
|
|
707
|
+
);
|
|
708
|
+
|
|
709
|
+
const onChangeLayout = ( next: DashboardGridLayoutItem[] ) => {
|
|
710
|
+
setTiles(
|
|
711
|
+
next.map( ( item ) => {
|
|
712
|
+
const existing = tiles.find( ( t ) => t.key === item.key );
|
|
713
|
+
return {
|
|
714
|
+
...item,
|
|
715
|
+
tone: existing?.tone ?? 'neutral',
|
|
716
|
+
};
|
|
717
|
+
} )
|
|
718
|
+
);
|
|
719
|
+
setPreviewLayout( null );
|
|
720
|
+
};
|
|
721
|
+
|
|
722
|
+
const removeTile = ( key: string ) => {
|
|
723
|
+
setTiles( tiles.filter( ( tile ) => tile.key !== key ) );
|
|
724
|
+
};
|
|
725
|
+
|
|
726
|
+
const toggleFill = ( key: string ) => {
|
|
727
|
+
setTiles(
|
|
728
|
+
tiles.map( ( tile ) =>
|
|
729
|
+
tile.key === key
|
|
730
|
+
? {
|
|
731
|
+
...tile,
|
|
732
|
+
width:
|
|
733
|
+
tile.width === 'fill' ? undefined : 'fill',
|
|
734
|
+
}
|
|
735
|
+
: tile
|
|
736
|
+
)
|
|
737
|
+
);
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
const toggleFull = ( key: string ) => {
|
|
741
|
+
setTiles(
|
|
742
|
+
tiles.map( ( tile ) =>
|
|
743
|
+
tile.key === key
|
|
744
|
+
? {
|
|
745
|
+
...tile,
|
|
746
|
+
width:
|
|
747
|
+
tile.width === 'full' ? undefined : 'full',
|
|
748
|
+
}
|
|
749
|
+
: tile
|
|
750
|
+
)
|
|
751
|
+
);
|
|
752
|
+
};
|
|
753
|
+
|
|
754
|
+
const tileElements = useMemo(
|
|
755
|
+
() =>
|
|
756
|
+
tiles.map( ( tile ) => (
|
|
757
|
+
<Tile
|
|
758
|
+
key={ tile.key }
|
|
759
|
+
tone={ tile.tone }
|
|
760
|
+
actionableArea={
|
|
761
|
+
<TileActions
|
|
762
|
+
isFill={ tile.width === 'fill' }
|
|
763
|
+
isFull={ tile.width === 'full' }
|
|
764
|
+
onToggleFill={ () => toggleFill( tile.key ) }
|
|
765
|
+
onToggleFull={ () => toggleFull( tile.key ) }
|
|
766
|
+
onRemove={ () => removeTile( tile.key ) }
|
|
767
|
+
/>
|
|
768
|
+
}
|
|
769
|
+
>
|
|
770
|
+
<span style={ { textAlign: 'center' } }>
|
|
771
|
+
{ formatTileLabel( tile ) }
|
|
772
|
+
<br />
|
|
773
|
+
{ LIMIT_LABELS[ tile.key ] ?? 'no limits' }
|
|
774
|
+
</span>
|
|
775
|
+
</Tile>
|
|
776
|
+
) ),
|
|
777
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
778
|
+
[ tiles ]
|
|
779
|
+
);
|
|
780
|
+
|
|
781
|
+
return (
|
|
782
|
+
<Stack direction="row" gap="lg" align="flex-start">
|
|
783
|
+
<div style={ { width: '800px' } }>
|
|
784
|
+
<DashboardGrid
|
|
785
|
+
{ ...args }
|
|
786
|
+
layout={ layout }
|
|
787
|
+
onChangeLayout={ onChangeLayout }
|
|
788
|
+
onPreviewLayout={ setPreviewLayout }
|
|
789
|
+
>
|
|
790
|
+
{ tileElements }
|
|
791
|
+
</DashboardGrid>
|
|
792
|
+
</div>
|
|
793
|
+
|
|
794
|
+
<LayoutStatePanel
|
|
795
|
+
label={ previewLayout ? 'Staging' : 'Committed' }
|
|
796
|
+
layout={ previewLayout ?? layout }
|
|
797
|
+
tone={ previewLayout ? 'warning' : 'success' }
|
|
798
|
+
/>
|
|
799
|
+
</Stack>
|
|
800
|
+
);
|
|
801
|
+
},
|
|
802
|
+
};
|
|
803
|
+
|
|
645
804
|
/**
|
|
646
805
|
* Custom corner-resize glyph: a diagonal line plus a filled triangle,
|
|
647
806
|
* both leaning toward the bottom-right corner of the tile.
|
|
@@ -723,6 +882,11 @@ function CustomDragPreview( { children }: DragPreviewRenderProps ) {
|
|
|
723
882
|
* respond.
|
|
724
883
|
*/
|
|
725
884
|
export const Customization: Story = {
|
|
885
|
+
parameters: {
|
|
886
|
+
// FIXME: Resize handles are nameless aria commands; edit mode nests interactive controls (aria-command-name, nested-interactive).
|
|
887
|
+
// See: https://github.com/WordPress/gutenberg/issues/81596
|
|
888
|
+
a11y: { test: 'todo' },
|
|
889
|
+
},
|
|
726
890
|
args: {
|
|
727
891
|
columns: 6,
|
|
728
892
|
rowHeight: 80,
|
|
@@ -866,6 +1030,11 @@ function NumberedOverlay( { columns, isActive }: GridOverlayRenderProps ) {
|
|
|
866
1030
|
* on.
|
|
867
1031
|
*/
|
|
868
1032
|
export const CustomGridOverlayStory: Story = {
|
|
1033
|
+
parameters: {
|
|
1034
|
+
// FIXME: Resize handles are nameless aria commands; edit mode nests interactive controls (aria-command-name, nested-interactive).
|
|
1035
|
+
// See: https://github.com/WordPress/gutenberg/issues/81596
|
|
1036
|
+
a11y: { test: 'todo' },
|
|
1037
|
+
},
|
|
869
1038
|
name: 'Custom Grid Overlay',
|
|
870
1039
|
args: {
|
|
871
1040
|
columns: 12,
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { render } from '@testing-library/react';
|
|
2
|
+
import { DashboardGrid } from '..';
|
|
3
|
+
|
|
4
|
+
class MockResizeObserver {
|
|
5
|
+
observe() {}
|
|
6
|
+
unobserve() {}
|
|
7
|
+
disconnect() {}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let originalResizeObserver: typeof ResizeObserver;
|
|
11
|
+
|
|
12
|
+
beforeEach( () => {
|
|
13
|
+
originalResizeObserver = global.ResizeObserver;
|
|
14
|
+
( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
15
|
+
MockResizeObserver;
|
|
16
|
+
} );
|
|
17
|
+
|
|
18
|
+
afterEach( () => {
|
|
19
|
+
( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
20
|
+
originalResizeObserver;
|
|
21
|
+
} );
|
|
22
|
+
|
|
23
|
+
/* eslint-disable testing-library/no-container */
|
|
24
|
+
function renderItem( flags: { draggable?: boolean; resizable?: boolean } ) {
|
|
25
|
+
const { container } = render(
|
|
26
|
+
<DashboardGrid
|
|
27
|
+
layout={ [ { key: 'a', width: 1, ...flags } ] }
|
|
28
|
+
columns={ 2 }
|
|
29
|
+
editMode
|
|
30
|
+
>
|
|
31
|
+
<div key="a">A</div>
|
|
32
|
+
</DashboardGrid>
|
|
33
|
+
);
|
|
34
|
+
return {
|
|
35
|
+
activator: container.querySelector(
|
|
36
|
+
'[aria-roledescription="sortable"]'
|
|
37
|
+
),
|
|
38
|
+
resizeHandle: container.querySelector(
|
|
39
|
+
'[aria-roledescription="draggable"]'
|
|
40
|
+
),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/* eslint-enable testing-library/no-container */
|
|
44
|
+
|
|
45
|
+
describe( 'DashboardGrid item interactions', () => {
|
|
46
|
+
it( 'drags and resizes by default in edit mode', () => {
|
|
47
|
+
const { activator, resizeHandle } = renderItem( {} );
|
|
48
|
+
expect( activator ).toHaveAttribute( 'aria-disabled', 'false' );
|
|
49
|
+
expect( resizeHandle ).not.toBeNull();
|
|
50
|
+
} );
|
|
51
|
+
|
|
52
|
+
it( 'keeps an item in place when draggable is false', () => {
|
|
53
|
+
const { activator, resizeHandle } = renderItem( { draggable: false } );
|
|
54
|
+
expect( activator ).toHaveAttribute( 'aria-disabled', 'true' );
|
|
55
|
+
expect( resizeHandle ).not.toBeNull();
|
|
56
|
+
} );
|
|
57
|
+
|
|
58
|
+
it( 'keeps an item at its size when resizable is false', () => {
|
|
59
|
+
const { activator, resizeHandle } = renderItem( { resizable: false } );
|
|
60
|
+
expect( activator ).toHaveAttribute( 'aria-disabled', 'false' );
|
|
61
|
+
expect( resizeHandle ).toBeNull();
|
|
62
|
+
} );
|
|
63
|
+
} );
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { act, fireEvent, render } from '@testing-library/react';
|
|
2
|
+
import { DashboardGrid } from '..';
|
|
3
|
+
import type { DashboardGridLayoutItem } from '../types';
|
|
4
|
+
|
|
5
|
+
class MockResizeObserver {
|
|
6
|
+
observe() {}
|
|
7
|
+
unobserve() {}
|
|
8
|
+
disconnect() {}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let originalResizeObserver: typeof ResizeObserver;
|
|
12
|
+
let originalGetBoundingClientRect: typeof HTMLElement.prototype.getBoundingClientRect;
|
|
13
|
+
|
|
14
|
+
function rect( left: number, top: number, width: number, height: number ) {
|
|
15
|
+
return {
|
|
16
|
+
left,
|
|
17
|
+
top,
|
|
18
|
+
width,
|
|
19
|
+
height,
|
|
20
|
+
right: left + width,
|
|
21
|
+
bottom: top + height,
|
|
22
|
+
x: left,
|
|
23
|
+
y: top,
|
|
24
|
+
toJSON() {},
|
|
25
|
+
} as DOMRect;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Container 240px wide, six columns, fallback gap 24px: one column
|
|
29
|
+
// track is 20px + 24px gap = 44px. A 60px minimum width quantizes to
|
|
30
|
+
// two columns; one keyboard step (25px) rounds to one column.
|
|
31
|
+
const CONTAINER_RECT = rect( 0, 0, 240, 600 );
|
|
32
|
+
|
|
33
|
+
// Tile rects for the sortable keyboard coordinates: `b` sits to the
|
|
34
|
+
// right of `a` and is wider, so a step right lands past its center.
|
|
35
|
+
const ITEM_RECTS: Record< string, DOMRect > = {
|
|
36
|
+
a: rect( 0, 0, 100, 100 ),
|
|
37
|
+
b: rect( 120, 0, 200, 100 ),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Space picks the handle up, one arrow step moves 25px, Space drops.
|
|
41
|
+
// dnd-kit binds its keyboard listener on a timeout after pickup and
|
|
42
|
+
// settles the drop through a microtask and timers, so both flushes run
|
|
43
|
+
// inside `act`.
|
|
44
|
+
async function activateAndStepRight( activator: Element ) {
|
|
45
|
+
fireEvent.keyDown( activator, { code: 'Space' } );
|
|
46
|
+
act( () => {
|
|
47
|
+
jest.runOnlyPendingTimers();
|
|
48
|
+
} );
|
|
49
|
+
fireEvent.keyDown( activator, { code: 'ArrowRight' } );
|
|
50
|
+
fireEvent.keyDown( activator, { code: 'Space' } );
|
|
51
|
+
await act( async () => {
|
|
52
|
+
jest.runOnlyPendingTimers();
|
|
53
|
+
} );
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
beforeEach( () => {
|
|
57
|
+
jest.useFakeTimers();
|
|
58
|
+
originalResizeObserver = global.ResizeObserver;
|
|
59
|
+
( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
60
|
+
MockResizeObserver;
|
|
61
|
+
originalGetBoundingClientRect = HTMLElement.prototype.getBoundingClientRect;
|
|
62
|
+
HTMLElement.prototype.getBoundingClientRect = function () {
|
|
63
|
+
// eslint-disable-next-line testing-library/no-node-access
|
|
64
|
+
const key = this.closest( '[data-wp-grid-item-key]' )?.getAttribute(
|
|
65
|
+
'data-wp-grid-item-key'
|
|
66
|
+
);
|
|
67
|
+
return ( key && ITEM_RECTS[ key ] ) || CONTAINER_RECT;
|
|
68
|
+
};
|
|
69
|
+
} );
|
|
70
|
+
|
|
71
|
+
afterEach( () => {
|
|
72
|
+
( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
|
|
73
|
+
originalResizeObserver;
|
|
74
|
+
HTMLElement.prototype.getBoundingClientRect = originalGetBoundingClientRect;
|
|
75
|
+
jest.useRealTimers();
|
|
76
|
+
} );
|
|
77
|
+
|
|
78
|
+
function renderGrid( onChangeLayout: jest.Mock ) {
|
|
79
|
+
const layout: DashboardGridLayoutItem[] = [
|
|
80
|
+
{ key: 'a', width: 1, height: 1 },
|
|
81
|
+
{ key: 'b', width: 1, height: 1 },
|
|
82
|
+
];
|
|
83
|
+
return render(
|
|
84
|
+
<DashboardGrid
|
|
85
|
+
layout={ layout }
|
|
86
|
+
columns={ 6 }
|
|
87
|
+
itemLimits={ { a: { minWidth: 60 } } }
|
|
88
|
+
editMode
|
|
89
|
+
onChangeLayout={ onChangeLayout }
|
|
90
|
+
>
|
|
91
|
+
<div key="a">A</div>
|
|
92
|
+
<div key="b">B</div>
|
|
93
|
+
</DashboardGrid>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/* eslint-disable testing-library/no-container, testing-library/no-node-access */
|
|
98
|
+
describe( 'DashboardGrid item limits', () => {
|
|
99
|
+
it( 'renders a stored span below the floor lifted to it', () => {
|
|
100
|
+
const { container } = renderGrid( jest.fn() );
|
|
101
|
+
const itemA = container.querySelector( '[data-wp-grid-item-key="a"]' );
|
|
102
|
+
expect( itemA ).toHaveStyle( { gridColumnEnd: 'span 2' } );
|
|
103
|
+
} );
|
|
104
|
+
|
|
105
|
+
it( 'resizing another tile leaves the stored span untouched', async () => {
|
|
106
|
+
const onChangeLayout = jest.fn();
|
|
107
|
+
const { container } = renderGrid( onChangeLayout );
|
|
108
|
+
const handleB = container.querySelector(
|
|
109
|
+
'[data-wp-grid-item-key="b"] [aria-roledescription="draggable"]'
|
|
110
|
+
);
|
|
111
|
+
expect( handleB ).not.toBeNull();
|
|
112
|
+
|
|
113
|
+
await activateAndStepRight( handleB! );
|
|
114
|
+
|
|
115
|
+
expect( onChangeLayout ).toHaveBeenCalledTimes( 1 );
|
|
116
|
+
const committed: DashboardGridLayoutItem[] =
|
|
117
|
+
onChangeLayout.mock.calls[ 0 ][ 0 ];
|
|
118
|
+
expect( committed.find( ( item ) => item.key === 'b' )?.width ).toBe(
|
|
119
|
+
2
|
|
120
|
+
);
|
|
121
|
+
expect( committed.find( ( item ) => item.key === 'a' )?.width ).toBe(
|
|
122
|
+
1
|
|
123
|
+
);
|
|
124
|
+
} );
|
|
125
|
+
|
|
126
|
+
it( 'reordering leaves the stored span untouched', async () => {
|
|
127
|
+
const onChangeLayout = jest.fn();
|
|
128
|
+
const { container } = renderGrid( onChangeLayout );
|
|
129
|
+
const activatorA = container.querySelector(
|
|
130
|
+
'[data-wp-grid-item-key="a"] [aria-roledescription="sortable"]'
|
|
131
|
+
);
|
|
132
|
+
expect( activatorA ).not.toBeNull();
|
|
133
|
+
|
|
134
|
+
await activateAndStepRight( activatorA! );
|
|
135
|
+
|
|
136
|
+
expect( onChangeLayout ).toHaveBeenCalledTimes( 1 );
|
|
137
|
+
const committed: DashboardGridLayoutItem[] =
|
|
138
|
+
onChangeLayout.mock.calls[ 0 ][ 0 ];
|
|
139
|
+
expect( committed.find( ( item ) => item.key === 'a' )?.order ).toBe(
|
|
140
|
+
1
|
|
141
|
+
);
|
|
142
|
+
expect( committed.find( ( item ) => item.key === 'a' )?.width ).toBe(
|
|
143
|
+
1
|
|
144
|
+
);
|
|
145
|
+
} );
|
|
146
|
+
} );
|
|
147
|
+
/* eslint-enable testing-library/no-container, testing-library/no-node-access */
|
|
@@ -244,4 +244,162 @@ describe( 'resolveFillWidths', () => {
|
|
|
244
244
|
expect( result.get( 'fill' ) ).toBe( 9 );
|
|
245
245
|
} );
|
|
246
246
|
} );
|
|
247
|
+
|
|
248
|
+
describe( 'with item bounds', () => {
|
|
249
|
+
function makeBounds(
|
|
250
|
+
entries: Record< string, { minWidth: number; maxWidth: number } >
|
|
251
|
+
): Map< string, { minWidth: number; maxWidth: number } > {
|
|
252
|
+
return new Map( Object.entries( entries ) );
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
it( 'reserves the fill floor when planning the row', () => {
|
|
256
|
+
// Without bounds the plan is 1 + fill(2) + 3 = 6; the floor
|
|
257
|
+
// pushes the trailing item out, so the fill takes the rest.
|
|
258
|
+
const items: DashboardGridLayoutItem[] = [
|
|
259
|
+
{ key: 'a', width: 1 },
|
|
260
|
+
{ key: 'fill', width: 'fill' },
|
|
261
|
+
{ key: 'b', width: 3 },
|
|
262
|
+
];
|
|
263
|
+
const result = resolveFillWidths(
|
|
264
|
+
keys( items ),
|
|
265
|
+
makeMap( items ),
|
|
266
|
+
6,
|
|
267
|
+
makeBounds( { fill: { minWidth: 3, maxWidth: 6 } } )
|
|
268
|
+
);
|
|
269
|
+
expect( result.get( 'fill' ) ).toBe( 5 );
|
|
270
|
+
} );
|
|
271
|
+
|
|
272
|
+
it( 'caps the fill at its maximum span', () => {
|
|
273
|
+
const items: DashboardGridLayoutItem[] = [
|
|
274
|
+
{ key: 'a', width: 1 },
|
|
275
|
+
{ key: 'fill', width: 'fill' },
|
|
276
|
+
];
|
|
277
|
+
const result = resolveFillWidths(
|
|
278
|
+
keys( items ),
|
|
279
|
+
makeMap( items ),
|
|
280
|
+
6,
|
|
281
|
+
makeBounds( { fill: { minWidth: 1, maxWidth: 2 } } )
|
|
282
|
+
);
|
|
283
|
+
expect( result.get( 'fill' ) ).toBe( 2 );
|
|
284
|
+
} );
|
|
285
|
+
|
|
286
|
+
it( 'wraps a floored fill that no longer fits the row', () => {
|
|
287
|
+
const items: DashboardGridLayoutItem[] = [
|
|
288
|
+
{ key: 'a', width: 4 },
|
|
289
|
+
{ key: 'fill', width: 'fill' },
|
|
290
|
+
];
|
|
291
|
+
const result = resolveFillWidths(
|
|
292
|
+
keys( items ),
|
|
293
|
+
makeMap( items ),
|
|
294
|
+
6,
|
|
295
|
+
makeBounds( { fill: { minWidth: 3, maxWidth: 6 } } )
|
|
296
|
+
);
|
|
297
|
+
expect( result.get( 'fill' ) ).toBe( 6 );
|
|
298
|
+
} );
|
|
299
|
+
|
|
300
|
+
it( 'scans for a free run wide enough for the floor', () => {
|
|
301
|
+
// The row-1 run beside the tall tile is nine columns; a
|
|
302
|
+
// twelve-column floor sends the fill to the next empty row.
|
|
303
|
+
const items: DashboardGridLayoutItem[] = [
|
|
304
|
+
{ key: 'tall', width: 3, height: 2 },
|
|
305
|
+
{ key: 'header', width: 9, height: 1 },
|
|
306
|
+
{ key: 'fill', width: 'fill', height: 1 },
|
|
307
|
+
];
|
|
308
|
+
const result = resolveFillWidths(
|
|
309
|
+
keys( items ),
|
|
310
|
+
makeMap( items ),
|
|
311
|
+
12,
|
|
312
|
+
makeBounds( { fill: { minWidth: 12, maxWidth: 12 } } )
|
|
313
|
+
);
|
|
314
|
+
expect( result.get( 'fill' ) ).toBe( 12 );
|
|
315
|
+
} );
|
|
316
|
+
|
|
317
|
+
it( 'reserves trailing items against the floored run', () => {
|
|
318
|
+
const items: DashboardGridLayoutItem[] = [
|
|
319
|
+
{ key: 'tall', width: 3, height: 2 },
|
|
320
|
+
{ key: 'header', width: 9, height: 1 },
|
|
321
|
+
{ key: 'fill', width: 'fill', height: 1 },
|
|
322
|
+
{ key: 'trailing', width: 3, height: 1 },
|
|
323
|
+
];
|
|
324
|
+
const result = resolveFillWidths(
|
|
325
|
+
keys( items ),
|
|
326
|
+
makeMap( items ),
|
|
327
|
+
12,
|
|
328
|
+
makeBounds( { fill: { minWidth: 6, maxWidth: 12 } } )
|
|
329
|
+
);
|
|
330
|
+
expect( result.get( 'fill' ) ).toBe( 6 );
|
|
331
|
+
} );
|
|
332
|
+
|
|
333
|
+
it( 'caps the fill inside a free run', () => {
|
|
334
|
+
const items: DashboardGridLayoutItem[] = [
|
|
335
|
+
{ key: 'tall', width: 3, height: 2 },
|
|
336
|
+
{ key: 'header', width: 9, height: 1 },
|
|
337
|
+
{ key: 'fill', width: 'fill', height: 1 },
|
|
338
|
+
];
|
|
339
|
+
const result = resolveFillWidths(
|
|
340
|
+
keys( items ),
|
|
341
|
+
makeMap( items ),
|
|
342
|
+
12,
|
|
343
|
+
makeBounds( { fill: { minWidth: 1, maxWidth: 6 } } )
|
|
344
|
+
);
|
|
345
|
+
expect( result.get( 'fill' ) ).toBe( 6 );
|
|
346
|
+
} );
|
|
347
|
+
|
|
348
|
+
it( 'places a capped full item like a fixed item of its capped width', () => {
|
|
349
|
+
const items: DashboardGridLayoutItem[] = [
|
|
350
|
+
{ key: 'full', width: 'full' },
|
|
351
|
+
{ key: 'fill', width: 'fill' },
|
|
352
|
+
];
|
|
353
|
+
const result = resolveFillWidths(
|
|
354
|
+
keys( items ),
|
|
355
|
+
makeMap( items ),
|
|
356
|
+
12,
|
|
357
|
+
makeBounds( { full: { minWidth: 1, maxWidth: 4 } } )
|
|
358
|
+
);
|
|
359
|
+
expect( result.get( 'fill' ) ).toBe( 8 );
|
|
360
|
+
} );
|
|
361
|
+
|
|
362
|
+
it( 'reserves a capped full item in the fill look-ahead', () => {
|
|
363
|
+
const items: DashboardGridLayoutItem[] = [
|
|
364
|
+
{ key: 'fill', width: 'fill' },
|
|
365
|
+
{ key: 'full', width: 'full' },
|
|
366
|
+
];
|
|
367
|
+
const result = resolveFillWidths(
|
|
368
|
+
keys( items ),
|
|
369
|
+
makeMap( items ),
|
|
370
|
+
12,
|
|
371
|
+
makeBounds( { full: { minWidth: 1, maxWidth: 4 } } )
|
|
372
|
+
);
|
|
373
|
+
expect( result.get( 'fill' ) ).toBe( 8 );
|
|
374
|
+
} );
|
|
375
|
+
|
|
376
|
+
it( 'places a capped full item beside a tall tile', () => {
|
|
377
|
+
const items: DashboardGridLayoutItem[] = [
|
|
378
|
+
{ key: 'tall', width: 3, height: 2 },
|
|
379
|
+
{ key: 'full', width: 'full', height: 1 },
|
|
380
|
+
{ key: 'fill', width: 'fill', height: 1 },
|
|
381
|
+
];
|
|
382
|
+
const result = resolveFillWidths(
|
|
383
|
+
keys( items ),
|
|
384
|
+
makeMap( items ),
|
|
385
|
+
12,
|
|
386
|
+
makeBounds( { full: { minWidth: 1, maxWidth: 4 } } )
|
|
387
|
+
);
|
|
388
|
+
expect( result.get( 'fill' ) ).toBe( 5 );
|
|
389
|
+
} );
|
|
390
|
+
|
|
391
|
+
it( 'keeps a full item at the column count when its cap allows it', () => {
|
|
392
|
+
const items: DashboardGridLayoutItem[] = [
|
|
393
|
+
{ key: 'full', width: 'full' },
|
|
394
|
+
{ key: 'fill', width: 'fill' },
|
|
395
|
+
];
|
|
396
|
+
const result = resolveFillWidths(
|
|
397
|
+
keys( items ),
|
|
398
|
+
makeMap( items ),
|
|
399
|
+
12,
|
|
400
|
+
makeBounds( { full: { minWidth: 1, maxWidth: 12 } } )
|
|
401
|
+
);
|
|
402
|
+
expect( result.get( 'fill' ) ).toBe( 12 );
|
|
403
|
+
} );
|
|
404
|
+
} );
|
|
247
405
|
} );
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
DragPreviewRenderProps,
|
|
3
|
+
GridItemLimits,
|
|
3
4
|
GridOverlayRenderProps,
|
|
4
5
|
ResizeDelta,
|
|
5
6
|
ResizeHandleRenderProps,
|
|
@@ -38,6 +39,22 @@ export type DashboardGridLayoutItem = {
|
|
|
38
39
|
* omitted, the item falls back to its index in the `layout` array.
|
|
39
40
|
*/
|
|
40
41
|
order?: number;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Whether the item can be dragged while the surface is in edit mode.
|
|
45
|
+
* When `false`, the item is pinned: it also holds its index while the
|
|
46
|
+
* other items reorder around it.
|
|
47
|
+
*
|
|
48
|
+
* @default true
|
|
49
|
+
*/
|
|
50
|
+
draggable?: boolean;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Whether the item can be resized while the surface is in edit mode.
|
|
54
|
+
*
|
|
55
|
+
* @default true
|
|
56
|
+
*/
|
|
57
|
+
resizable?: boolean;
|
|
41
58
|
};
|
|
42
59
|
|
|
43
60
|
/**
|
|
@@ -61,6 +78,20 @@ export type GridItemProps = {
|
|
|
61
78
|
*/
|
|
62
79
|
disabled?: boolean;
|
|
63
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Whether the item can be dragged. Combined with `disabled`.
|
|
83
|
+
*
|
|
84
|
+
* @default true
|
|
85
|
+
*/
|
|
86
|
+
draggable?: boolean;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Whether the item can be resized. Combined with `disabled`.
|
|
90
|
+
*
|
|
91
|
+
* @default true
|
|
92
|
+
*/
|
|
93
|
+
resizable?: boolean;
|
|
94
|
+
|
|
64
95
|
/**
|
|
65
96
|
* Whether the item can be resized vertically. Disabled when the
|
|
66
97
|
* grid uses `rowHeight: 'auto'`, where row height is driven by
|
|
@@ -127,6 +158,18 @@ export type GridItemProps = {
|
|
|
127
158
|
*/
|
|
128
159
|
minResizeHeightPx?: number;
|
|
129
160
|
|
|
161
|
+
/**
|
|
162
|
+
* Maximum tile width while resizing, in pixels. Omitted when the
|
|
163
|
+
* item declares no width limit.
|
|
164
|
+
*/
|
|
165
|
+
maxResizeWidthPx?: number;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Maximum tile height while resizing, in pixels. Omitted when
|
|
169
|
+
* vertical resize is disabled or the item declares no height limit.
|
|
170
|
+
*/
|
|
171
|
+
maxResizeHeightPx?: number;
|
|
172
|
+
|
|
130
173
|
/**
|
|
131
174
|
* Callback fired when the resize gesture ends.
|
|
132
175
|
*/
|
|
@@ -265,4 +308,11 @@ export interface DashboardGridProps
|
|
|
265
308
|
* down to 1.
|
|
266
309
|
*/
|
|
267
310
|
minColumnWidth?: number;
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Per-item size limits in pixels, keyed by layout item key. Width
|
|
314
|
+
* bounds saturate at the column count; `'full'` and `'fill'` widths
|
|
315
|
+
* respect a maximum.
|
|
316
|
+
*/
|
|
317
|
+
itemLimits?: Record< string, GridItemLimits >;
|
|
268
318
|
}
|