Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/widget-dashboard/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
registered type be inserted
([#81967](https://github.com/WordPress/gutenberg/pull/81967)).

### Enhancements

- `gridSettings.columns` now lowers the column cap on wide containers, from
`1` to `WIDGET_DASHBOARD_COLUMN_COUNT`; the responsive steps scale from
the cap. Defaults are unchanged: `DEFAULT_GRID` keeps four columns
([#82204](https://github.com/WordPress/gutenberg/pull/82204)).

### Internal

- Remove unused dependency `@wordpress/primitives` ([#82103](https://github.com/WordPress/gutenberg/pull/82103)).
Expand Down
2 changes: 1 addition & 1 deletion packages/widget-dashboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ The exported kit for handling them:
- `DEFAULT_GRID` — canonical default settings, applied when `gridSettings` is omitted.
- `normalizeGridSettings( settings, defaultRowHeight )` — coerces legacy freeform row heights to the nearest preset. Run it over stored payloads before passing them in.
- `ROW_HEIGHT_PRESETS` / `DEFAULT_ROW_HEIGHT` — the row-height presets (`small`, `medium`, `large`) that `rowHeight` values normalize to.
- `WIDGET_DASHBOARD_COLUMN_COUNT` — maximum column count on wide containers. The effective count steps down from container width; persisted `columns` values are ignored.
- `WIDGET_DASHBOARD_COLUMN_COUNT` — default and maximum column count on wide containers. `gridSettings.columns` lowers it (`1` to the constant; anything else resolves to the constant). The effective count steps down from container width: the cap at `960px` and above, two below that, one below `600px`.
Comment thread
Nikschavan marked this conversation as resolved.
Resolved

```tsx
<WidgetDashboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ export const Widgets = forwardRef< HTMLDivElement, WidgetsProps >(
widgetTypes,
canPerform,
} = useDashboardInternalContext();
const { containerRef, columnCount } =
useDashboardContainerColumnCount( ref );
const { containerRef, columnCount } = useDashboardContainerColumnCount(
ref,
gridSettings.columns
);
const isMasonry = gridSettings.model === 'masonry';

const permissionsFor = useCallback< TilePermissionsFor >(
Expand Down
4 changes: 2 additions & 2 deletions packages/widget-dashboard/src/context/dashboard-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import type {
} from '@wordpress/widget-primitives';
import { DEFAULT_GRID } from '../utils/default-grid';
import { normalizeGridSettings } from '../utils/normalize-grid-settings';
import { resolveDashboardColumnCap } from '../utils/resolve-dashboard-column-count/resolve-dashboard-column-count';
import { DEFAULT_ROW_HEIGHT } from '../utils/row-height-presets';
import { useDashboardPolicy } from '../components/dashboard-policy';
import type {
CanPerformDashboardOperation,
WidgetGridSettings,
DashboardWidget,
} from '../types';
import { WIDGET_DASHBOARD_COLUMN_COUNT } from '../types';

type GridSettingsWithColumns = WidgetGridSettings & { columns: number };

Expand All @@ -32,7 +32,7 @@ function resolveGridSettings(
const normalized = normalizeGridSettings( settings, DEFAULT_ROW_HEIGHT );
return {
...normalized,
columns: WIDGET_DASHBOARD_COLUMN_COUNT,
columns: resolveDashboardColumnCap( normalized.columns ),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ interface UseDashboardContainerColumnCountResult {

/**
* Tracks the dashboard grid container width and maps it to an opinionated
* column count (4 → 2 → 1). Measurement is container-based via
* column count (cap → 2 → 1). Measurement is container-based via
* `ResizeObserver`, not viewport media queries.
*
* @param {Ref< HTMLDivElement >} [forwardedRef] Ref forwarded from the grid wrapper.
* @param {number} [maxColumns] Column cap for wide containers.
*/
export function useDashboardContainerColumnCount(
forwardedRef?: Ref< HTMLDivElement >
forwardedRef?: Ref< HTMLDivElement >,
maxColumns?: number
): UseDashboardContainerColumnCountResult {
const [ container, setContainer ] = useState< HTMLDivElement | null >(
null
Expand Down Expand Up @@ -48,8 +50,8 @@ export function useDashboardContainerColumnCount(
}, [ container ] );

const columnCount = useMemo(
() => resolveDashboardColumnCount( containerWidth ),
[ containerWidth ]
() => resolveDashboardColumnCount( containerWidth, maxColumns ),
[ containerWidth, maxColumns ]
);

return { containerRef, columnCount };
Expand Down
21 changes: 12 additions & 9 deletions packages/widget-dashboard/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,19 @@ export interface WidgetContextValue {
export type WidgetGridModel = 'grid' | 'masonry';

/**
* Maximum column count for the widget dashboard on wide containers.
* Not user-configurable; container width steps the count down to two
* and one column at fixed breakpoints.
* Default and maximum column count for the widget dashboard on wide
* containers. A host may lower it through `gridSettings.columns`; container
* width then steps the count down to two and one column at fixed
* breakpoints. Not a user-facing setting.
Comment thread
Nikschavan marked this conversation as resolved.
Resolved
*/
export const WIDGET_DASHBOARD_COLUMN_COUNT = 4;

/**
* Settings common to every grid model. Column count is resolved from
* the dashboard container width (see
* `utils/resolve-dashboard-column-count`). `columns` and `minColumnWidth`
* on this type remain for persisted payloads and `@wordpress/grid`
* compatibility; the dashboard ignores user-facing values for both.
* the host's cap and the dashboard container width (see
* `utils/resolve-dashboard-column-count`). `minColumnWidth` remains for
* persisted payloads and `@wordpress/grid` compatibility; the dashboard
* ignores it.
*
* `spacing` is intentionally absent: the gap between tiles is
* presentational and lives with the design-system theme/density, not
Expand All @@ -132,8 +133,10 @@ export const WIDGET_DASHBOARD_COLUMN_COUNT = 4;
*/
interface BaseWidgetGridSettings {
/**
* Target column count (cap). The dashboard always uses
* {@link WIDGET_DASHBOARD_COLUMN_COUNT}; persisted values are ignored.
* Column cap on wide containers, `1` to
* {@link WIDGET_DASHBOARD_COLUMN_COUNT}. Values outside that range, or
* absent, resolve to the constant. Container width steps the effective
* count down from the cap.
*/
columns?: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,56 @@ import { WIDGET_DASHBOARD_COLUMN_COUNT } from '../../types';
export const WIDGET_DASHBOARD_CONTAINER_BREAKPOINT_ONE_COLUMN = 600;

/**
* Container width (px) below which the dashboard drops from four columns
* to two. Above this threshold (and below the four-column cap) the
* grid uses {@link WIDGET_DASHBOARD_COLUMN_COUNT}.
* Container width (px) below which the dashboard drops from the column
* cap to two. Above this threshold the grid uses the cap itself.
*/
export const WIDGET_DASHBOARD_CONTAINER_BREAKPOINT_TWO_COLUMNS = 960;

/**
* Resolves the column cap a host asked for. The cap can only lower the
* count: anything above {@link WIDGET_DASHBOARD_COLUMN_COUNT}, below one,
* or not a finite number falls back to the package default.
*
* @param columns Host-provided `gridSettings.columns`, if any.
* @return Column cap for wide containers.
*/
export function resolveDashboardColumnCap( columns?: number ): number {
if ( typeof columns !== 'number' || ! Number.isFinite( columns ) ) {
return WIDGET_DASHBOARD_COLUMN_COUNT;
}

return Math.min(
WIDGET_DASHBOARD_COLUMN_COUNT,
Math.max( 1, Math.floor( columns ) )
);
}

/**
* Resolves the dashboard grid column count from the widget surface
* container width. Uses discrete steps (4 → 2 → 1), not viewport
* container width. Uses discrete steps (cap → 2 → 1), not viewport
* media queries.
*
* @param containerWidth Measured inline size of the dashboard grid container.
* @param maxColumns Column cap for wide containers; see
* {@link resolveDashboardColumnCap}.
* @return Column count for {@link @wordpress/grid} surfaces.
*/
export function resolveDashboardColumnCount( containerWidth: number ): number {
export function resolveDashboardColumnCount(
containerWidth: number,
maxColumns: number = WIDGET_DASHBOARD_COLUMN_COUNT
): number {
if ( containerWidth <= 0 ) {
return WIDGET_DASHBOARD_COLUMN_COUNT;
return maxColumns;
}

if ( containerWidth < WIDGET_DASHBOARD_CONTAINER_BREAKPOINT_ONE_COLUMN ) {
return 1;
}

if ( containerWidth < WIDGET_DASHBOARD_CONTAINER_BREAKPOINT_TWO_COLUMNS ) {
return 2;
// A cap of one must not step up to two in the middle band.
return Math.min( 2, maxColumns );
}

return WIDGET_DASHBOARD_COLUMN_COUNT;
return maxColumns;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
import {
resolveDashboardColumnCap,
resolveDashboardColumnCount,
WIDGET_DASHBOARD_CONTAINER_BREAKPOINT_ONE_COLUMN,
WIDGET_DASHBOARD_CONTAINER_BREAKPOINT_TWO_COLUMNS,
} from '../resolve-dashboard-column-count';
import { WIDGET_DASHBOARD_COLUMN_COUNT } from '../../../types';

describe( 'resolveDashboardColumnCap', () => {
it( 'falls back to the package default when the host sets nothing', () => {
expect( resolveDashboardColumnCap( undefined ) ).toBe(
WIDGET_DASHBOARD_COLUMN_COUNT
);
} );

it( 'keeps a cap within range', () => {
expect( resolveDashboardColumnCap( 3 ) ).toBe( 3 );
expect( resolveDashboardColumnCap( 1 ) ).toBe( 1 );
expect(
resolveDashboardColumnCap( WIDGET_DASHBOARD_COLUMN_COUNT )
).toBe( WIDGET_DASHBOARD_COLUMN_COUNT );
} );

it( 'never raises the count above the package default', () => {
expect(
resolveDashboardColumnCap( WIDGET_DASHBOARD_COLUMN_COUNT + 2 )
).toBe( WIDGET_DASHBOARD_COLUMN_COUNT );
} );

it( 'never drops below one column', () => {
expect( resolveDashboardColumnCap( 0 ) ).toBe( 1 );
expect( resolveDashboardColumnCap( -3 ) ).toBe( 1 );
} );

it( 'floors a fractional cap', () => {
expect( resolveDashboardColumnCap( 2.9 ) ).toBe( 2 );
} );

it( 'treats a non-numeric cap as unset', () => {
expect( resolveDashboardColumnCap( Number.NaN ) ).toBe(
WIDGET_DASHBOARD_COLUMN_COUNT
);
expect( resolveDashboardColumnCap( Number.POSITIVE_INFINITY ) ).toBe(
WIDGET_DASHBOARD_COLUMN_COUNT
);
} );
} );

describe( 'resolveDashboardColumnCount', () => {
it( 'defaults to four columns before measurement', () => {
Expand Down Expand Up @@ -38,4 +80,52 @@ describe( 'resolveDashboardColumnCount', () => {
)
).toBe( 1 );
} );

describe( 'with a host cap', () => {
it( 'uses the cap before measurement and on wide containers', () => {
expect( resolveDashboardColumnCount( 0, 3 ) ).toBe( 3 );
expect(
resolveDashboardColumnCount(
WIDGET_DASHBOARD_CONTAINER_BREAKPOINT_TWO_COLUMNS,
3
)
).toBe( 3 );
expect( resolveDashboardColumnCount( 1200, 3 ) ).toBe( 3 );
} );

it( 'keeps the two- and one-column steps below the cap', () => {
expect(
resolveDashboardColumnCount(
WIDGET_DASHBOARD_CONTAINER_BREAKPOINT_TWO_COLUMNS - 1,
3
)
).toBe( 2 );
expect(
resolveDashboardColumnCount(
WIDGET_DASHBOARD_CONTAINER_BREAKPOINT_ONE_COLUMN - 1,
3
)
).toBe( 1 );
} );

it( 'never steps above a cap of one in the middle band', () => {
expect(
resolveDashboardColumnCount(
WIDGET_DASHBOARD_CONTAINER_BREAKPOINT_TWO_COLUMNS - 1,
1
)
).toBe( 1 );
expect( resolveDashboardColumnCount( 1200, 1 ) ).toBe( 1 );
} );

it( 'keeps a cap of two flat across the middle and wide bands', () => {
expect(
resolveDashboardColumnCount(
WIDGET_DASHBOARD_CONTAINER_BREAKPOINT_TWO_COLUMNS - 1,
2
)
).toBe( 2 );
expect( resolveDashboardColumnCount( 1200, 2 ) ).toBe( 2 );
} );
} );
} );
Loading