Skip to content

Commit

Permalink
Fix Global Styles sidebar block selection on zoom out mode (#50708)
Browse files Browse the repository at this point in the history
* Fix Global Styles sidebar block selection on zoom out mode

* [Global Styles]: Enable deep linking to the selected block only in the `Blocks` screen
  • Loading branch information
ntsekouras authored May 18, 2023
1 parent 91fc1cc commit 6725f29
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/edit-site/src/components/global-styles/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { __, sprintf, _n } from '@wordpress/i18n';
import { store as preferencesStore } from '@wordpress/preferences';
import { moreVertical } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
import { useEffect, useRef } from '@wordpress/element';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -203,7 +203,6 @@ function GlobalStylesStyleBook() {

function GlobalStylesBlockLink() {
const navigator = useNavigator();
const isMounted = useRef();
const { selectedBlockName, selectedBlockClientId } = useSelect(
( select ) => {
const { getSelectedBlockClientId, getBlockName } =
Expand All @@ -217,20 +216,23 @@ function GlobalStylesBlockLink() {
[]
);
const blockHasGlobalStyles = useBlockHasGlobalStyles( selectedBlockName );
// When we're in the `Blocks` screen enable deep linking to the selected block.
useEffect( () => {
// Avoid navigating to the block screen on mount.
if ( ! isMounted.current ) {
isMounted.current = true;
if ( ! selectedBlockClientId || ! blockHasGlobalStyles ) {
return;
}
if ( ! selectedBlockClientId || ! blockHasGlobalStyles ) {
const currentPath = navigator.location.path;
if (
currentPath !== '/blocks' &&
! currentPath.startsWith( '/blocks/' )
) {
return;
}
const path = '/blocks/' + encodeURIComponent( selectedBlockName );
const newPath = '/blocks/' + encodeURIComponent( selectedBlockName );
// Avoid navigating to the same path. This can happen when selecting
// a new block of the same type.
if ( path !== navigator.location.path ) {
navigator.goTo( path, { skipFocus: true } );
if ( newPath !== currentPath ) {
navigator.goTo( newPath, { skipFocus: true } );
}
}, [ selectedBlockClientId, selectedBlockName, blockHasGlobalStyles ] );
}
Expand Down

0 comments on commit 6725f29

Please sign in to comment.