From 3f69854e9baaa5e150f76d8baf6f1635661cc30d Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 24 Mar 2023 20:03:28 +0200 Subject: [PATCH 1/7] Global Styles: Link to the block type panel when selecting a block with Styles open --- .../navigator-provider/component.tsx | 2 ++ .../navigator/navigator-screen/component.tsx | 5 +++- packages/components/src/navigator/types.ts | 1 + .../global-styles/screen-block-list.js | 14 +++++---- .../src/components/global-styles/ui.js | 30 +++++++++++++++++-- 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/packages/components/src/navigator/navigator-provider/component.tsx b/packages/components/src/navigator/navigator-provider/component.tsx index 28e710fa577b2d..4c98db5b1a4d18 100644 --- a/packages/components/src/navigator/navigator-provider/component.tsx +++ b/packages/components/src/navigator/navigator-provider/component.tsx @@ -147,6 +147,7 @@ function UnconnectedNavigatorProvider( const { focusTargetSelector, isBack = false, + skipFocus = false, ...restOptions } = options; @@ -168,6 +169,7 @@ function UnconnectedNavigatorProvider( path, isBack, hasRestoredFocus: false, + skipFocus, }; if ( prevLocationHistory.length < 1 ) { diff --git a/packages/components/src/navigator/navigator-screen/component.tsx b/packages/components/src/navigator/navigator-screen/component.tsx index 2d4588d91b14d6..cba000141bdb69 100644 --- a/packages/components/src/navigator/navigator-screen/component.tsx +++ b/packages/components/src/navigator/navigator-screen/component.tsx @@ -100,11 +100,13 @@ function UnconnectedNavigatorScreen( // - when the screen becomes visible // - if the wrapper ref has been assigned // - if focus hasn't already been restored for the current location + // - if the `skipFocus` option is set to `true`. This is useful when we trigger the navigation outside of NavigatorScreen. if ( isInitialLocation || ! isMatch || ! wrapperRef.current || - locationRef.current.hasRestoredFocus + locationRef.current.hasRestoredFocus || + location.skipFocus ) { return; } @@ -143,6 +145,7 @@ function UnconnectedNavigatorScreen( isMatch, location.isBack, location.focusTargetSelector, + location.skipFocus, ] ); const mergedWrapperRef = useMergeRefs( [ forwardedRef, wrapperRef ] ); diff --git a/packages/components/src/navigator/types.ts b/packages/components/src/navigator/types.ts index f6d8f5c22b0a5f..057968fa6f4c16 100644 --- a/packages/components/src/navigator/types.ts +++ b/packages/components/src/navigator/types.ts @@ -13,6 +13,7 @@ export type MatchParams = Record< string, string | string[] >; type NavigateOptions = { focusTargetSelector?: string; isBack?: boolean; + skipFocus?: boolean; }; export type NavigatorLocation = NavigateOptions & { diff --git a/packages/edit-site/src/components/global-styles/screen-block-list.js b/packages/edit-site/src/components/global-styles/screen-block-list.js index 7c60273beda76c..b929339860b2aa 100644 --- a/packages/edit-site/src/components/global-styles/screen-block-list.js +++ b/packages/edit-site/src/components/global-styles/screen-block-list.js @@ -57,21 +57,25 @@ function useSortedBlockTypes() { return [ ...coreItems, ...nonCoreItems ]; } -function BlockMenuItem( { block } ) { - const [ rawSettings ] = useGlobalSetting( '', block.name ); - const settings = useSettingsForBlockElement( rawSettings, block.name ); +export function useBlockHasGlobalStyles( blockName ) { + const [ rawSettings ] = useGlobalSetting( '', blockName ); + const settings = useSettingsForBlockElement( rawSettings, blockName ); const hasTypographyPanel = useHasTypographyPanel( settings ); const hasColorPanel = useHasColorPanel( settings ); const hasBorderPanel = useHasBorderPanel( settings ); const hasDimensionsPanel = useHasDimensionsPanel( settings ); const hasLayoutPanel = hasBorderPanel || hasDimensionsPanel; - const hasVariationsPanel = useHasVariationsPanel( block.name ); - const hasBlockMenuItem = + const hasVariationsPanel = useHasVariationsPanel( blockName ); + const hasGlobalStyles = hasTypographyPanel || hasColorPanel || hasLayoutPanel || hasVariationsPanel; + return hasGlobalStyles; +} +function BlockMenuItem( { block } ) { + const hasBlockMenuItem = useBlockHasGlobalStyles( block.name ); if ( ! hasBlockMenuItem ) { return null; } diff --git a/packages/edit-site/src/components/global-styles/ui.js b/packages/edit-site/src/components/global-styles/ui.js index b9319df5a4a9f1..786dfe0f3b0f42 100644 --- a/packages/edit-site/src/components/global-styles/ui.js +++ b/packages/edit-site/src/components/global-styles/ui.js @@ -10,17 +10,24 @@ import { } from '@wordpress/components'; import { getBlockTypes, store as blocksStore } from '@wordpress/blocks'; import { useSelect, useDispatch } from '@wordpress/data'; -import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; +import { + privateApis as blockEditorPrivateApis, + store as blockEditorStore, +} from '@wordpress/block-editor'; import { __ } 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 } from '@wordpress/element'; /** * Internal dependencies */ import ScreenRoot from './screen-root'; -import ScreenBlockList from './screen-block-list'; +import { + useBlockHasGlobalStyles, + default as ScreenBlockList, +} from './screen-block-list'; import ScreenBlock from './screen-block'; import ScreenTypography from './screen-typography'; import ScreenTypographyElement from './screen-typography-element'; @@ -255,6 +262,24 @@ function GlobalStylesStyleBook( { onClose } ) { ); } +function GlobalStylesBlockLink() { + const navigator = useNavigator(); + const selectedBlockName = useSelect( ( select ) => { + const { getSelectedBlockClientId, getBlockName } = + select( blockEditorStore ); + return getBlockName( getSelectedBlockClientId() ); + }, [] ); + const blockHasGlobalStyles = useBlockHasGlobalStyles( selectedBlockName ); + useEffect( () => { + if ( ! selectedBlockName || ! blockHasGlobalStyles ) { + return; + } + navigator.goTo( '/blocks/' + encodeURIComponent( selectedBlockName ), { + skipFocus: true, + } ); + }, [ selectedBlockName, blockHasGlobalStyles ] ); +} + function GlobalStylesUI( { isStyleBookOpened, onCloseStyleBook } ) { const blocks = getBlockTypes(); @@ -307,6 +332,7 @@ function GlobalStylesUI( { isStyleBookOpened, onCloseStyleBook } ) { ) } + ); } From 1fb9f68c831fc2436d2f4c41e5332c9a35da9e4d Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 28 Mar 2023 18:06:30 +0300 Subject: [PATCH 2/7] add unit test --- .../components/src/navigator/test/index.tsx | 52 +++++++++++++++++++ packages/components/src/navigator/types.ts | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/packages/components/src/navigator/test/index.tsx b/packages/components/src/navigator/test/index.tsx index a27be5475d8028..5a711b8730224a 100644 --- a/packages/components/src/navigator/test/index.tsx +++ b/packages/components/src/navigator/test/index.tsx @@ -22,6 +22,7 @@ import { NavigatorToParentButton, useNavigator, } from '..'; +import type { NavigateOptions } from '../types'; const INVALID_HTML_ATTRIBUTE = { raw: ' "\'><=invalid_path', @@ -57,6 +58,7 @@ const BUTTON_TEXT = { 'Navigate to screen with an invalid HTML value as a path.', back: 'Go back', backUsingGoTo: 'Go back using goTo', + goToWithSkipFocus: 'Go to with skipFocus', }; type CustomTestOnClickHandler = ( @@ -64,6 +66,7 @@ type CustomTestOnClickHandler = ( | { type: 'goTo'; path: string; + options?: NavigateOptions; } | { type: 'goBack' } | { type: 'goToParent' } @@ -108,6 +111,26 @@ function CustomNavigatorGoToBackButton( { ); } +function CustomNavigatorGoToSkipFocusButton( { + path, + onClick, + ...props +}: Omit< ComponentPropsWithoutRef< typeof NavigatorButton >, 'onClick' > & { + onClick?: CustomTestOnClickHandler; +} ) { + const { goTo } = useNavigator(); + return ( +