Skip to content
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
2 changes: 2 additions & 0 deletions packages/global-styles-ui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Bug Fixes

- Revisions: close the screen on the first click of the back arrow after a revision has been selected, instead of requiring a second click ([#81897](https://github.com/WordPress/gutenberg/pull/81897)).
- Revisions: close the screen after applying a revision, which stopped happening when the screen moved into this package ([#81897](https://github.com/WordPress/gutenberg/pull/81897)).
- Color palette panel: do not render the theme colors wrapper when the theme provides no colors, which left an empty gap above the Custom section ([#81894](https://github.com/WordPress/gutenberg/pull/81894)).

### Enhancements
Expand Down
31 changes: 25 additions & 6 deletions packages/global-styles-ui/src/screen-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
__experimentalHeading as Heading,
__experimentalView as View,
__experimentalText as WCText,
Button,
Navigator,
} from '@wordpress/components';
import { isRTL, __ } from '@wordpress/i18n';
Expand All @@ -19,6 +20,12 @@ const { StateControl, StateControlBadges } = unlock( blockEditorPrivateApis );
interface ScreenHeaderProps {
title: string;
description?: string | React.ReactElement;
/**
* Replaces the back button's default navigation. Without it the button
* moves up one path segment, which is wrong for a screen whose path
* carries a selection (for example `/revisions/12`) rather than a
* sub-screen.
*/
onBack?: () => void;
viewportStates?: StateDefinition[];
pseudoStates?: StateDefinition[];
Expand Down Expand Up @@ -47,12 +54,24 @@ export function ScreenHeader( {
<Spacer marginBottom={ 0 } paddingX={ 4 } paddingY={ 3 }>
<VStack spacing={ 2 }>
<HStack spacing={ 2 } alignment="top">
<Navigator.BackButton
icon={ isRTL() ? chevronRight : chevronLeft }
size="small"
label={ __( 'Back' ) }
onClick={ onBack }
/>
{ onBack ? (
<Button
icon={
isRTL() ? chevronRight : chevronLeft
}
size="small"
label={ __( 'Back' ) }
onClick={ onBack }
/>
) : (
<Navigator.BackButton
icon={
isRTL() ? chevronRight : chevronLeft
}
size="small"
label={ __( 'Back' ) }
/>
) }
<Spacer>
<HStack justify="space-between" alignment="top">
<Heading
Expand Down
20 changes: 9 additions & 11 deletions packages/global-styles-ui/src/screen-revisions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import Pagination from '../pagination';

const PAGE_SIZE = 10;

interface ScreenRevisionsProps {
onClose?: () => void;
}

function ScreenRevisions( { onClose }: ScreenRevisionsProps = {} ) {
function ScreenRevisions() {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped the props.

ScreenRevisions isn’t exported from global-styles-ui and <ScreenRevisions /> is used in one place.

Just restoring things to what they were before #72599

const { user: currentEditorGlobalStyles, onChange: setUserConfig } =
useContext( GlobalStylesContext );
const { params, goTo } = useNavigator();
Expand Down Expand Up @@ -55,16 +51,18 @@ function ScreenRevisions( { onClose }: ScreenRevisionsProps = {} ) {
currentEditorGlobalStyles
);

const onCloseRevisions = () => {
if ( onClose ) {
onClose();
}
// Both the back arrow and Apply leave the revisions screen. Selecting a
// revision appends its id to the path (`/revisions/12`), so the navigator's
// default back action would only strip the id and leave the user on the
// same screen. Go straight to the root screen instead.
const closeRevisions = () => {
goTo( '/', { isBack: true } );
};

const restoreRevision = ( revision: any ) => {
setUserConfig( revision );
setIsLoadingRevisionWithUnsavedChanges( false );
onCloseRevisions();
closeRevisions();
};

const handleRevisionSelect = ( revision: any ) => {
Expand Down Expand Up @@ -98,7 +96,7 @@ function ScreenRevisions( { onClose }: ScreenRevisionsProps = {} ) {
description={ __(
'Click on previously saved styles to preview them. To restore a selected version to the editor, hit "Apply." When you\'re ready, use the Save button to save your changes.'
) }
onBack={ onCloseRevisions }
onBack={ closeRevisions }
/>
{ ! hasRevisions && (
<Spinner className="global-styles-ui-screen-revisions__loading" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,15 @@ function RevisionsButtons( {
size="compact"
variant="primary"
className="global-styles-ui-screen-revisions__apply-button"
onClick={ onApplyRevision }
onClick={ ( event: React.MouseEvent ) => {
// This button sits inside the option,
// whose own click handler selects the
// revision. Without this the selection
// would re-run and navigate back to
// the revision just applied.
event.stopPropagation();
onApplyRevision?.();
} }
aria-label={ __(
'Apply the selected revision to your site.'
) }
Expand Down
52 changes: 52 additions & 0 deletions packages/global-styles-ui/src/test/screen-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { Navigator, useNavigator } from '@wordpress/components';
import { ScreenHeader } from '../screen-header';

function CurrentPath() {
const { location } = useNavigator();
return <span data-testid="path">{ location.path }</span>;
}

function renderAtRevision( headerProps ) {
return render(
<Navigator initialPath="/revisions/10">
<CurrentPath />
<Navigator.Screen path="/">
<ScreenHeader title="Styles" />
</Navigator.Screen>
<Navigator.Screen path="/revisions/:revisionId?">
<ScreenHeader title="Revisions (16)" { ...headerProps } />
</Navigator.Screen>
</Navigator>
);
}

describe( 'ScreenHeader back button', () => {
it( 'walks up one path segment by default, which leaves the user on the revisions screen', () => {
renderAtRevision();

fireEvent.click(
screen.getAllByRole( 'button', { name: 'Back' } )[ 0 ]
);

expect( screen.getByTestId( 'path' ) ).toHaveTextContent(
/^\/revisions$/
);
} );

it( 'uses onBack instead of the default navigation when one is given', () => {
const onBack = jest.fn();
renderAtRevision( { onBack } );

fireEvent.click(
screen.getAllByRole( 'button', { name: 'Back' } )[ 0 ]
);

expect( onBack ).toHaveBeenCalledTimes( 1 );
// The default parent walk must not run as well, otherwise the caller's
// destination lands on top of an intermediate location.
expect( screen.getByTestId( 'path' ) ).toHaveTextContent(
/^\/revisions\/10$/
);
} );
} );
48 changes: 48 additions & 0 deletions test/e2e/specs/site-editor/user-global-styles-revisions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,54 @@ test.describe( 'Style Revisions', () => {
).toBeHidden();
} );

test( 'should close the revisions panel with a single click of Back after selecting a revision', async ( {
page,
editor,
userGlobalStylesRevisions,
} ) => {
await editor.canvas.locator( 'body' ).click();
await userGlobalStylesRevisions.openStylesPanel();
await page.getByRole( 'button', { name: 'Revisions' } ).click();

// Selecting a revision puts its id in the path (`/revisions/12`).
await page
.getByRole( 'option', { name: /^Changes saved by / } )
.last()
.click();

await page.getByRole( 'button', { name: 'Back', exact: true } ).click();

await expect(
page.getByLabel( 'Global styles revisions list' )
).toBeHidden();
} );

test( 'should close the revisions panel after applying a revision', async ( {
page,
editor,
userGlobalStylesRevisions,
} ) => {
await editor.canvas.locator( 'body' ).click();
await userGlobalStylesRevisions.openStylesPanel();
await page.getByRole( 'button', { name: 'Revisions' } ).click();

await page
.getByLabel( 'Global styles revisions list' )
.getByRole( 'option' )
.last()
.click();

await page
.getByRole( 'button', {
name: 'Apply the selected revision to your site.',
} )
.click();

await expect(
page.getByLabel( 'Global styles revisions list' )
).toBeHidden();
} );

test( 'should close revisions panel and leave style book open if activated', async ( {
page,
editor,
Expand Down
Loading