forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include viewport size in logic to show publish toggle or button (Word…
- Loading branch information
Showing
8 changed files
with
202 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
packages/edit-post/src/components/header/post-publish-button-or-toggle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* External Dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies. | ||
*/ | ||
import { PostPublishPanelToggle, PostPublishButton } from '@wordpress/editor'; | ||
import { compose } from '@wordpress/compose'; | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
import { withViewportMatch } from '@wordpress/viewport'; | ||
|
||
export function PostPublishButtonOrToggle( { | ||
forceIsDirty, | ||
forceIsSaving, | ||
hasPublishAction, | ||
isBeingScheduled, | ||
isLessThanMediumViewport, | ||
isPending, | ||
isPublished, | ||
isPublishSidebarEnabled, | ||
isPublishSidebarOpened, | ||
isScheduled, | ||
togglePublishSidebar, | ||
} ) { | ||
const button = ( | ||
<PostPublishButton | ||
forceIsDirty={ forceIsDirty } | ||
forceIsSaving={ forceIsSaving } | ||
/> | ||
); | ||
const toggle = ( | ||
<PostPublishPanelToggle | ||
isOpen={ isPublishSidebarOpened } | ||
onToggle={ togglePublishSidebar } | ||
forceIsSaving={ forceIsSaving } | ||
/> | ||
); | ||
|
||
/** | ||
* We want to show a BUTTON when the post status is at the _final stage_ | ||
* for a particular role (see https://codex.wordpress.org/Post_Status): | ||
* | ||
* - is published | ||
* - is scheduled to be published | ||
* - is pending and can't be published (but only for viewports >= medium) | ||
* | ||
* Originally we considered showing a button for pending posts | ||
* that couldn't be published (for ex, for a contributor role). | ||
* Some languages can have really long translations for "Submit for review", | ||
* so given the lack of UI real state we decided to take into account the viewport | ||
* in that particular case. | ||
*/ | ||
if ( | ||
isPublished || | ||
( isScheduled && isBeingScheduled ) || | ||
( isPending && ! hasPublishAction && ! isLessThanMediumViewport ) | ||
) { | ||
return button; | ||
} | ||
|
||
/** | ||
* Then, we take other things into account: | ||
* | ||
* - Show TOGGLE if it is small viewport. | ||
* - Otherwise, use publish sidebar status to decide - TOGGLE if enabled, BUTTON if not. | ||
*/ | ||
if ( isLessThanMediumViewport ) { | ||
return toggle; | ||
} | ||
|
||
return isPublishSidebarEnabled ? toggle : button; | ||
} | ||
|
||
export default compose( | ||
withSelect( ( select ) => ( { | ||
hasPublishAction: get( | ||
select( 'core/editor' ).getCurrentPost(), | ||
[ '_links', 'wp:action-publish' ], | ||
false | ||
), | ||
isBeingScheduled: select( 'core/editor' ).isEditedPostBeingScheduled(), | ||
isPending: select( 'core/editor' ).isCurrentPostPending(), | ||
isPublished: select( 'core/editor' ).isCurrentPostPublished(), | ||
isPublishSidebarEnabled: select( 'core/editor' ).isPublishSidebarEnabled(), | ||
isPublishSidebarOpened: select( 'core/edit-post' ).isPublishSidebarOpened(), | ||
isScheduled: select( 'core/editor' ).isCurrentPostScheduled(), | ||
} ) ), | ||
withDispatch( ( dispatch ) => { | ||
const { togglePublishSidebar } = dispatch( 'core/edit-post' ); | ||
return { | ||
togglePublishSidebar, | ||
}; | ||
} ), | ||
withViewportMatch( { isLessThanMediumViewport: '< medium' } ), | ||
)( PostPublishButtonOrToggle ); |
13 changes: 13 additions & 0 deletions
13
packages/edit-post/src/components/header/test/__snapshots__/index.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`PostPublishButtonOrToggle should render a button when post is not (1), (2), (3), the viewport is >= medium, and the publish sidebar is disabled 1`] = `<WithSelect(WithDispatch(PostPublishButton)) />`; | ||
|
||
exports[`PostPublishButtonOrToggle should render a button when the post is pending and cannot be published but the viewport is >= medium (3) 1`] = `<WithSelect(WithDispatch(PostPublishButton)) />`; | ||
|
||
exports[`PostPublishButtonOrToggle should render a button when the post is published (1) 1`] = `<WithSelect(WithDispatch(PostPublishButton)) />`; | ||
|
||
exports[`PostPublishButtonOrToggle should render a button when the post is scheduled (2) 1`] = `<WithSelect(WithDispatch(PostPublishButton)) />`; | ||
|
||
exports[`PostPublishButtonOrToggle should render a toggle when post is not (1), (2), (3), the viewport is >= medium, and the publish sidebar is enabled 1`] = `<WithSelect(PostPublishPanelToggle) />`; | ||
|
||
exports[`PostPublishButtonOrToggle should render a toggle when post is not published or scheduled and the viewport is < medium 1`] = `<WithSelect(PostPublishPanelToggle) />`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* Internal dependencies. | ||
*/ | ||
import { PostPublishButtonOrToggle } from '../post-publish-button-or-toggle'; | ||
|
||
describe( 'PostPublishButtonOrToggle should render a', () => { | ||
it( 'button when the post is published (1)', () => { | ||
const wrapper = shallow( <PostPublishButtonOrToggle isPublished={ true } /> ); | ||
expect( wrapper ).toMatchSnapshot(); | ||
} ); | ||
|
||
it( 'button when the post is scheduled (2)', () => { | ||
const wrapper = shallow( <PostPublishButtonOrToggle | ||
isScheduled={ true } | ||
isBeingScheduled={ true } | ||
/> ); | ||
expect( wrapper ).toMatchSnapshot(); | ||
} ); | ||
|
||
it( 'button when the post is pending and cannot be published but the viewport is >= medium (3)', () => { | ||
const wrapper = shallow( <PostPublishButtonOrToggle | ||
isPending={ true } | ||
hasPublishAction={ false } | ||
isLessThanMediumViewport={ false } | ||
/> ); | ||
expect( wrapper ).toMatchSnapshot(); | ||
} ); | ||
|
||
it( 'toggle when post is not published or scheduled and the viewport is < medium', () => { | ||
const wrapper = shallow( <PostPublishButtonOrToggle isLessThanMediumViewport={ true } /> ); | ||
expect( wrapper ).toMatchSnapshot(); | ||
} ); | ||
|
||
it( 'toggle when post is not (1), (2), (3), the viewport is >= medium, and the publish sidebar is enabled', () => { | ||
const wrapper = shallow( <PostPublishButtonOrToggle isPublishSidebarEnabled={ true } /> ); | ||
expect( wrapper ).toMatchSnapshot(); | ||
} ); | ||
|
||
it( 'button when post is not (1), (2), (3), the viewport is >= medium, and the publish sidebar is disabled', () => { | ||
const wrapper = shallow( <PostPublishButtonOrToggle isPublishSidebarEnabled={ false } /> ); | ||
expect( wrapper ).toMatchSnapshot(); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters