Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Position: Refactor "Position" controls panel to use ToolsPanel instead of PanelBody #67967

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Changes from all 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
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 @@ -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 (
<PanelBody
<ToolsPanel
className="block-editor-block-inspector__position"
title={ __( 'Position' ) }
initialOpen={ initialOpen ?? false }
label={ __( 'Position' ) }
resetAll={ resetPosition }
>
<InspectorControls.Slot group="position" />
</PanelBody>
<ToolsPanelItem
isShownByDefault
label={ __( 'Position' ) }
hasValue={ () => !! positionAttribute }
onDeselect={ resetPosition }
>
<InspectorControls.Slot group="position" />
</ToolsPanelItem>
</ToolsPanel>
);
};

Expand Down
Loading