Skip to content
Merged
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 @@ -5,33 +5,64 @@ import {
PanelBody,
__experimentalUseSlotFills as useSlotFills,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useLayoutEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import InspectorControlsGroups from '../inspector-controls/groups';
import { default as InspectorControls } from '../inspector-controls';
import { store as blockEditorStore } from '../../store';

const PositionControls = () => {
const fills = useSlotFills(
InspectorControlsGroups.position.Slot.__unstableName
);
const hasFills = Boolean( fills && fills.length );
const PositionControlsPanel = () => {
const [ initialOpen, setInitialOpen ] = useState();

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

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 ] );

return (
<PanelBody
className="block-editor-block-inspector__position"
title={ __( 'Position' ) }
initialOpen={ false }
initialOpen={ initialOpen ?? false }
>
<InspectorControls.Slot group="position" />
</PanelBody>
);
};

const PositionControls = () => {
const fills = useSlotFills(
InspectorControlsGroups.position.Slot.__unstableName
);
const hasFills = Boolean( fills && fills.length );

if ( ! hasFills ) {
return null;
}

return <PositionControlsPanel />;
};

export default PositionControls;