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

Hide grid visualizer when grid is template locked or block editing mode is not default #66065

Merged
Merged
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
32 changes: 23 additions & 9 deletions packages/block-editor/src/hooks/grid-visualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,34 @@ function GridLayoutSync( props ) {
}

function GridTools( { clientId, layout } ) {
const { isSelected, isDragging } = useSelect( ( select ) => {
const { isBlockSelected, isDraggingBlocks } =
select( blockEditorStore );
const isVisible = useSelect(
( select ) => {
const {
isBlockSelected,
isDraggingBlocks,
getTemplateLock,
getBlockEditingMode,
} = select( blockEditorStore );

return {
isSelected: isBlockSelected( clientId ),
isDragging: isDraggingBlocks(),
};
} );
// These calls are purposely ordered from least expensive to most expensive.
// Hides the visualizer in cases where the user is not or cannot interact with it.
if (
( ! isDraggingBlocks() && ! isBlockSelected( clientId ) ) ||
getTemplateLock( clientId ) ||
getBlockEditingMode( clientId ) !== 'default'
) {
return false;
}

return true;
},
[ clientId ]
);

return (
<>
<GridLayoutSync clientId={ clientId } />
{ ( isSelected || isDragging ) && (
{ isVisible && (
<GridVisualizer clientId={ clientId } parentLayout={ layout } />
) }
</>
Expand Down
51 changes: 48 additions & 3 deletions packages/block-editor/src/hooks/layout-child.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,62 @@ function ChildLayoutControlsPure( { clientId, style, setAttributes } ) {
isManualPlacement,
} = parentLayout;

const rootClientId = useSelect(
if ( parentLayoutType !== 'grid' ) {
return null;
}

return (
<GridTools
clientId={ clientId }
style={ style }
setAttributes={ setAttributes }
allowSizingOnChildren={ allowSizingOnChildren }
isManualPlacement={ isManualPlacement }
parentLayout={ parentLayout }
/>
);
}

function GridTools( {
clientId,
style,
setAttributes,
allowSizingOnChildren,
isManualPlacement,
parentLayout,
} ) {
const { rootClientId, isVisible } = useSelect(
( select ) => {
return select( blockEditorStore ).getBlockRootClientId( clientId );
const {
getBlockRootClientId,
getBlockEditingMode,
getTemplateLock,
} = select( blockEditorStore );

const _rootClientId = getBlockRootClientId( clientId );

if (
getTemplateLock( _rootClientId ) ||
getBlockEditingMode( _rootClientId ) !== 'default'
) {
return {
rootClientId: _rootClientId,
isVisible: false,
};
}

return {
rootClientId: _rootClientId,
isVisible: true,
};
},
[ clientId ]
);

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

if ( parentLayoutType !== 'grid' ) {
if ( ! isVisible ) {
return null;
}

Expand Down
Loading