Skip to content

Commit 25048e3

Browse files
authored
Hide grid visualizer when grid is template locked or block editing mode is not default (#66065)
Co-authored-by: talldan <[email protected]> Co-authored-by: aaronrobertshaw <[email protected]> Co-authored-by: andrewserong <[email protected]> Co-authored-by: MaggieCabrera <[email protected]> Co-authored-by: bph <[email protected]> * Hide grid visualizer when template locked or block editing mode is not default * Remove unlocks
1 parent 7b29bb3 commit 25048e3

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

packages/block-editor/src/hooks/grid-visualizer.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,34 @@ function GridLayoutSync( props ) {
1616
}
1717

1818
function GridTools( { clientId, layout } ) {
19-
const { isSelected, isDragging } = useSelect( ( select ) => {
20-
const { isBlockSelected, isDraggingBlocks } =
21-
select( blockEditorStore );
19+
const isVisible = useSelect(
20+
( select ) => {
21+
const {
22+
isBlockSelected,
23+
isDraggingBlocks,
24+
getTemplateLock,
25+
getBlockEditingMode,
26+
} = select( blockEditorStore );
2227

23-
return {
24-
isSelected: isBlockSelected( clientId ),
25-
isDragging: isDraggingBlocks(),
26-
};
27-
} );
28+
// These calls are purposely ordered from least expensive to most expensive.
29+
// Hides the visualizer in cases where the user is not or cannot interact with it.
30+
if (
31+
( ! isDraggingBlocks() && ! isBlockSelected( clientId ) ) ||
32+
getTemplateLock( clientId ) ||
33+
getBlockEditingMode( clientId ) !== 'default'
34+
) {
35+
return false;
36+
}
37+
38+
return true;
39+
},
40+
[ clientId ]
41+
);
2842

2943
return (
3044
<>
3145
<GridLayoutSync clientId={ clientId } />
32-
{ ( isSelected || isDragging ) && (
46+
{ isVisible && (
3347
<GridVisualizer clientId={ clientId } parentLayout={ layout } />
3448
) }
3549
</>

packages/block-editor/src/hooks/layout-child.js

+48-3
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,62 @@ function ChildLayoutControlsPure( { clientId, style, setAttributes } ) {
175175
isManualPlacement,
176176
} = parentLayout;
177177

178-
const rootClientId = useSelect(
178+
if ( parentLayoutType !== 'grid' ) {
179+
return null;
180+
}
181+
182+
return (
183+
<GridTools
184+
clientId={ clientId }
185+
style={ style }
186+
setAttributes={ setAttributes }
187+
allowSizingOnChildren={ allowSizingOnChildren }
188+
isManualPlacement={ isManualPlacement }
189+
parentLayout={ parentLayout }
190+
/>
191+
);
192+
}
193+
194+
function GridTools( {
195+
clientId,
196+
style,
197+
setAttributes,
198+
allowSizingOnChildren,
199+
isManualPlacement,
200+
parentLayout,
201+
} ) {
202+
const { rootClientId, isVisible } = useSelect(
179203
( select ) => {
180-
return select( blockEditorStore ).getBlockRootClientId( clientId );
204+
const {
205+
getBlockRootClientId,
206+
getBlockEditingMode,
207+
getTemplateLock,
208+
} = select( blockEditorStore );
209+
210+
const _rootClientId = getBlockRootClientId( clientId );
211+
212+
if (
213+
getTemplateLock( _rootClientId ) ||
214+
getBlockEditingMode( _rootClientId ) !== 'default'
215+
) {
216+
return {
217+
rootClientId: _rootClientId,
218+
isVisible: false,
219+
};
220+
}
221+
222+
return {
223+
rootClientId: _rootClientId,
224+
isVisible: true,
225+
};
181226
},
182227
[ clientId ]
183228
);
184229

185230
// Use useState() instead of useRef() so that GridItemResizer updates when ref is set.
186231
const [ resizerBounds, setResizerBounds ] = useState();
187232

188-
if ( parentLayoutType !== 'grid' ) {
233+
if ( ! isVisible ) {
189234
return null;
190235
}
191236

0 commit comments

Comments
 (0)