Skip to content

Commit

Permalink
Move creation of ToolsPanel to within fills
Browse files Browse the repository at this point in the history
Creating the ToolsPanel for block supports around the slot resulted in the slot adding an extra div within the ToolsPanel breaking its layout. This div is needed for the slot's ref to be applied allowing virtual bubbling.  This change effectively moves the ToolsPanel inside that. It also avoids needing to retrieve ToolsPanelContext and recreating the context provider.
  • Loading branch information
aaronrobertshaw committed Sep 13, 2021
1 parent 397c85b commit 94f444c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 37 deletions.

This file was deleted.

Expand Up @@ -10,7 +10,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '../../store';
import { cleanEmptyObject } from '../../hooks/utils';

export default function BlockSupportToolsPanel( { children, label, header } ) {
export default function BlockSupportToolsPanel( { children, label } ) {
const { clientId, attributes } = useSelect( ( select ) => {
const { getBlockAttributes, getSelectedBlockClientId } = select(
blockEditorStore
Expand Down Expand Up @@ -47,7 +47,6 @@ export default function BlockSupportToolsPanel( { children, label, header } ) {
return (
<ToolsPanel
label={ label }
header={ header }
resetAll={ resetAll }
key={ clientId }
panelId={ clientId }
Expand Down
28 changes: 12 additions & 16 deletions packages/block-editor/src/components/inspector-controls/fill.js
@@ -1,20 +1,13 @@
/**
* External dependencies
*/
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
import {
__experimentalStyleProvider as StyleProvider,
__experimentalToolsPanelContext as ToolsPanelContext,
} from '@wordpress/components';
import { __experimentalStyleProvider as StyleProvider } from '@wordpress/components';
import warning from '@wordpress/warning';

/**
* Internal dependencies
*/
import BlockSupportToolsPanel from './block-support-tools-panel';
import useDisplayBlockControls from '../use-display-block-controls';
import groups from './groups';

Expand All @@ -36,15 +29,18 @@ export default function InspectorControlsFill( {
<StyleProvider document={ document }>
<Fill>
{ ( fillProps ) => {
// Children passed to InspectorControlsFill will not have
// access to any React Context whose Provider is part of
// the InspectorControlsSlot tree. So we re-create the
// Provider in this subtree.
const value = ! isEmpty( fillProps ) ? fillProps : null;
// Check if the fills here represent the contents of a
// block support panel and require a wrapping ToolsPanel.
const { label } = fillProps;

if ( ! label ) {
return children;
}

return (
<ToolsPanelContext.Provider value={ value }>
<BlockSupportToolsPanel label={ label }>
{ children }
</ToolsPanelContext.Provider>
</BlockSupportToolsPanel>
);
} }
</Fill>
Expand Down
17 changes: 8 additions & 9 deletions packages/block-editor/src/components/inspector-controls/slot.js
Expand Up @@ -7,8 +7,6 @@ import warning from '@wordpress/warning';
/**
* Internal dependencies
*/
import BlockSupportToolsPanel from './block-support-tools-panel';
import BlockSupportSlotContainer from './block-support-slot-container';
import groups from './groups';

export default function InspectorControlsSlot( {
Expand All @@ -30,14 +28,15 @@ export default function InspectorControlsSlot( {
}

if ( label ) {
// Slots for block support panels will include a label for the panel
// header. This is passed through the fillProps to indicate when the
// fills require a wrapping ToolsPanel.
return (
<BlockSupportToolsPanel group={ group } label={ label }>
<BlockSupportSlotContainer
{ ...props }
bubblesVirtually={ bubblesVirtually }
Slot={ Slot }
/>
</BlockSupportToolsPanel>
<Slot
{ ...props }
bubblesVirtually={ bubblesVirtually }
fillProps={ { label } }
/>
);
}

Expand Down

0 comments on commit 94f444c

Please sign in to comment.