Skip to content

Commit

Permalink
Try a new block controls group - parent
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Aug 11, 2021
1 parent 87b3cf2 commit 4e3f7d8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
7 changes: 3 additions & 4 deletions packages/block-editor/src/components/block-controls/fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ import {
/**
* Internal dependencies
*/
import useDisplayBlockControls from '../use-display-block-controls';
import groups from './groups';
import useBlockControlsFill from './hook';

export default function BlockControlsFill( {
group = 'default',
controls,
children,
__experimentalExposeToChildren = false,
} ) {
if ( ! useDisplayBlockControls( { __experimentalExposeToChildren } ) ) {
const Fill = useBlockControlsFill( group, __experimentalExposeToChildren );
if ( ! Fill ) {
return null;
}
const Fill = groups[ group ].Fill;

return (
<StyleProvider document={ document }>
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/block-controls/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ const BlockControlsDefault = createSlotFill( 'BlockControls' );
const BlockControlsBlock = createSlotFill( 'BlockControlsBlock' );
const BlockControlsInline = createSlotFill( 'BlockFormatControls' );
const BlockControlsOther = createSlotFill( 'BlockControlsOther' );
const BlockControlsParent = createSlotFill( 'BlockControlsParent' );

const groups = {
default: BlockControlsDefault,
block: BlockControlsBlock,
inline: BlockControlsInline,
other: BlockControlsOther,
parent: BlockControlsParent,
};

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

/**
* 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 useBlockControlsFill( group, exposeToChildren ) {
const isDisplayed = useDisplayBlockControls();
const { clientId } = useBlockEditContext();
const isParentDisplayed = useSelect(
( select ) => {
return (
exposeToChildren &&
select( blockEditorStore ).hasSelectedInnerBlock( clientId )
);
},
[ exposeToChildren, clientId ]
);

if ( isDisplayed ) {
return groups[ group ]?.Fill;
}
if ( isParentDisplayed ) {
return groups.parent.Fill;
}
return null;
}
10 changes: 9 additions & 1 deletion packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@ export default function BlockToolbar( { hideDragHandle } ) {
return (
<div className={ classes }>
{ ! isMultiToolbar && ! displayHeaderToolbar && (
<BlockParentSelector clientIds={ blockClientIds } />
<>
<BlockParentSelector clientIds={ blockClientIds } />
{ shouldShowVisualToolbar && (
<BlockControls.Slot
group="parent"
className="block-editor-block-toolbar__slot"
/>
) }
</>
) }
<div ref={ nodeRef } { ...showMoversGestures }>
{ ( shouldShowVisualToolbar || isMultiToolbar ) && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import { useSelect } from '@wordpress/data';
import { useBlockEditContext } from '../block-edit/context';
import { store as blockEditorStore } from '../../store';

export default function useDisplayBlockControls( {
__experimentalExposeToChildren = false,
} = {} ) {
export default function useDisplayBlockControls() {
const { isSelected, clientId, name } = useBlockEditContext();
const isActive = useSelect(
return useSelect(
( select ) => {
if ( isSelected ) {
return true;
Expand All @@ -23,22 +21,16 @@ export default function useDisplayBlockControls( {
getBlockName,
isFirstMultiSelectedBlock,
getMultiSelectedBlockClientIds,
hasSelectedInnerBlock,
} = select( blockEditorStore );

if ( isFirstMultiSelectedBlock( clientId ) ) {
return getMultiSelectedBlockClientIds().every(
( id ) => getBlockName( id ) === name
);
}
if ( __experimentalExposeToChildren ) {
return hasSelectedInnerBlock( clientId );
}

return false;
},
[ clientId, isSelected, name ]
);

return isActive;
}

0 comments on commit 4e3f7d8

Please sign in to comment.