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

Block Editor: Add a way to absorb inspector controls from the parent #34261

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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 @@ -3,6 +3,7 @@
*/
import { store as blocksStore } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import warning from '@wordpress/warning';

/**
* Internal dependencies
Expand Down Expand Up @@ -34,8 +35,12 @@ export default function useBlockControlsFill( group, exposeToChildren ) {
[ exposeToChildren, clientId ]
);

if ( ! groups[ group ] ) {
warning( `Unknown BlockControls group "${ group }" provided.` );
return null;
}
if ( isDisplayed ) {
return groups[ group ]?.Fill;
return groups[ group ].Fill;
}
if ( isParentDisplayed ) {
return groups.parent.Fill;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ const BlockInspectorSingleBlock = ( {
</PanelBody>
</div>
) }
<InspectorControls.Slot
__experimentalGroup="parent"
bubblesVirtually={ bubblesVirtually }
/>
<InspectorControls.Slot bubblesVirtually={ bubblesVirtually } />
<div>
<AdvancedControls bubblesVirtually={ bubblesVirtually } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@
* WordPress dependencies
*/
import { __experimentalStyleProvider as StyleProvider } from '@wordpress/components';
import warning from '@wordpress/warning';

/**
* Internal dependencies
*/
import useDisplayBlockControls from '../use-display-block-controls';
import groups from './groups';
import useInspectorControlsFill from './hook';

export default function InspectorControlsFill( {
__experimentalGroup: group = 'default',
__experimentalExposeToChildren = false,
children,
} ) {
const isDisplayed = useDisplayBlockControls();
const Fill = groups[ group ]?.Fill;
const Fill = useInspectorControlsFill(
group,
__experimentalExposeToChildren
);
if ( ! Fill ) {
warning( `Unknown InspectorControl group "${ group }" provided.` );
return null;
}
if ( ! isDisplayed ) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,24 @@ import { View } from 'react-native';
*/
import { Children } from '@wordpress/element';
import { BottomSheetConsumer } from '@wordpress/components';
import warning from '@wordpress/warning';

/**
* Internal dependencies
*/
import groups from './groups';
import { useBlockEditContext } from '../block-edit/context';
import { BlockSettingsButton } from '../block-settings';
import useInspectorControlsFill from './hook';

export default function InspectorControlsFill( {
children,
__experimentalGroup: group = 'default',
__experimentalExposeToChildren = false,
children,
...props
} ) {
const { isSelected } = useBlockEditContext();
const Fill = groups[ group ]?.Fill;
const Fill = useInspectorControlsFill(
group,
__experimentalExposeToChildren
);
if ( ! Fill ) {
warning( `Unknown InspectorControl group "${ group }" provided.` );
return null;
}
if ( ! isSelected ) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { createSlotFill } from '@wordpress/components';

const InspectorControlsDefault = createSlotFill( 'InspectorControls' );
const InspectorControlsAdvanced = createSlotFill( 'InspectorAdvancedControls' );
const InspectorControlsParent = createSlotFill( 'InspectorControlsParent' );

const groups = {
default: InspectorControlsDefault,
advanced: InspectorControlsAdvanced,
parent: InspectorControlsParent,
};

export default groups;
49 changes: 49 additions & 0 deletions packages/block-editor/src/components/inspector-controls/hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import warning from '@wordpress/warning';

/**
* Internal dependencies
*/
import groups from './groups';
import { store as blockEditorStore } from '../../store';
import { useBlockEditContext } from '../block-edit/context';
import useDisplayBlockControls from '../use-display-block-controls';

export default function useInspectorControlsFill( group, exposeToChildren ) {
const isDisplayed = useDisplayBlockControls();
const { clientId } = useBlockEditContext();
const isParentDisplayed = useSelect(
( select ) => {
const { getBlockName, hasSelectedInnerBlock } = select(
blockEditorStore
);
const { hasBlockSupport } = select( blocksStore );
return (
exposeToChildren &&
hasBlockSupport(
getBlockName( clientId ),
'__experimentalExposeControlsToChildren',
false
) &&
hasSelectedInnerBlock( clientId )
);
},
[ exposeToChildren, clientId ]
);

if ( ! groups[ group ] ) {
warning( `Unknown InspectorControls group "${ group }" provided.` );
return null;
}
if ( isDisplayed ) {
return groups[ group ].Fill;
}
if ( isParentDisplayed ) {
return groups.parent.Fill;
}
return null;
}
2 changes: 1 addition & 1 deletion packages/block-library/src/social-links/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export function SocialLinksEdit( props ) {
) }
</ToolbarDropdownMenu>
</BlockControls>
<InspectorControls>
<InspectorControls __experimentalExposeToChildren>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think too much about whether it makes sense to expose these two inspector controls present in the Social Links blocks. Let's assume it's only for demo and testing purposes. I'm happy to expose other inspector controls from different blocks as part of this PR or keep it outside of the scope.

<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ __( 'Open links in new tab' ) }
Expand Down