Skip to content

Commit

Permalink
Refactor position-controls-panel to reset multiple selected blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
yogeshbhutkar committed Jan 20, 2025
1 parent d41f5f9 commit 6aa96a2
Showing 1 changed file with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,37 @@ import { default as InspectorControls } from '../inspector-controls';
import { store as blockEditorStore } from '../../store';

const PositionControlsPanel = () => {
// Determine whether the panel should be expanded.
const { selectedClientID, positionAttribute } = useSelect( ( select ) => {
const { getSelectedBlockClientIds, getBlockAttributes } =
select( blockEditorStore );
const { selectedClientIDs, positionAttribute, isShownByDefault } =
useSelect( ( select ) => {
const {
getBlocksByClientId,
getSelectedBlockClientIds,
getBlockAttributes,
} = select( blockEditorStore );

const clientIds = getSelectedBlockClientIds();
const selectedBlockClientIds = getSelectedBlockClientIds();
const multiSelectedBlocks = getBlocksByClientId(
selectedBlockClientIds
);

// If multiple blocks are selected, prioritize the first block.
const blockAttributes = getBlockAttributes( clientIds[ 0 ] );
// If multiple blocks are selected, the position value will be the value of the first block.
const blockAttributes = getBlockAttributes(
selectedBlockClientIds[ 0 ]
);

return {
selectedClientID: clientIds[ 0 ],
positionAttribute: blockAttributes?.style?.position?.type,
};
}, [] );
return {
selectedClientIDs: selectedBlockClientIds,
positionAttribute: blockAttributes?.style?.position?.type,
isShownByDefault: multiSelectedBlocks.some(
( { attributes } ) => !! attributes?.style?.position?.type
),
};
}, [] );

const { updateBlockAttributes } = useDispatch( blockEditorStore );

function resetPosition() {
updateBlockAttributes( selectedClientID, {
updateBlockAttributes( selectedClientIDs, {
style: {
position: {
type: undefined,
Expand All @@ -52,7 +63,7 @@ const PositionControlsPanel = () => {
resetAll={ resetPosition }
>
<ToolsPanelItem
isShownByDefault
isShownByDefault={ isShownByDefault }
label={ __( 'Position' ) }
hasValue={ () => !! positionAttribute }
onDeselect={ resetPosition }
Expand Down

0 comments on commit 6aa96a2

Please sign in to comment.