diff --git a/packages/block-editor/src/components/inspector-controls-tabs/position-controls-panel.js b/packages/block-editor/src/components/inspector-controls-tabs/position-controls-panel.js index 42a8597227dee9..457816f72337d5 100644 --- a/packages/block-editor/src/components/inspector-controls-tabs/position-controls-panel.js +++ b/packages/block-editor/src/components/inspector-controls-tabs/position-controls-panel.js @@ -2,11 +2,11 @@ * WordPress dependencies */ import { - PanelBody, __experimentalUseSlotFills as useSlotFills, + __experimentalToolsPanel as ToolsPanel, + __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; -import { useSelect } from '@wordpress/data'; -import { useLayoutEffect, useState } from '@wordpress/element'; +import { useDispatch, useSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; /** @@ -17,38 +17,49 @@ import { default as InspectorControls } from '../inspector-controls'; import { store as blockEditorStore } from '../../store'; const PositionControlsPanel = () => { - const [ initialOpen, setInitialOpen ] = useState(); - // Determine whether the panel should be expanded. - const { multiSelectedBlocks } = useSelect( ( select ) => { - const { getBlocksByClientId, getSelectedBlockClientIds } = + const { selectedClientID, positionAttribute } = useSelect( ( select ) => { + const { getSelectedBlockClientIds, getBlockAttributes } = select( blockEditorStore ); + const clientIds = getSelectedBlockClientIds(); + + // If multiple blocks are selected, prioritize the first block. + const blockAttributes = getBlockAttributes( clientIds[ 0 ] ); + return { - multiSelectedBlocks: getBlocksByClientId( clientIds ), + selectedClientID: clientIds[ 0 ], + positionAttribute: blockAttributes?.style?.position?.type, }; }, [] ); - useLayoutEffect( () => { - // If any selected block has a position set, open the panel by default. - // The first block's value will still be used within the control though. - if ( initialOpen === undefined ) { - setInitialOpen( - multiSelectedBlocks.some( - ( { attributes } ) => !! attributes?.style?.position?.type - ) - ); - } - }, [ initialOpen, multiSelectedBlocks, setInitialOpen ] ); + const { updateBlockAttributes } = useDispatch( blockEditorStore ); + + function resetPosition() { + updateBlockAttributes( selectedClientID, { + style: { + position: { + type: undefined, + }, + }, + } ); + } return ( - - - + !! positionAttribute } + onDeselect={ resetPosition } + > + + + ); };