From eae2fa9d1f5cb81c0939f8e6875d76a7e60f5423 Mon Sep 17 00:00:00 2001 From: Mikael Korpela Date: Tue, 19 May 2026 10:00:03 +0300 Subject: [PATCH 1/8] Dashboard: add "customize" command palette command --- routes/dashboard/package.json | 1 + .../dashboard-commands/dashboard-commands.tsx | 51 +++++++++++ .../components/dashboard-commands/index.ts | 4 + .../test/dashboard-commands.test.tsx | 84 +++++++++++++++++++ .../widget-dashboard/widget-dashboard.tsx | 2 + 5 files changed, 142 insertions(+) create mode 100644 routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx create mode 100644 routes/dashboard/widget-dashboard/components/dashboard-commands/index.ts create mode 100644 routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx diff --git a/routes/dashboard/package.json b/routes/dashboard/package.json index 2b9c4132cfbcf1..22de7aaf85c6fe 100644 --- a/routes/dashboard/package.json +++ b/routes/dashboard/package.json @@ -13,6 +13,7 @@ "@wordpress/api-fetch": "file:../../packages/api-fetch", "@wordpress/base-styles": "file:../../packages/base-styles", "@wordpress/core-data": "file:../../packages/core-data", + "@wordpress/commands": "file:../../packages/commands", "@wordpress/components": "file:../../packages/components", "@wordpress/compose": "file:../../packages/compose", "@wordpress/data": "file:../../packages/data", diff --git a/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx b/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx new file mode 100644 index 00000000000000..0c2d2991e8754e --- /dev/null +++ b/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx @@ -0,0 +1,51 @@ +/** + * WordPress dependencies + */ +import { useCallback } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { + useCommand, + privateApis as commandsPrivateApis, +} from '@wordpress/commands'; +import { layout } from '@wordpress/icons'; + +/** + * Internal dependencies + */ +import { unlock } from '../../../lock-unlock'; +import { useDashboardInternalContext } from '../../context/dashboard-context'; + +const { useCommandContext } = unlock( commandsPrivateApis ); + +export const DASHBOARD_COMMAND_CONTEXT = 'dashboard'; + +/** + * Registers dashboard-specific command palette commands and sets the + * active command context so they surface under Suggestions by default. + */ +export function DashboardCommands() { + const { editMode, onEditChange } = useDashboardInternalContext(); + + useCommandContext( DASHBOARD_COMMAND_CONTEXT ); + + const customize = useCallback( + ( { close }: { close: () => void } ) => { + onEditChange?.( true ); + close(); + }, + [ onEditChange ] + ); + + useCommand( { + name: 'core/dashboard/customize', + label: __( 'Customize dashboard' ), + icon: layout, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [], + disabled: ! onEditChange || editMode, + callback: customize, + } ); + + return null; +} diff --git a/routes/dashboard/widget-dashboard/components/dashboard-commands/index.ts b/routes/dashboard/widget-dashboard/components/dashboard-commands/index.ts new file mode 100644 index 00000000000000..b2cd081992acb5 --- /dev/null +++ b/routes/dashboard/widget-dashboard/components/dashboard-commands/index.ts @@ -0,0 +1,4 @@ +export { + DashboardCommands, + DASHBOARD_COMMAND_CONTEXT, +} from './dashboard-commands'; diff --git a/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx b/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx new file mode 100644 index 00000000000000..e3d0f95a877fc4 --- /dev/null +++ b/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx @@ -0,0 +1,84 @@ +/** + * External dependencies + */ +import '@testing-library/jest-dom'; +import { render, screen } from '@testing-library/react'; + +/** + * WordPress dependencies + */ +import { useState } from '@wordpress/element'; +import { useSelect } from '@wordpress/data'; +import { store as commandsStore } from '@wordpress/commands'; + +/** + * Internal dependencies + */ +import { WidgetDashboard } from '../widget-dashboard'; +import { DASHBOARD_COMMAND_CONTEXT } from '../components/dashboard-commands'; +import type { DashboardWidget, WidgetType } from '../types'; + +const widgetTypes: WidgetType[] = []; + +const layout: DashboardWidget[] = [ + { uuid: 'a', type: 'core/test', placement: { width: 1, height: 1 } }, +]; + +function CommandsProbe() { + const { context, customizeCommand } = useSelect( ( select ) => { + const { getContext, getCommands } = select( commandsStore ); + const contextualCommands = getCommands( true ); + return { + context: getContext(), + customizeCommand: contextualCommands.find( + ( command ) => command.name === 'core/dashboard/customize' + ), + }; + }, [] ); + + return ( +
+ ); +} + +function Harness( { initialEditMode = false }: { initialEditMode?: boolean } ) { + const [ editMode, setEditMode ] = useState( initialEditMode ); + + return ( + {} } + widgetTypes={ widgetTypes } + editMode={ editMode } + onEditChange={ setEditMode } + > + + + ); +} + +describe( 'WidgetDashboard.DashboardCommands', () => { + it( 'sets the dashboard command context and registers Customize', () => { + render( ); + const probe = screen.getByTestId( 'commands-probe' ); + + expect( probe ).toHaveAttribute( + 'data-context', + DASHBOARD_COMMAND_CONTEXT + ); + expect( probe ).toHaveAttribute( 'data-has-customize', 'yes' ); + } ); + + it( 'unregisters Customize while edit mode is active', () => { + render( ); + + expect( screen.getByTestId( 'commands-probe' ) ).toHaveAttribute( + 'data-has-customize', + 'no' + ); + } ); +} ); diff --git a/routes/dashboard/widget-dashboard/widget-dashboard.tsx b/routes/dashboard/widget-dashboard/widget-dashboard.tsx index d741511d552004..a81f834a807e06 100644 --- a/routes/dashboard/widget-dashboard/widget-dashboard.tsx +++ b/routes/dashboard/widget-dashboard/widget-dashboard.tsx @@ -4,6 +4,7 @@ import { WidgetDashboardProvider } from './context/dashboard-context'; import { WidgetDashboardUIProvider } from './context/ui-context'; import { Actions } from './components/actions'; +import { DashboardCommands } from './components/dashboard-commands'; import { Inserter } from './components/inserter'; import { WidgetChrome } from './components/widget-chrome'; import { Widgets } from './components/widgets'; @@ -71,6 +72,7 @@ export const WidgetDashboard = Object.assign( ) } + From 7eb79a0ee12986f02b79aae9f5311f04d98ecc56 Mon Sep 17 00:00:00 2001 From: Mikael Korpela Date: Tue, 19 May 2026 10:14:16 +0300 Subject: [PATCH 2/8] Add more commands --- .../components/actions/actions.tsx | 27 +-- .../dashboard-commands/dashboard-commands.tsx | 188 ++++++++++++++++-- .../use-pending-when-edit-mode.ts | 44 ++++ .../components/dashboard-commands/utils.ts | 51 +++++ .../context/dashboard-context.tsx | 38 ++++ .../widget-dashboard/context/ui-context.tsx | 17 +- .../test/dashboard-commands.test.tsx | 137 +++++++++++-- .../grid-model-change/grid-model-change.ts | 51 +++++ .../utils/grid-model-change/index.ts | 1 + 9 files changed, 503 insertions(+), 51 deletions(-) create mode 100644 routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts create mode 100644 routes/dashboard/widget-dashboard/components/dashboard-commands/utils.ts create mode 100644 routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts create mode 100644 routes/dashboard/widget-dashboard/utils/grid-model-change/index.ts diff --git a/routes/dashboard/widget-dashboard/components/actions/actions.tsx b/routes/dashboard/widget-dashboard/components/actions/actions.tsx index a01900dd8f7315..2bfebac7a24afb 100644 --- a/routes/dashboard/widget-dashboard/components/actions/actions.tsx +++ b/routes/dashboard/widget-dashboard/components/actions/actions.tsx @@ -71,10 +71,13 @@ export function Actions(): React.ReactNode { return () => clearTimeout( exitTimeout ); }, [ editMode, isEditActionsMounted ] ); - const { setInserterOpen } = useDashboardUIContext(); - - const [ isResetDialogOpen, setIsResetDialogOpen ] = useState( false ); - const [ isLayoutSettingsOpen, setIsLayoutSettingsOpen ] = useState( false ); + const { + setInserterOpen, + layoutSettingsOpen, + setLayoutSettingsOpen, + resetDialogOpen, + setResetDialogOpen, + } = useDashboardUIContext(); const handleEditMode = useCallback( () => { onEditChange?.( ! editMode ); @@ -93,13 +96,13 @@ export function Actions(): React.ReactNode { }, [ commit ] ); const openLayoutSettings = useCallback( () => { - setIsLayoutSettingsOpen( true ); - }, [] ); + setLayoutSettingsOpen( true ); + }, [ setLayoutSettingsOpen ] ); const moreActionsItems: MoreActionsDropdownItem[] = [ { label: __( 'Reset to default' ), - onClick: () => setIsResetDialogOpen( true ), + onClick: () => setResetDialogOpen( true ), disabled: ! onLayoutReset, }, ]; @@ -171,12 +174,12 @@ export function Actions(): React.ReactNode { { await onLayoutReset?.(); onEditChange?.( false ); - setIsResetDialogOpen( false ); + setResetDialogOpen( false ); } } > ) } diff --git a/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx b/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx index 0c2d2991e8754e..8c0a6b8a26da3f 100644 --- a/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx +++ b/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx @@ -1,51 +1,207 @@ /** * WordPress dependencies */ -import { useCallback } from '@wordpress/element'; +import { useCallback, useMemo } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { - useCommand, + useCommands, privateApis as commandsPrivateApis, } from '@wordpress/commands'; -import { layout } from '@wordpress/icons'; +import { + columns, + grid, + layout as layoutIcon, + plus, + settings, + trash, +} from '@wordpress/icons'; /** * Internal dependencies */ import { unlock } from '../../../lock-unlock'; import { useDashboardInternalContext } from '../../context/dashboard-context'; +import { useDashboardUIContext } from '../../context/ui-context'; +import { getGridModel } from '../../utils/grid-model-change'; +import { usePendingWhenEditMode } from './use-pending-when-edit-mode'; const { useCommandContext } = unlock( commandsPrivateApis ); export const DASHBOARD_COMMAND_CONTEXT = 'dashboard'; +type CommandCallback = ( options: { close: () => void } ) => void; + /** * Registers dashboard-specific command palette commands and sets the * active command context so they surface under Suggestions by default. */ export function DashboardCommands() { - const { editMode, onEditChange } = useDashboardInternalContext(); + const { + editMode, + onEditChange, + onLayoutReset, + gridSettings, + canEditGridSettings, + commitGridModelChange, + } = useDashboardInternalContext(); + + const { setInserterOpen, setLayoutSettingsOpen, setResetDialogOpen } = + useDashboardUIContext(); useCommandContext( DASHBOARD_COMMAND_CONTEXT ); - const customize = useCallback( - ( { close }: { close: () => void } ) => { + const runWhenInEditMode = usePendingWhenEditMode( { + editMode, + onEditChange, + } ); + + const gridModel = getGridModel( gridSettings ); + const isGridLayout = gridModel === 'grid'; + const isMasonryLayout = gridModel === 'masonry'; + + const switchToMasonry = useCallback< CommandCallback >( + ( { close } ) => { + close(); + commitGridModelChange( 'masonry' ); + }, + [ commitGridModelChange ] + ); + + const switchToGrid = useCallback< CommandCallback >( + ( { close } ) => { + close(); + commitGridModelChange( 'grid' ); + }, + [ commitGridModelChange ] + ); + + const openLayoutSettings = useCallback< CommandCallback >( + ( { close } ) => { + close(); + setLayoutSettingsOpen( true ); + }, + [ setLayoutSettingsOpen ] + ); + + const resetToDefault = useCallback< CommandCallback >( + ( { close } ) => { + close(); + setResetDialogOpen( true ); + }, + [ setResetDialogOpen ] + ); + + const addWidgets = useCallback< CommandCallback >( + ( { close } ) => { + close(); + runWhenInEditMode( () => { + setInserterOpen( true ); + } ); + }, + [ runWhenInEditMode, setInserterOpen ] + ); + + const customize = useCallback< CommandCallback >( + ( { close } ) => { onEditChange?.( true ); close(); }, [ onEditChange ] ); - useCommand( { - name: 'core/dashboard/customize', - label: __( 'Customize dashboard' ), - icon: layout, - category: 'command', - context: DASHBOARD_COMMAND_CONTEXT, - keywords: [], - disabled: ! onEditChange || editMode, - callback: customize, - } ); + const commands = useMemo( + () => [ + { + name: 'core/dashboard/customize', + label: __( 'Customize dashboard' ), + icon: layoutIcon, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [ __( 'edit' ), __( 'widgets' ), __( 'layout' ) ], + disabled: ! onEditChange || editMode, + callback: customize, + }, + { + name: 'core/dashboard/add-widgets', + label: __( 'Add dashboard widgets' ), + icon: plus, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [ __( 'widgets' ), __( 'inserter' ) ], + disabled: ! onEditChange, + callback: addWidgets, + }, + { + name: 'core/dashboard/switch-to-masonry-layout', + label: __( 'Switch dashboard to masonry layout' ), + icon: columns, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [ + __( 'layout' ), + __( 'layout model' ), + __( 'masonry' ), + ], + disabled: ! canEditGridSettings || isMasonryLayout || editMode, + callback: switchToMasonry, + }, + { + name: 'core/dashboard/switch-to-grid-layout', + label: __( 'Switch dashboard to grid layout' ), + icon: grid, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [ + __( 'layout' ), + __( 'layout model' ), + __( 'grid' ), + __( 'standard grid' ), + ], + disabled: ! canEditGridSettings || isGridLayout || editMode, + callback: switchToGrid, + }, + { + name: 'core/dashboard/layout-settings', + label: __( 'Configure dashboard layout settings' ), + icon: settings, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [ + __( 'layout' ), + __( 'columns' ), + __( 'row height' ), + ], + disabled: ! canEditGridSettings || editMode, + callback: openLayoutSettings, + }, + { + name: 'core/dashboard/reset-to-default', + label: __( 'Reset dashboard widgets to default' ), + icon: trash, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [ __( 'reset' ), __( 'default' ) ], + disabled: ! onLayoutReset, + callback: resetToDefault, + }, + ], + [ + onEditChange, + editMode, + customize, + addWidgets, + canEditGridSettings, + isMasonryLayout, + isGridLayout, + switchToMasonry, + switchToGrid, + openLayoutSettings, + onLayoutReset, + resetToDefault, + ] + ); + + useCommands( commands ); return null; } diff --git a/routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts b/routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts new file mode 100644 index 00000000000000..ce1e2927a317b3 --- /dev/null +++ b/routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts @@ -0,0 +1,44 @@ +/** + * WordPress dependencies + */ +import { useCallback, useEffect, useRef } from '@wordpress/element'; + +/** + * Runs an action immediately when already in edit mode. Otherwise enables + * edit mode and runs the action after edit mode becomes active. + * @param root0 + * @param root0.editMode + * @param root0.onEditChange + */ +export function usePendingWhenEditMode( { + editMode, + onEditChange, +}: { + editMode: boolean; + onEditChange?: ( next: boolean ) => void; +} ) { + const pendingRef = useRef< ( () => void ) | null >( null ); + const wasEditingRef = useRef( editMode ); + + useEffect( () => { + if ( ! wasEditingRef.current && editMode && pendingRef.current ) { + const action = pendingRef.current; + pendingRef.current = null; + action(); + } + wasEditingRef.current = editMode; + }, [ editMode ] ); + + return useCallback( + ( action: () => void ) => { + if ( editMode ) { + action(); + return; + } + + pendingRef.current = action; + onEditChange?.( true ); + }, + [ editMode, onEditChange ] + ); +} diff --git a/routes/dashboard/widget-dashboard/components/dashboard-commands/utils.ts b/routes/dashboard/widget-dashboard/components/dashboard-commands/utils.ts new file mode 100644 index 00000000000000..7f051c02ad84c7 --- /dev/null +++ b/routes/dashboard/widget-dashboard/components/dashboard-commands/utils.ts @@ -0,0 +1,51 @@ +/** + * Internal dependencies + */ +import { + computeGridModelChange, + getGridModel, +} from '../../utils/grid-model-change'; +import type { + DashboardWidget, + WidgetGridModel, + WidgetGridSettings, +} from '../../types'; + +export { getGridModel, computeGridModelChange }; + +/** + * Applies a layout-model change the same way the layout settings drawer does: + * migrates widget placements, then updates the staged grid settings. + * @param root0 + * @param root0.layout + * @param root0.gridSettings + * @param root0.targetModel + * @param root0.onLayoutChange + * @param root0.onGridSettingsChange + */ +export function applyGridModelChange( { + layout, + gridSettings, + targetModel, + onLayoutChange, + onGridSettingsChange, +}: { + layout: DashboardWidget[]; + gridSettings: WidgetGridSettings; + targetModel: WidgetGridModel; + onLayoutChange: ( layout: DashboardWidget[] ) => void; + onGridSettingsChange: ( gridSettings: WidgetGridSettings ) => void; +} ): void { + const next = computeGridModelChange( { + layout, + gridSettings, + targetModel, + } ); + + if ( ! next ) { + return; + } + + onLayoutChange( next.layout ); + onGridSettingsChange( next.gridSettings ); +} diff --git a/routes/dashboard/widget-dashboard/context/dashboard-context.tsx b/routes/dashboard/widget-dashboard/context/dashboard-context.tsx index 34555afcc59dd3..3deef4b6b98efe 100644 --- a/routes/dashboard/widget-dashboard/context/dashboard-context.tsx +++ b/routes/dashboard/widget-dashboard/context/dashboard-context.tsx @@ -19,8 +19,10 @@ import { /** * Internal dependencies */ +import { computeGridModelChange } from '../utils/grid-model-change'; import type { ResolveWidgetModule, + WidgetGridModel, WidgetGridSettings, DashboardWidget, WidgetType, @@ -111,6 +113,13 @@ interface InternalDashboardContextValue { */ commit: () => void; + /** + * Switches the layout model, updates staging, and publishes + * immediately — equivalent to changing the model in layout + * settings and clicking Save. + */ + commitGridModelChange: ( targetModel: WidgetGridModel ) => void; + /** Reverts both staging slices and exits edit mode. */ cancel: () => void; @@ -284,6 +293,33 @@ export function WidgetDashboardProvider( { onEditChange?.( false ); }, [ committedLayout, committedGridSettings, onEditChange ] ); + const commitGridModelChange = useCallback( + ( targetModel: WidgetGridModel ) => { + const next = computeGridModelChange( { + layout: stagingLayout, + gridSettings: stagingGridSettings, + targetModel, + } ); + + if ( ! next ) { + return; + } + + setStagingLayout( next.layout ); + setStagingGridSettings( next.gridSettings ); + onLayoutChange( canonicalize( next.layout ) ); + onGridSettingsChange?.( next.gridSettings ); + onEditChange?.( false ); + }, + [ + stagingLayout, + stagingGridSettings, + onLayoutChange, + onGridSettingsChange, + onEditChange, + ] + ); + const resetGridSettings = useCallback( () => { setStagingGridSettings( DEFAULT_GRID ); }, [] ); @@ -312,6 +348,7 @@ export function WidgetDashboardProvider( { canEditGridSettings, resetGridSettings, commit, + commitGridModelChange, cancel, hasUncommittedChanges, editMode, @@ -326,6 +363,7 @@ export function WidgetDashboardProvider( { canEditGridSettings, resetGridSettings, commit, + commitGridModelChange, cancel, hasUncommittedChanges, editMode, diff --git a/routes/dashboard/widget-dashboard/context/ui-context.tsx b/routes/dashboard/widget-dashboard/context/ui-context.tsx index bcb1dbde67ac07..eaf8b3bbd0f970 100644 --- a/routes/dashboard/widget-dashboard/context/ui-context.tsx +++ b/routes/dashboard/widget-dashboard/context/ui-context.tsx @@ -16,6 +16,10 @@ import { interface DashboardUIContextValue { inserterOpen: boolean; setInserterOpen: ( next: boolean ) => void; + layoutSettingsOpen: boolean; + setLayoutSettingsOpen: ( next: boolean ) => void; + resetDialogOpen: boolean; + setResetDialogOpen: ( next: boolean ) => void; } const Context = createContext< DashboardUIContextValue | null >( null ); @@ -50,10 +54,19 @@ interface ProviderProps { */ export function WidgetDashboardUIProvider( { children }: ProviderProps ) { const [ inserterOpen, setInserterOpen ] = useState( false ); + const [ layoutSettingsOpen, setLayoutSettingsOpen ] = useState( false ); + const [ resetDialogOpen, setResetDialogOpen ] = useState( false ); const value = useMemo< DashboardUIContextValue >( - () => ( { inserterOpen, setInserterOpen } ), - [ inserterOpen ] + () => ( { + inserterOpen, + setInserterOpen, + layoutSettingsOpen, + setLayoutSettingsOpen, + resetDialogOpen, + setResetDialogOpen, + } ), + [ inserterOpen, layoutSettingsOpen, resetDialogOpen ] ); return { children }; diff --git a/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx b/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx index e3d0f95a877fc4..21913357a14ad6 100644 --- a/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx +++ b/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx @@ -7,7 +7,7 @@ import { render, screen } from '@testing-library/react'; /** * WordPress dependencies */ -import { useState } from '@wordpress/element'; +import { useMemo, useState } from '@wordpress/element'; import { useSelect } from '@wordpress/data'; import { store as commandsStore } from '@wordpress/commands'; @@ -24,28 +24,60 @@ const layout: DashboardWidget[] = [ { uuid: 'a', type: 'core/test', placement: { width: 1, height: 1 } }, ]; -function CommandsProbe() { - const { context, customizeCommand } = useSelect( ( select ) => { - const { getContext, getCommands } = select( commandsStore ); - const contextualCommands = getCommands( true ); - return { - context: getContext(), - customizeCommand: contextualCommands.find( - ( command ) => command.name === 'core/dashboard/customize' +function CommandsProbe( { names }: { names: string[] } ) { + const context = useSelect( + ( select ) => select( commandsStore ).getContext(), + [] + ); + const contextualCommands = useSelect( + ( select ) => select( commandsStore ).getCommands( true ), + [] + ); + + const registered = useMemo( + () => + Object.fromEntries( + names.map( ( name ) => [ + name, + contextualCommands.some( + ( command ) => command.name === name + ), + ] ) ), - }; - }, [] ); + [ names, contextualCommands ] + ); return (
); } -function Harness( { initialEditMode = false }: { initialEditMode?: boolean } ) { +const COMMAND_NAMES = [ + 'core/dashboard/customize', + 'core/dashboard/add-widgets', + 'core/dashboard/switch-to-masonry-layout', + 'core/dashboard/switch-to-grid-layout', + 'core/dashboard/layout-settings', + 'core/dashboard/reset-to-default', +]; + +interface HarnessProps { + initialEditMode?: boolean; + withGridSettings?: boolean; + withLayoutReset?: boolean; + gridModel?: 'grid' | 'masonry'; +} + +function Harness( { + initialEditMode = false, + withGridSettings = false, + withLayoutReset = false, + gridModel = 'grid', +}: HarnessProps ) { const [ editMode, setEditMode ] = useState( initialEditMode ); return ( @@ -55,30 +87,93 @@ function Harness( { initialEditMode = false }: { initialEditMode?: boolean } ) { widgetTypes={ widgetTypes } editMode={ editMode } onEditChange={ setEditMode } + onLayoutReset={ withLayoutReset ? async () => {} : undefined } + gridSettings={ + withGridSettings + ? { model: gridModel, columns: 6, minColumnWidth: 350 } + : undefined + } + onGridSettingsChange={ withGridSettings ? () => {} : undefined } > - + ); } +function getRegistered( probe: HTMLElement ): Record< string, boolean > { + return JSON.parse( probe.getAttribute( 'data-registered' ) ?? '{}' ); +} + describe( 'WidgetDashboard.DashboardCommands', () => { - it( 'sets the dashboard command context and registers Customize', () => { - render( ); + it( 'sets the dashboard command context and registers core commands', () => { + render( ); const probe = screen.getByTestId( 'commands-probe' ); + const registered = getRegistered( probe ); expect( probe ).toHaveAttribute( 'data-context', DASHBOARD_COMMAND_CONTEXT ); - expect( probe ).toHaveAttribute( 'data-has-customize', 'yes' ); + expect( registered[ 'core/dashboard/customize' ] ).toBe( true ); + expect( registered[ 'core/dashboard/add-widgets' ] ).toBe( true ); + expect( registered[ 'core/dashboard/reset-to-default' ] ).toBe( true ); } ); it( 'unregisters Customize while edit mode is active', () => { - render( ); + render( ); + const registered = getRegistered( + screen.getByTestId( 'commands-probe' ) + ); + + expect( registered[ 'core/dashboard/customize' ] ).toBe( false ); + expect( registered[ 'core/dashboard/add-widgets' ] ).toBe( true ); + } ); + + it( 'disables layout commands while edit mode is active', () => { + render( + + ); + const registered = getRegistered( + screen.getByTestId( 'commands-probe' ) + ); + + expect( registered[ 'core/dashboard/switch-to-masonry-layout' ] ).toBe( + false + ); + expect( registered[ 'core/dashboard/switch-to-grid-layout' ] ).toBe( + false + ); + expect( registered[ 'core/dashboard/layout-settings' ] ).toBe( false ); + } ); + + it( 'only registers the active layout-model switch command', () => { + render( ); + const registered = getRegistered( + screen.getByTestId( 'commands-probe' ) + ); + + expect( registered[ 'core/dashboard/switch-to-masonry-layout' ] ).toBe( + true + ); + expect( registered[ 'core/dashboard/switch-to-grid-layout' ] ).toBe( + false + ); + } ); + + it( 'omits grid-settings commands when grid settings are not editable', () => { + render( ); + const registered = getRegistered( + screen.getByTestId( 'commands-probe' ) + ); - expect( screen.getByTestId( 'commands-probe' ) ).toHaveAttribute( - 'data-has-customize', - 'no' + expect( registered[ 'core/dashboard/switch-to-masonry-layout' ] ).toBe( + false ); + expect( registered[ 'core/dashboard/layout-settings' ] ).toBe( false ); } ); } ); diff --git a/routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts b/routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts new file mode 100644 index 00000000000000..cc2ee8ca24906c --- /dev/null +++ b/routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts @@ -0,0 +1,51 @@ +/** + * Internal dependencies + */ +import { migrateLayout } from '../migrate-layout'; +import type { + DashboardWidget, + WidgetGridModel, + WidgetGridSettings, +} from '../../types'; + +const DEFAULT_FIXED_COLUMNS = 6; + +export function getGridModel( settings: WidgetGridSettings ): WidgetGridModel { + return settings.model ?? 'grid'; +} + +/** + * Computes the staged layout and grid settings after a layout-model change. + * @param root0 + * @param root0.layout + * @param root0.gridSettings + * @param root0.targetModel + */ +export function computeGridModelChange( { + layout, + gridSettings, + targetModel, +}: { + layout: DashboardWidget[]; + gridSettings: WidgetGridSettings; + targetModel: WidgetGridModel; +} ): { + layout: DashboardWidget[]; + gridSettings: WidgetGridSettings; +} | null { + const currentModel = getGridModel( gridSettings ); + + if ( currentModel === targetModel ) { + return null; + } + + return { + layout: migrateLayout( layout, currentModel, targetModel, { + columns: gridSettings.columns ?? DEFAULT_FIXED_COLUMNS, + } ), + gridSettings: { + ...gridSettings, + model: targetModel, + } as WidgetGridSettings, + }; +} diff --git a/routes/dashboard/widget-dashboard/utils/grid-model-change/index.ts b/routes/dashboard/widget-dashboard/utils/grid-model-change/index.ts new file mode 100644 index 00000000000000..efb90fab583b64 --- /dev/null +++ b/routes/dashboard/widget-dashboard/utils/grid-model-change/index.ts @@ -0,0 +1 @@ +export { computeGridModelChange, getGridModel } from './grid-model-change'; From 4687d6e04c3212f817458e29107f016304cb2205 Mon Sep 17 00:00:00 2001 From: Mikael Korpela Date: Tue, 19 May 2026 11:33:39 +0300 Subject: [PATCH 3/8] Update package-lock.json --- package-lock.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package-lock.json b/package-lock.json index 855e6fd9c1e47c..2560102de2c73c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -64324,6 +64324,7 @@ "@wordpress/admin-ui": "file:../../packages/admin-ui", "@wordpress/api-fetch": "file:../../packages/api-fetch", "@wordpress/base-styles": "file:../../packages/base-styles", + "@wordpress/commands": "file:../../packages/commands", "@wordpress/components": "file:../../packages/components", "@wordpress/compose": "file:../../packages/compose", "@wordpress/core-data": "file:../../packages/core-data", From c597f11bd6918831330d50935a24e7362294dc90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Wed, 20 May 2026 14:18:42 +0100 Subject: [PATCH 4/8] Update routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts --- .../use-pending-when-edit-mode.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts b/routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts index ce1e2927a317b3..58e9aa0d73dfcd 100644 --- a/routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts +++ b/routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts @@ -3,20 +3,24 @@ */ import { useCallback, useEffect, useRef } from '@wordpress/element'; +type usePendingWhenEditModeProps = { + editMode: boolean; + onEditChange?: ( next: boolean ) => void; +}; + +type usePendingWhenEditModeResult = ( action: () => void ) => void; + /** * Runs an action immediately when already in edit mode. Otherwise enables * edit mode and runs the action after edit mode becomes active. - * @param root0 - * @param root0.editMode - * @param root0.onEditChange + * + * @param {usePendingWhenEditModeProps} params - The parameters for the function. + * @return {usePendingWhenEditModeResult} The function result. */ export function usePendingWhenEditMode( { editMode, onEditChange, -}: { - editMode: boolean; - onEditChange?: ( next: boolean ) => void; -} ) { +}: usePendingWhenEditModeProps ): usePendingWhenEditModeResult { const pendingRef = useRef< ( () => void ) | null >( null ); const wasEditingRef = useRef( editMode ); From 7b29b7f39b937b430c2cd89a896b2572705cb219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Wed, 20 May 2026 14:18:54 +0100 Subject: [PATCH 5/8] Update routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts --- .../grid-model-change/grid-model-change.ts | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts b/routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts index cc2ee8ca24906c..3ba7a59512083f 100644 --- a/routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts +++ b/routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts @@ -14,25 +14,27 @@ export function getGridModel( settings: WidgetGridSettings ): WidgetGridModel { return settings.model ?? 'grid'; } +type computeGridModelChangeProps = { + layout: DashboardWidget[]; + gridSettings: WidgetGridSettings; + targetModel: WidgetGridModel; +}; + +type computeGridModelChangeResult = { + layout: DashboardWidget[]; + gridSettings: WidgetGridSettings; +} | null; + /** * Computes the staged layout and grid settings after a layout-model change. - * @param root0 - * @param root0.layout - * @param root0.gridSettings - * @param root0.targetModel + * @param {computeGridModelChangeProps} params - The parameters for the function. + * @return {computeGridModelChangeResult} The function result. */ export function computeGridModelChange( { layout, gridSettings, targetModel, -}: { - layout: DashboardWidget[]; - gridSettings: WidgetGridSettings; - targetModel: WidgetGridModel; -} ): { - layout: DashboardWidget[]; - gridSettings: WidgetGridSettings; -} | null { +}: computeGridModelChangeProps ): computeGridModelChangeResult { const currentModel = getGridModel( gridSettings ); if ( currentModel === targetModel ) { From 2fd4eb0886ce043905bd038b8e12a6ed1fdbc6dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Sat, 23 May 2026 15:54:55 +0100 Subject: [PATCH 6/8] remove unused dashboard-commands utils file --- .../components/dashboard-commands/utils.ts | 51 ------------------- 1 file changed, 51 deletions(-) delete mode 100644 routes/dashboard/widget-dashboard/components/dashboard-commands/utils.ts diff --git a/routes/dashboard/widget-dashboard/components/dashboard-commands/utils.ts b/routes/dashboard/widget-dashboard/components/dashboard-commands/utils.ts deleted file mode 100644 index 7f051c02ad84c7..00000000000000 --- a/routes/dashboard/widget-dashboard/components/dashboard-commands/utils.ts +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Internal dependencies - */ -import { - computeGridModelChange, - getGridModel, -} from '../../utils/grid-model-change'; -import type { - DashboardWidget, - WidgetGridModel, - WidgetGridSettings, -} from '../../types'; - -export { getGridModel, computeGridModelChange }; - -/** - * Applies a layout-model change the same way the layout settings drawer does: - * migrates widget placements, then updates the staged grid settings. - * @param root0 - * @param root0.layout - * @param root0.gridSettings - * @param root0.targetModel - * @param root0.onLayoutChange - * @param root0.onGridSettingsChange - */ -export function applyGridModelChange( { - layout, - gridSettings, - targetModel, - onLayoutChange, - onGridSettingsChange, -}: { - layout: DashboardWidget[]; - gridSettings: WidgetGridSettings; - targetModel: WidgetGridModel; - onLayoutChange: ( layout: DashboardWidget[] ) => void; - onGridSettingsChange: ( gridSettings: WidgetGridSettings ) => void; -} ): void { - const next = computeGridModelChange( { - layout, - gridSettings, - targetModel, - } ); - - if ( ! next ) { - return; - } - - onLayoutChange( next.layout ); - onGridSettingsChange( next.gridSettings ); -} From b7491bf4937b860e4161d1dc9f2ef82be3ad8cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Sat, 23 May 2026 15:55:02 +0100 Subject: [PATCH 7/8] remove layout settings command from palette unify customize callback to close() before acting --- .../dashboard-commands/dashboard-commands.tsx | 29 ++----------------- .../test/dashboard-commands.test.tsx | 3 -- 2 files changed, 2 insertions(+), 30 deletions(-) diff --git a/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx b/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx index 8c0a6b8a26da3f..9ee5f0d5c40809 100644 --- a/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx +++ b/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx @@ -12,7 +12,6 @@ import { grid, layout as layoutIcon, plus, - settings, trash, } from '@wordpress/icons'; @@ -45,8 +44,7 @@ export function DashboardCommands() { commitGridModelChange, } = useDashboardInternalContext(); - const { setInserterOpen, setLayoutSettingsOpen, setResetDialogOpen } = - useDashboardUIContext(); + const { setInserterOpen, setResetDialogOpen } = useDashboardUIContext(); useCommandContext( DASHBOARD_COMMAND_CONTEXT ); @@ -75,14 +73,6 @@ export function DashboardCommands() { [ commitGridModelChange ] ); - const openLayoutSettings = useCallback< CommandCallback >( - ( { close } ) => { - close(); - setLayoutSettingsOpen( true ); - }, - [ setLayoutSettingsOpen ] - ); - const resetToDefault = useCallback< CommandCallback >( ( { close } ) => { close(); @@ -103,8 +93,8 @@ export function DashboardCommands() { const customize = useCallback< CommandCallback >( ( { close } ) => { - onEditChange?.( true ); close(); + onEditChange?.( true ); }, [ onEditChange ] ); @@ -160,20 +150,6 @@ export function DashboardCommands() { disabled: ! canEditGridSettings || isGridLayout || editMode, callback: switchToGrid, }, - { - name: 'core/dashboard/layout-settings', - label: __( 'Configure dashboard layout settings' ), - icon: settings, - category: 'command', - context: DASHBOARD_COMMAND_CONTEXT, - keywords: [ - __( 'layout' ), - __( 'columns' ), - __( 'row height' ), - ], - disabled: ! canEditGridSettings || editMode, - callback: openLayoutSettings, - }, { name: 'core/dashboard/reset-to-default', label: __( 'Reset dashboard widgets to default' ), @@ -195,7 +171,6 @@ export function DashboardCommands() { isGridLayout, switchToMasonry, switchToGrid, - openLayoutSettings, onLayoutReset, resetToDefault, ] diff --git a/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx b/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx index 21913357a14ad6..4c347d7b0ae4b4 100644 --- a/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx +++ b/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx @@ -61,7 +61,6 @@ const COMMAND_NAMES = [ 'core/dashboard/add-widgets', 'core/dashboard/switch-to-masonry-layout', 'core/dashboard/switch-to-grid-layout', - 'core/dashboard/layout-settings', 'core/dashboard/reset-to-default', ]; @@ -148,7 +147,6 @@ describe( 'WidgetDashboard.DashboardCommands', () => { expect( registered[ 'core/dashboard/switch-to-grid-layout' ] ).toBe( false ); - expect( registered[ 'core/dashboard/layout-settings' ] ).toBe( false ); } ); it( 'only registers the active layout-model switch command', () => { @@ -174,6 +172,5 @@ describe( 'WidgetDashboard.DashboardCommands', () => { expect( registered[ 'core/dashboard/switch-to-masonry-layout' ] ).toBe( false ); - expect( registered[ 'core/dashboard/layout-settings' ] ).toBe( false ); } ); } ); From 3354ca0492267976f0d9897d416a6ad664fdcf68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Sat, 23 May 2026 20:29:45 +0100 Subject: [PATCH 8/8] add Actions to WidgetDashboard JSDoc example command-palette reset opens the dialog Actions mounts --- routes/dashboard/widget-dashboard/widget-dashboard.tsx | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/routes/dashboard/widget-dashboard/widget-dashboard.tsx b/routes/dashboard/widget-dashboard/widget-dashboard.tsx index b8b6766f010b42..12a8b71fab4bb3 100644 --- a/routes/dashboard/widget-dashboard/widget-dashboard.tsx +++ b/routes/dashboard/widget-dashboard/widget-dashboard.tsx @@ -24,20 +24,27 @@ import { NoWidgetsState } from './components/no-widgets-state'; * * function MyDashboard() { * const [ layout, setLayout ] = useState( defaultLayout ); + * const [ editMode, setEditMode ] = useState( false ); * return ( * * *

No widgets yet.

*
+ * * *
* ); * } * ``` + * + * `Actions` hosts the reset dialog, so include it (or rely on the default + * children) for the command-palette "Reset to default" action to work. */ export const WidgetDashboard = Object.assign( function WidgetDashboard( {