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

Style Book: Clear Global Styles navigation history when selecting a block #46391

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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

- `ColorPalette`: show "Clear" button even when colors array is empty ([#46001](https://github.com/WordPress/gutenberg/pull/46001)).
- `InputControl`: Fix internal `Flex` wrapper usage that could add an unintended `height: 100%` ([#46213](https://github.com/WordPress/gutenberg/pull/46213)).
- `Navigator`: Allow calling `goTo` and `goBack` twice in one render cycle ([#46391](https://github.com/WordPress/gutenberg/pull/46391)).

### Enhancements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ function UnconnectedNavigatorProvider(

const goTo: NavigatorContextType[ 'goTo' ] = useCallback(
( path, options = {} ) => {
setLocationHistory( [
...locationHistory,
setLocationHistory( ( prevLocationHistory ) => [
...prevLocationHistory,
{
...options,
path,
Expand All @@ -53,21 +53,24 @@ function UnconnectedNavigatorProvider(
},
] );
},
[ locationHistory ]
[]
);

const goBack: NavigatorContextType[ 'goBack' ] = useCallback( () => {
if ( locationHistory.length > 1 ) {
setLocationHistory( [
...locationHistory.slice( 0, -2 ),
setLocationHistory( ( prevLocationHistory ) => {
if ( prevLocationHistory.length <= 1 ) {
return prevLocationHistory;
}
return [
...prevLocationHistory.slice( 0, -2 ),
{
...locationHistory[ locationHistory.length - 2 ],
...prevLocationHistory[ prevLocationHistory.length - 2 ],
isBack: true,
hasRestoredFocus: false,
},
] );
}
}, [ locationHistory ] );
];
} );
}, [] );

const navigatorContextValue: NavigatorContextType = useMemo(
() => ( {
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/global-styles/palette.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function Palette( { name } ) {

const screenPath = ! name
? '/colors/palette'
: '/blocks/' + name + '/colors/palette';
: '/blocks/' + encodeURIComponent( name ) + '/colors/palette';
const paletteButtonText =
colors.length > 0
? sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function BlockMenuItem( { block } ) {

return (
<NavigationButtonAsItem
path={ '/blocks/' + block.name }
path={ '/blocks/' + encodeURIComponent( block.name ) }
aria-label={ navigationButtonLabel }
>
<HStack justify="flex-start">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ function ScreenBlock( { name } ) {
<Spacer paddingX={ 4 }>
<BlockPreviewPanel name={ name } />
</Spacer>
<ContextMenu parentMenu={ '/blocks/' + name } name={ name } />
<ContextMenu
parentMenu={ '/blocks/' + encodeURIComponent( name ) }
name={ name }
/>
</>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ function ButtonColorItem( { name, parentMenu } ) {
}

function ScreenColors( { name } ) {
const parentMenu = name === undefined ? '' : '/blocks/' + name;
const parentMenu =
name === undefined ? '' : '/blocks/' + encodeURIComponent( name );

return (
<>
Expand Down
26 changes: 20 additions & 6 deletions packages/edit-site/src/components/global-styles/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ function GlobalStylesNavigationScreen( { className, ...props } ) {
}

function ContextScreens( { name } ) {
const parentMenu = name === undefined ? '' : '/blocks/' + name;
const parentMenu =
name === undefined ? '' : '/blocks/' + encodeURIComponent( name );

return (
<>
Expand Down Expand Up @@ -124,14 +125,27 @@ function ContextScreens( { name } ) {

function GlobalStylesStyleBook( { onClose } ) {
const navigator = useNavigator();
const { path } = navigator.location;
return (
<StyleBook
isSelected={ ( blockName ) =>
navigator.location.path.startsWith( '/blocks/' + blockName )
}
onSelect={ ( blockName ) =>
navigator.goTo( '/blocks/' + blockName )
// Match '/blocks/core%2Fbutton' and
// '/blocks/core%2Fbutton/typography', but not
// '/blocks/core%2Fbuttons'.
path === `/blocks/${ encodeURIComponent( blockName ) }` ||
path.startsWith(
`/blocks/${ encodeURIComponent( blockName ) }/`
)
}
onSelect={ ( blockName ) => {
// Clear navigator history by going back to the root.
const depth = path.match( /\//g ).length;
for ( let i = 0; i < depth; i++ ) {
navigator.goBack();
}
// Now go to the selected block.
navigator.goTo( '/blocks/' + encodeURIComponent( blockName ) );
} }
onClose={ onClose }
/>
);
Expand Down Expand Up @@ -159,7 +173,7 @@ function GlobalStylesUI( { isStyleBookOpened, onCloseStyleBook } ) {
{ blocks.map( ( block ) => (
<GlobalStylesNavigationScreen
key={ 'menu-block-' + block.name }
path={ '/blocks/' + block.name }
path={ '/blocks/' + encodeURIComponent( block.name ) }
>
<ScreenBlock name={ block.name } />
</GlobalStylesNavigationScreen>
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/specs/site-editor/style-book.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ test.describe( 'Style Book', () => {
).toBeVisible();
} );

test( 'should clear Global Styles navigator history when example is clicked', async ( {
page,
} ) => {
await page.click( 'role=button[name="Blocks styles"]' );
await page.click( 'role=button[name="Heading block styles"]' );
await page.click( 'role=button[name="Typography styles"]' );

await page.click(
'role=button[name="Open Quote styles in Styles panel"i]'
);

await page.click( 'role=button[name="Navigate to the previous view"]' );

await expect(
page.locator( 'role=button[name="Navigate to the previous view"]' )
).not.toBeVisible();
} );

test( 'should disappear when closed', async ( { page } ) => {
await page.click(
'role=region[name="Style Book"i] >> role=button[name="Close Style Book"i]'
Expand Down