Skip to content

Commit df2028f

Browse files
committed
Use fillProps for filtering contentOnly block tools
1 parent 4232366 commit df2028f

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

packages/block-editor/src/components/block-controls/fill.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { Children } from '@wordpress/element';
1111
* Internal dependencies
1212
*/
1313
import useBlockControlsFill from './hook';
14-
import { useBlockEditingMode } from '../block-editing-mode';
1514

1615
export default function BlockControlsFill( {
1716
group = 'default',
@@ -23,40 +22,39 @@ export default function BlockControlsFill( {
2322
group,
2423
__experimentalShareWithChildBlocks
2524
);
26-
const blockEditingMode = useBlockEditingMode();
2725

2826
if ( ! Fill ) {
2927
return null;
3028
}
3129

32-
// Filter children in content-only mode
33-
let filteredChildren = children;
34-
if ( blockEditingMode === 'contentOnly' ) {
35-
const childrenArray = Children.toArray( children );
36-
filteredChildren = childrenArray.filter( ( child ) => {
37-
return child?.props?.category === 'content';
38-
} );
39-
}
40-
41-
const innerMarkup = (
42-
<>
43-
{ group === 'default' && <ToolbarGroup controls={ controls } /> }
44-
{ filteredChildren }
45-
</>
46-
);
47-
4830
return (
4931
<StyleProvider document={ document }>
5032
<Fill>
5133
{ ( fillProps ) => {
5234
// `fillProps.forwardedContext` is an array of context provider entries, provided by slot,
5335
// that should wrap the fill markup.
54-
const { forwardedContext = [] } = fillProps;
36+
const { forwardedContext = [], shouldRender = () => true } =
37+
fillProps;
38+
39+
// Filter children based on shouldRender callback
40+
const childrenArray = Children.toArray( children );
41+
const filteredChildren =
42+
childrenArray.filter( shouldRender );
43+
44+
const filteredMarkup = (
45+
<>
46+
{ group === 'default' && (
47+
<ToolbarGroup controls={ controls } />
48+
) }
49+
{ filteredChildren }
50+
</>
51+
);
52+
5553
return forwardedContext.reduce(
5654
( inner, [ Provider, props ] ) => (
5755
<Provider { ...props }>{ inner }</Provider>
5856
),
59-
innerMarkup
57+
filteredMarkup
6058
);
6159
} }
6260
</Fill>

packages/block-editor/src/components/block-controls/slot.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,33 @@ import warning from '@wordpress/warning';
1515
*/
1616
import groups from './groups';
1717
import { unlock } from '../../lock-unlock';
18+
import { useBlockEditingMode } from '../block-editing-mode';
1819

1920
const { ComponentsContext } = unlock( privateApis );
2021

2122
export default function BlockControlsSlot( { group = 'default', ...props } ) {
2223
const toolbarState = useContext( ToolbarContext );
2324
const contextState = useContext( ComponentsContext );
25+
26+
// Get the block editing mode for the current block
27+
const blockEditingMode = useBlockEditingMode();
28+
2429
const fillProps = useMemo(
2530
() => ( {
2631
forwardedContext: [
2732
[ ToolbarContext.Provider, { value: toolbarState } ],
2833
[ ComponentsContext.Provider, { value: contextState } ],
2934
],
35+
shouldRender: ( element ) => {
36+
// In content-only mode, only render elements with category="content"
37+
if ( blockEditingMode === 'contentOnly' ) {
38+
return element?.props?.category === 'content';
39+
}
40+
// In other modes, render all elements
41+
return true;
42+
},
3043
} ),
31-
[ toolbarState, contextState ]
44+
[ toolbarState, contextState, blockEditingMode ]
3245
);
3346

3447
const slotFill = groups[ group ];

0 commit comments

Comments
 (0)