close
Skip to content

Commit 08bda26

Browse files
Hide grid visualiser if the grid block is hidden (#74963)
Co-authored-by: tellthemachines <isabel_brison@git.wordpress.org> Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
1 parent fdb5877 commit 08bda26

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ import { useSelect } from '@wordpress/data';
1010
*/
1111
import { GridVisualizer, useGridLayoutSync } from '../components/grid';
1212
import { store as blockEditorStore } from '../store';
13+
import useBlockVisibility from '../components/block-visibility/use-block-visibility';
14+
import { deviceTypeKey } from '../store/private-keys';
15+
import { BLOCK_VISIBILITY_VIEWPORTS } from '../components/block-visibility/constants';
1316

1417
function GridLayoutSync( props ) {
1518
useGridLayoutSync( props );
1619
}
1720

1821
function GridTools( { clientId, layout } ) {
19-
const isVisible = useSelect(
22+
const { isVisible, blockVisibility, deviceType } = useSelect(
2023
( select ) => {
2124
const {
2225
isBlockSelected,
2326
isDraggingBlocks,
2427
getTemplateLock,
2528
getBlockEditingMode,
29+
getBlockAttributes,
30+
getSettings,
2631
} = select( blockEditorStore );
2732

2833
// These calls are purposely ordered from least expensive to most expensive.
@@ -32,18 +37,32 @@ function GridTools( { clientId, layout } ) {
3237
getTemplateLock( clientId ) ||
3338
getBlockEditingMode( clientId ) !== 'default'
3439
) {
35-
return false;
40+
return { isVisible: false };
3641
}
3742

38-
return true;
43+
const attributes = getBlockAttributes( clientId );
44+
const settings = getSettings();
45+
46+
return {
47+
isVisible: true,
48+
blockVisibility: attributes?.metadata?.blockVisibility,
49+
deviceType:
50+
settings?.[ deviceTypeKey ]?.toLowerCase() ||
51+
BLOCK_VISIBILITY_VIEWPORTS.desktop.value,
52+
};
3953
},
4054
[ clientId ]
4155
);
4256

57+
const { isBlockCurrentlyHidden } = useBlockVisibility( {
58+
blockVisibility,
59+
deviceType,
60+
} );
61+
4362
return (
4463
<>
4564
<GridLayoutSync clientId={ clientId } />
46-
{ isVisible && (
65+
{ isVisible && ! isBlockCurrentlyHidden && (
4766
<GridVisualizer clientId={ clientId } parentLayout={ layout } />
4867
) }
4968
</>

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import {
1616
GridItemResizer,
1717
GridItemMovers,
1818
} from '../components/grid';
19+
import useBlockVisibility from '../components/block-visibility/use-block-visibility';
20+
import { deviceTypeKey } from '../store/private-keys';
21+
import { BLOCK_VISIBILITY_VIEWPORTS } from '../components/block-visibility/constants';
1922

2023
// Used for generating the instance ID
2124
const LAYOUT_CHILD_BLOCK_PROPS_REFERENCE = {};
@@ -199,12 +202,20 @@ function GridTools( {
199202
isManualPlacement,
200203
parentLayout,
201204
} ) {
202-
const { rootClientId, isVisible } = useSelect(
205+
const {
206+
rootClientId,
207+
isVisible,
208+
parentBlockVisibility,
209+
blockBlockVisibility,
210+
deviceType,
211+
} = useSelect(
203212
( select ) => {
204213
const {
205214
getBlockRootClientId,
206215
getBlockEditingMode,
207216
getTemplateLock,
217+
getBlockAttributes,
218+
getSettings,
208219
} = select( blockEditorStore );
209220

210221
const _rootClientId = getBlockRootClientId( clientId );
@@ -219,21 +230,46 @@ function GridTools( {
219230
};
220231
}
221232

233+
const parentAttributes = getBlockAttributes( _rootClientId );
234+
const blockAttributes = getBlockAttributes( clientId );
235+
const settings = getSettings();
236+
222237
return {
223238
rootClientId: _rootClientId,
224239
isVisible: true,
240+
parentBlockVisibility:
241+
parentAttributes?.metadata?.blockVisibility,
242+
blockBlockVisibility:
243+
blockAttributes?.metadata?.blockVisibility,
244+
deviceType:
245+
settings?.[ deviceTypeKey ]?.toLowerCase() ||
246+
BLOCK_VISIBILITY_VIEWPORTS.desktop.value,
225247
};
226248
},
227249
[ clientId ]
228250
);
229251

252+
const { isBlockCurrentlyHidden: isParentBlockCurrentlyHidden } =
253+
useBlockVisibility( {
254+
blockVisibility: parentBlockVisibility,
255+
deviceType,
256+
} );
257+
258+
const { isBlockCurrentlyHidden: isBlockItselfCurrentlyHidden } =
259+
useBlockVisibility( {
260+
blockVisibility: blockBlockVisibility,
261+
deviceType,
262+
} );
263+
230264
// Use useState() instead of useRef() so that GridItemResizer updates when ref is set.
231265
const [ resizerBounds, setResizerBounds ] = useState();
232266

233-
if ( ! isVisible ) {
267+
if ( ! isVisible || isParentBlockCurrentlyHidden ) {
234268
return null;
235269
}
236270

271+
const showResizer = allowSizingOnChildren && ! isBlockItselfCurrentlyHidden;
272+
237273
function updateLayout( layout ) {
238274
setAttributes( {
239275
style: {
@@ -253,7 +289,7 @@ function GridTools( {
253289
contentRef={ setResizerBounds }
254290
parentLayout={ parentLayout }
255291
/>
256-
{ allowSizingOnChildren && (
292+
{ showResizer && (
257293
<GridItemResizer
258294
clientId={ clientId }
259295
// Don't allow resizing beyond the grid visualizer.

0 commit comments

Comments
 (0)