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

Global Styles: Enable deep linking to the selected block only in the Blocks screen #50708

Merged
merged 2 commits into from
May 18, 2023
Merged
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
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() {
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
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