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 Panel: Open by default if a position type is set #49151

Merged
merged 6 commits into from
Mar 21, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,56 @@ import {
PanelBody,
__experimentalUseSlotFills as useSlotFills,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
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 = () => {
// Determine whether the panel should be expanded.
const { initialOpen } = useSelect( ( select ) => {
const { getBlocksByClientId, getSelectedBlockClientIds } =
select( blockEditorStore );

if ( ! hasFills ) {
return null;
}
// 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.
const clientIds = getSelectedBlockClientIds();
const multiSelectedBlocks = getBlocksByClientId( clientIds );

return {
initialOpen: multiSelectedBlocks.some(
( { attributes } ) => !! attributes?.style?.position?.type
),
};
}, [] );

return (
<PanelBody
className="block-editor-block-inspector__position"
title={ __( 'Position' ) }
initialOpen={ false }
initialOpen={ initialOpen }
>
<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;