@wordpress/grid 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/CHANGELOG.md +22 -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 +110 -48
  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 +62 -20
  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 +111 -50
  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 +63 -22
  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 +12 -11
  63. package/src/dashboard-grid/grid-item.tsx +21 -7
  64. package/src/dashboard-grid/grid.module.css +6 -6
  65. package/src/dashboard-grid/index.tsx +155 -58
  66. package/src/dashboard-grid/resolve-fill-widths.ts +85 -62
  67. package/src/dashboard-grid/stories/index.story.tsx +169 -0
  68. package/src/dashboard-grid/test/item-interactions.jsdom.test.tsx +64 -0
  69. package/src/dashboard-grid/test/item-limits.jsdom.test.tsx +147 -0
  70. package/src/dashboard-grid/test/{keyboard-activation.test.tsx → keyboard-activation.jsdom.test.tsx} +4 -3
  71. package/src/dashboard-grid/test/resolve-fill-widths.test.ts +159 -0
  72. package/src/dashboard-grid/types.ts +50 -0
  73. package/src/dashboard-lanes/index.tsx +87 -25
  74. package/src/dashboard-lanes/lanes-item.tsx +34 -6
  75. package/src/dashboard-lanes/lanes.module.css +6 -6
  76. package/src/dashboard-lanes/stories/index.story.tsx +144 -0
  77. package/src/dashboard-lanes/test/item-interactions.jsdom.test.tsx +64 -0
  78. package/src/dashboard-lanes/test/item-limits.jsdom.test.tsx +140 -0
  79. package/src/dashboard-lanes/test/{keyboard-activation.test.tsx → keyboard-activation.jsdom.test.tsx} +4 -3
  80. package/src/dashboard-lanes/test/lane-placement.test.ts +1 -0
  81. package/src/dashboard-lanes/test/{use-lane-placement.test.tsx → use-lane-placement.jsdom.test.tsx} +21 -13
  82. package/src/dashboard-lanes/types.ts +23 -0
  83. package/src/index.ts +2 -0
  84. package/src/shared/array-move-with-pinned.ts +36 -0
  85. package/src/shared/resize-snap.ts +161 -5
  86. package/src/shared/test/array-move-with-pinned.test.ts +40 -0
  87. package/src/shared/test/resize-snap.test.ts +171 -2
  88. package/src/shared/test/use-span-bounds.jsdom.test.ts +105 -0
  89. package/src/shared/types.ts +35 -0
  90. package/src/shared/use-span-bounds.ts +99 -0
  91. package/build-types/dashboard-grid/stories/index.story.d.ts +0 -92
  92. package/build-types/dashboard-grid/stories/index.story.d.ts.map +0 -1
  93. package/build-types/dashboard-lanes/stories/index.story.d.ts +0 -58
  94. 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,64 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
+ import { render } from '@testing-library/react';
3
+ import { DashboardGrid } from '..';
4
+
5
+ class MockResizeObserver {
6
+ observe() {}
7
+ unobserve() {}
8
+ disconnect() {}
9
+ }
10
+
11
+ let originalResizeObserver: typeof ResizeObserver;
12
+
13
+ beforeEach( () => {
14
+ originalResizeObserver = globalThis.ResizeObserver;
15
+ ( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
16
+ MockResizeObserver;
17
+ } );
18
+
19
+ afterEach( () => {
20
+ ( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
21
+ originalResizeObserver;
22
+ } );
23
+
24
+ /* eslint-disable testing-library/no-container */
25
+ function renderItem( flags: { draggable?: boolean; resizable?: boolean } ) {
26
+ const { container } = render(
27
+ <DashboardGrid
28
+ layout={ [ { key: 'a', width: 1, ...flags } ] }
29
+ columns={ 2 }
30
+ editMode
31
+ >
32
+ <div key="a">A</div>
33
+ </DashboardGrid>
34
+ );
35
+ return {
36
+ activator: container.querySelector(
37
+ '[aria-roledescription="sortable"]'
38
+ ),
39
+ resizeHandle: container.querySelector(
40
+ '[aria-roledescription="draggable"]'
41
+ ),
42
+ };
43
+ }
44
+ /* eslint-enable testing-library/no-container */
45
+
46
+ describe( 'DashboardGrid item interactions', () => {
47
+ it( 'drags and resizes by default in edit mode', () => {
48
+ const { activator, resizeHandle } = renderItem( {} );
49
+ expect( activator ).toHaveAttribute( 'aria-disabled', 'false' );
50
+ expect( resizeHandle ).not.toBeNull();
51
+ } );
52
+
53
+ it( 'keeps an item in place when draggable is false', () => {
54
+ const { activator, resizeHandle } = renderItem( { draggable: false } );
55
+ expect( activator ).toHaveAttribute( 'aria-disabled', 'true' );
56
+ expect( resizeHandle ).not.toBeNull();
57
+ } );
58
+
59
+ it( 'keeps an item at its size when resizable is false', () => {
60
+ const { activator, resizeHandle } = renderItem( { resizable: false } );
61
+ expect( activator ).toHaveAttribute( 'aria-disabled', 'false' );
62
+ expect( resizeHandle ).toBeNull();
63
+ } );
64
+ } );
@@ -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 */
@@ -1,4 +1,5 @@
1
1
  import { render } from '@testing-library/react';
2
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
3
  import { DashboardGrid } from '..';
3
4
 
4
5
  class MockResizeObserver {
@@ -17,13 +18,13 @@ class MockResizeObserver {
17
18
  let originalResizeObserver: typeof ResizeObserver;
18
19
 
19
20
  beforeEach( () => {
20
- originalResizeObserver = global.ResizeObserver;
21
- ( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
21
+ originalResizeObserver = globalThis.ResizeObserver;
22
+ ( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
22
23
  MockResizeObserver;
23
24
  } );
24
25
 
25
26
  afterEach( () => {
26
- ( global as unknown as { ResizeObserver: unknown } ).ResizeObserver =
27
+ ( globalThis as unknown as { ResizeObserver: unknown } ).ResizeObserver =
27
28
  originalResizeObserver;
28
29
  } );
29
30
 
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest';
1
2
  import { resolveFillWidths } from '../resolve-fill-widths';
2
3
  import type { DashboardGridLayoutItem } from '../types';
3
4
 
@@ -244,4 +245,162 @@ describe( 'resolveFillWidths', () => {
244
245
  expect( result.get( 'fill' ) ).toBe( 9 );
245
246
  } );
246
247
  } );
248
+
249
+ describe( 'with item bounds', () => {
250
+ function makeBounds(
251
+ entries: Record< string, { minWidth: number; maxWidth: number } >
252
+ ): Map< string, { minWidth: number; maxWidth: number } > {
253
+ return new Map( Object.entries( entries ) );
254
+ }
255
+
256
+ it( 'reserves the fill floor when planning the row', () => {
257
+ // Without bounds the plan is 1 + fill(2) + 3 = 6; the floor
258
+ // pushes the trailing item out, so the fill takes the rest.
259
+ const items: DashboardGridLayoutItem[] = [
260
+ { key: 'a', width: 1 },
261
+ { key: 'fill', width: 'fill' },
262
+ { key: 'b', width: 3 },
263
+ ];
264
+ const result = resolveFillWidths(
265
+ keys( items ),
266
+ makeMap( items ),
267
+ 6,
268
+ makeBounds( { fill: { minWidth: 3, maxWidth: 6 } } )
269
+ );
270
+ expect( result.get( 'fill' ) ).toBe( 5 );
271
+ } );
272
+
273
+ it( 'caps the fill at its maximum span', () => {
274
+ const items: DashboardGridLayoutItem[] = [
275
+ { key: 'a', width: 1 },
276
+ { key: 'fill', width: 'fill' },
277
+ ];
278
+ const result = resolveFillWidths(
279
+ keys( items ),
280
+ makeMap( items ),
281
+ 6,
282
+ makeBounds( { fill: { minWidth: 1, maxWidth: 2 } } )
283
+ );
284
+ expect( result.get( 'fill' ) ).toBe( 2 );
285
+ } );
286
+
287
+ it( 'wraps a floored fill that no longer fits the row', () => {
288
+ const items: DashboardGridLayoutItem[] = [
289
+ { key: 'a', width: 4 },
290
+ { key: 'fill', width: 'fill' },
291
+ ];
292
+ const result = resolveFillWidths(
293
+ keys( items ),
294
+ makeMap( items ),
295
+ 6,
296
+ makeBounds( { fill: { minWidth: 3, maxWidth: 6 } } )
297
+ );
298
+ expect( result.get( 'fill' ) ).toBe( 6 );
299
+ } );
300
+
301
+ it( 'scans for a free run wide enough for the floor', () => {
302
+ // The row-1 run beside the tall tile is nine columns; a
303
+ // twelve-column floor sends the fill to the next empty row.
304
+ const items: DashboardGridLayoutItem[] = [
305
+ { key: 'tall', width: 3, height: 2 },
306
+ { key: 'header', width: 9, height: 1 },
307
+ { key: 'fill', width: 'fill', height: 1 },
308
+ ];
309
+ const result = resolveFillWidths(
310
+ keys( items ),
311
+ makeMap( items ),
312
+ 12,
313
+ makeBounds( { fill: { minWidth: 12, maxWidth: 12 } } )
314
+ );
315
+ expect( result.get( 'fill' ) ).toBe( 12 );
316
+ } );
317
+
318
+ it( 'reserves trailing items against the floored run', () => {
319
+ const items: DashboardGridLayoutItem[] = [
320
+ { key: 'tall', width: 3, height: 2 },
321
+ { key: 'header', width: 9, height: 1 },
322
+ { key: 'fill', width: 'fill', height: 1 },
323
+ { key: 'trailing', width: 3, height: 1 },
324
+ ];
325
+ const result = resolveFillWidths(
326
+ keys( items ),
327
+ makeMap( items ),
328
+ 12,
329
+ makeBounds( { fill: { minWidth: 6, maxWidth: 12 } } )
330
+ );
331
+ expect( result.get( 'fill' ) ).toBe( 6 );
332
+ } );
333
+
334
+ it( 'caps the fill inside a free run', () => {
335
+ const items: DashboardGridLayoutItem[] = [
336
+ { key: 'tall', width: 3, height: 2 },
337
+ { key: 'header', width: 9, height: 1 },
338
+ { key: 'fill', width: 'fill', height: 1 },
339
+ ];
340
+ const result = resolveFillWidths(
341
+ keys( items ),
342
+ makeMap( items ),
343
+ 12,
344
+ makeBounds( { fill: { minWidth: 1, maxWidth: 6 } } )
345
+ );
346
+ expect( result.get( 'fill' ) ).toBe( 6 );
347
+ } );
348
+
349
+ it( 'places a capped full item like a fixed item of its capped width', () => {
350
+ const items: DashboardGridLayoutItem[] = [
351
+ { key: 'full', width: 'full' },
352
+ { key: 'fill', width: 'fill' },
353
+ ];
354
+ const result = resolveFillWidths(
355
+ keys( items ),
356
+ makeMap( items ),
357
+ 12,
358
+ makeBounds( { full: { minWidth: 1, maxWidth: 4 } } )
359
+ );
360
+ expect( result.get( 'fill' ) ).toBe( 8 );
361
+ } );
362
+
363
+ it( 'reserves a capped full item in the fill look-ahead', () => {
364
+ const items: DashboardGridLayoutItem[] = [
365
+ { key: 'fill', width: 'fill' },
366
+ { key: 'full', width: 'full' },
367
+ ];
368
+ const result = resolveFillWidths(
369
+ keys( items ),
370
+ makeMap( items ),
371
+ 12,
372
+ makeBounds( { full: { minWidth: 1, maxWidth: 4 } } )
373
+ );
374
+ expect( result.get( 'fill' ) ).toBe( 8 );
375
+ } );
376
+
377
+ it( 'places a capped full item beside a tall tile', () => {
378
+ const items: DashboardGridLayoutItem[] = [
379
+ { key: 'tall', width: 3, height: 2 },
380
+ { key: 'full', width: 'full', height: 1 },
381
+ { key: 'fill', width: 'fill', height: 1 },
382
+ ];
383
+ const result = resolveFillWidths(
384
+ keys( items ),
385
+ makeMap( items ),
386
+ 12,
387
+ makeBounds( { full: { minWidth: 1, maxWidth: 4 } } )
388
+ );
389
+ expect( result.get( 'fill' ) ).toBe( 5 );
390
+ } );
391
+
392
+ it( 'keeps a full item at the column count when its cap allows it', () => {
393
+ const items: DashboardGridLayoutItem[] = [
394
+ { key: 'full', width: 'full' },
395
+ { key: 'fill', width: 'fill' },
396
+ ];
397
+ const result = resolveFillWidths(
398
+ keys( items ),
399
+ makeMap( items ),
400
+ 12,
401
+ makeBounds( { full: { minWidth: 1, maxWidth: 12 } } )
402
+ );
403
+ expect( result.get( 'fill' ) ).toBe( 12 );
404
+ } );
405
+ } );
247
406
  } );
@@ -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
  }