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

Improve "switch to draft" placement #50217

Merged
merged 3 commits into from
May 1, 2023
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
16 changes: 14 additions & 2 deletions packages/edit-post/src/components/sidebar/post-status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { PanelBody } from '@wordpress/components';
import {
__experimentalHStack as HStack,
PanelBody,
} from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose, ifCondition } from '@wordpress/compose';
import { PostSwitchToDraftButton } from '@wordpress/editor';

/**
* Internal dependencies
Expand Down Expand Up @@ -48,7 +52,15 @@ function PostStatus( { isOpened, onTogglePanel } ) {
<PostSlug />
<PostAuthor />
{ fills }
<PostTrash />
<HStack
style={ {
marginTop: '16px',
} }
spacing={ 4 }
>
<PostSwitchToDraftButton />
<PostTrash />
</HStack>
</>
) }
</PluginPostStatusInfo.Slot>
Expand Down
6 changes: 3 additions & 3 deletions packages/edit-post/src/components/sidebar/post-trash/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* WordPress dependencies
*/
import { PanelRow } from '@wordpress/components';
import { PostTrash as PostTrashLink, PostTrashCheck } from '@wordpress/editor';
import { FlexItem } from '@wordpress/components';

export default function PostTrash() {
return (
<PostTrashCheck>
<PanelRow>
<FlexItem isBlock>
Copy link
Contributor

Choose a reason for hiding this comment

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

It feels like these "FlexItem" elements should move into the PostStatus component instead of keeping them within the reusable PostTrash and PostSwitchToDraftButton components?

Copy link
Member Author

Choose a reason for hiding this comment

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

These are not really reusable, I think — it already had a "PanelRow". Moving the flex item to PostStatus means lifting up the logic for display or not displaying each of these, otherwise you end up with an empty flex element that still takes space

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok 👍

<PostTrashLink />
</PanelRow>
</FlexItem>
</PostTrashCheck>
);
}
7 changes: 0 additions & 7 deletions packages/editor/src/components/post-saved-state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { displayShortcut } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import PostSwitchToDraftButton from '../post-switch-to-draft-button';
import { store as editorStore } from '../../store';

/**
Expand Down Expand Up @@ -48,10 +47,8 @@ export default function PostSavedState( {
isDirty,
isNew,
isPending,
isPublished,
isSaveable,
isSaving,
isScheduled,
Copy link
Contributor

Choose a reason for hiding this comment

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

Also we removed these two return values but forgot to remove the keys within the useSelect callback.

hasPublishAction,
} = useSelect(
( select ) => {
Expand Down Expand Up @@ -106,10 +103,6 @@ export default function PostSavedState( {
return null;
}

if ( isPublished || isScheduled ) {
return <PostSwitchToDraftButton />;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this change is causing a small bug. Now for "published posts", the button is there and it says "Save draft", should we return null here instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Opened this #51193 to fix this issue.


/* translators: button label text should, if possible, be under 16 characters. */
const label = isPending ? __( 'Save as pending' ) : __( 'Save draft' );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ exports[`PostSavedState returns a disabled button if the post is not saveable 1`
</button>
`;

exports[`PostSavedState returns a switch to draft link if the post is published 1`] = `
<button
class="components-button editor-post-switch-to-draft is-tertiary"
type="button"
>
Switch to draft
</button>
`;

exports[`PostSavedState should return Save button if edits to be saved 1`] = `
<button
aria-disabled="false"
Expand Down
10 changes: 0 additions & 10 deletions packages/editor/src/components/post-saved-state/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@ describe( 'PostSavedState', () => {
expect( screen.getByRole( 'button' ) ).toMatchSnapshot();
} );

it( 'returns a switch to draft link if the post is published', () => {
useSelect.mockImplementation( () => ( {
isPublished: true,
} ) );

render( <PostSavedState /> );

expect( screen.getByRole( 'button' ) ).toMatchSnapshot();
} );

it( 'should return Saved text if not new and not dirty', () => {
useSelect.mockImplementation( () => ( {
isDirty: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
*/
import {
Button,
FlexItem,
__experimentalConfirmDialog as ConfirmDialog,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose, useViewportMatch } from '@wordpress/compose';
import { compose } from '@wordpress/compose';
import { useState } from '@wordpress/element';

/**
Expand All @@ -21,7 +22,6 @@ function PostSwitchToDraftButton( {
isScheduled,
onClick,
} ) {
const isMobileViewport = useViewportMatch( 'small', '<' );
const [ showConfirmDialog, setShowConfirmDialog ] = useState( false );

if ( ! isPublished && ! isScheduled ) {
Expand All @@ -41,16 +41,17 @@ function PostSwitchToDraftButton( {
};

return (
<>
<FlexItem isBlock>
<Button
className="editor-post-switch-to-draft"
onClick={ () => {
setShowConfirmDialog( true );
} }
disabled={ isSaving }
variant="tertiary"
variant="secondary"
style={ { width: '100%', display: 'block' } }
>
{ isMobileViewport ? __( 'Draft' ) : __( 'Switch to draft' ) }
{ __( 'Switch to draft' ) }
</Button>
<ConfirmDialog
isOpen={ showConfirmDialog }
Expand All @@ -59,7 +60,7 @@ function PostSwitchToDraftButton( {
>
{ alertMessage }
</ConfirmDialog>
</>
</FlexItem>
);
}

Expand Down
4 changes: 1 addition & 3 deletions packages/editor/src/components/post-trash/style.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
.editor-post-trash.components-button {
display: flex;
justify-content: center;
margin-top: $grid-unit-05;
width: 100%;
display: block;
}
9 changes: 6 additions & 3 deletions test/e2e/specs/editor/various/switch-to-draft.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test.describe( 'Clicking "Switch to draft" on a published/scheduled post/page',
page,
switchToDraftUtils,
pageUtils,
editor,
} ) => {
await pageUtils.setBrowserViewport( viewport );

Expand All @@ -44,6 +45,8 @@ test.describe( 'Clicking "Switch to draft" on a published/scheduled post/page',
postStatus === 'schedule'
);

await editor.openDocumentSettingsSidebar();

await switchToDraftUtils.switchToDraftButton.click();

await page
Expand Down Expand Up @@ -100,9 +103,9 @@ class SwitchToDraftUtils {
this.#admin = admin;
this.#requestUtils = requestUtils;

this.switchToDraftButton = page
.getByRole( 'region', { name: 'Editor top bar' } )
.getByRole( 'button', { name: 'draft' } );
this.switchToDraftButton = page.locator(
'role=button[name="Switch to draft"i]'
);
}

/**
Expand Down