Skip to content

Commit

Permalink
Position: Refactor "Position" controls panel to use ToolsPanel inst…
Browse files Browse the repository at this point in the history
…ead of `PanelBody` (WordPress#67967)

* Refactor "Position" controls panel to use `ToolsPanel` instead of `PanelBody`

* Refactor imports

* Refactor `position-controls-panel` to reset multiple selected blocks

* Refactor `PositionControlsPanel` to simplify block attribute retrieval

* Refactor: Simplify the logic

* Refactor `PositionControlsPanel` to improve block attribute handling and reset logic

* Refactor `PositionControlsPanel` to clean up empty style properties for selected blocks

Co-authored-by: yogeshbhutkar <[email protected]>
Co-authored-by: fabiankaegy <[email protected]>
Co-authored-by: t-hamano <[email protected]>
  • Loading branch information
4 people authored Jan 21, 2025
1 parent 1295878 commit 34f09ca
Showing 1 changed file with 62 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -15,40 +15,75 @@ import { __ } from '@wordpress/i18n';
import InspectorControlsGroups from '../inspector-controls/groups';
import { default as InspectorControls } from '../inspector-controls';
import { store as blockEditorStore } from '../../store';
import { useToolsPanelDropdownMenuProps } from '../global-styles/utils';
import { cleanEmptyObject } from '../../hooks/utils';

const PositionControlsPanel = () => {
const [ initialOpen, setInitialOpen ] = useState();
const { selectedClientIds, selectedBlocks, hasPositionAttribute } =
useSelect( ( select ) => {
const { getBlocksByClientId, getSelectedBlockClientIds } =
select( blockEditorStore );

// Determine whether the panel should be expanded.
const { multiSelectedBlocks } = useSelect( ( select ) => {
const { getBlocksByClientId, getSelectedBlockClientIds } =
select( blockEditorStore );
const clientIds = getSelectedBlockClientIds();
return {
multiSelectedBlocks: getBlocksByClientId( clientIds ),
};
}, [] );
const selectedBlockClientIds = getSelectedBlockClientIds();
const _selectedBlocks = getBlocksByClientId(
selectedBlockClientIds
);

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(
return {
selectedClientIds: selectedBlockClientIds,
selectedBlocks: _selectedBlocks,
hasPositionAttribute: _selectedBlocks?.some(
( { attributes } ) => !! attributes?.style?.position?.type
)
);
),
};
}, [] );

const { updateBlockAttributes } = useDispatch( blockEditorStore );
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

function resetPosition() {
if ( ! selectedClientIds?.length || ! selectedBlocks?.length ) {
return;
}
}, [ initialOpen, multiSelectedBlocks, setInitialOpen ] );

const attributesByClientId = Object.fromEntries(
selectedBlocks?.map( ( { clientId, attributes } ) => [
clientId,
{
style: cleanEmptyObject( {
...attributes?.style,
position: {
...attributes?.style?.position,
type: undefined,
top: undefined,
right: undefined,
bottom: undefined,
left: undefined,
},
} ),
},
] )
);

updateBlockAttributes( selectedClientIds, attributesByClientId, true );
}

return (
<PanelBody
<ToolsPanel
className="block-editor-block-inspector__position"
title={ __( 'Position' ) }
initialOpen={ initialOpen ?? false }
label={ __( 'Position' ) }
resetAll={ resetPosition }
dropdownMenuProps={ dropdownMenuProps }
>
<InspectorControls.Slot group="position" />
</PanelBody>
<ToolsPanelItem
isShownByDefault={ hasPositionAttribute }
label={ __( 'Position' ) }
hasValue={ () => hasPositionAttribute }
onDeselect={ resetPosition }
>
<InspectorControls.Slot group="position" />
</ToolsPanelItem>
</ToolsPanel>
);
};

Expand Down

0 comments on commit 34f09ca

Please sign in to comment.