diff --git a/packages/edit-post/src/components/sidebar/post-visibility/index.js b/packages/edit-post/src/components/sidebar/post-visibility/index.js index 35043102f0e9e..a03de00917a9c 100644 --- a/packages/edit-post/src/components/sidebar/post-visibility/index.js +++ b/packages/edit-post/src/components/sidebar/post-visibility/index.js @@ -8,12 +8,14 @@ import { PostVisibilityLabel, PostVisibilityCheck, } from '@wordpress/editor'; +import { useRef } from '@wordpress/element'; export function PostVisibility() { + const rowRef = useRef(); return ( ( - + { __( 'Visibility' ) } { ! canEdit && ( @@ -24,6 +26,12 @@ export function PostVisibility() { ( ) } - renderContent={ () => } + renderContent={ ( { onClose } ) => ( + + ) } /> ) } diff --git a/packages/edit-post/src/components/sidebar/post-visibility/style.scss b/packages/edit-post/src/components/sidebar/post-visibility/style.scss index 3fff36cf09e3a..2f3599a3c5e8c 100644 --- a/packages/edit-post/src/components/sidebar/post-visibility/style.scss +++ b/packages/edit-post/src/components/sidebar/post-visibility/style.scss @@ -9,34 +9,6 @@ } .edit-post-post-visibility__dialog .components-popover__content { - @include break-medium { - // 279px (sidebar width) - 20px (padding) - 2px (horizontal borders) - width: 257px; - } -} - -.edit-post-post-visibility__dialog-legend { - font-weight: 600; -} - -.edit-post-post-visibility__choice { - margin: 10px 0; -} - -.edit-post-post-visibility__dialog-radio, -.edit-post-post-visibility__dialog-label { - vertical-align: top; -} - -.edit-post-post-visibility__dialog-password-input { - width: calc(100% - 20px); - margin-left: 20px; -} - -.edit-post-post-visibility__dialog-info { - color: $gray-700; - padding-left: 20px; - font-style: italic; - margin: 4px 0 0; - line-height: 1.4; + // sidebar width - padding - horizontal borders + width: $sidebar-width - $grid-unit-20 - $border-width * 2; } diff --git a/packages/editor/src/components/post-visibility/index.js b/packages/editor/src/components/post-visibility/index.js index 86fe0df98db15..f4fb6186d4c0b 100644 --- a/packages/editor/src/components/post-visibility/index.js +++ b/packages/editor/src/components/post-visibility/index.js @@ -2,13 +2,15 @@ * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { Component } from '@wordpress/element'; +import { useState } from '@wordpress/element'; import { VisuallyHidden, __experimentalConfirmDialog as ConfirmDialog, + Button, } from '@wordpress/components'; -import { withInstanceId, compose } from '@wordpress/compose'; -import { withSelect, withDispatch } from '@wordpress/data'; +import { useInstanceId } from '@wordpress/compose'; +import { useSelect, useDispatch } from '@wordpress/data'; +import { close as closeIcon } from '@wordpress/icons'; /** * Internal dependencies @@ -16,172 +18,150 @@ import { withSelect, withDispatch } from '@wordpress/data'; import { visibilityOptions } from './utils'; import { store as editorStore } from '../../store'; -export class PostVisibility extends Component { - constructor( props ) { - super( ...arguments ); +export default function PostVisibility( { onClose } ) { + const instanceId = useInstanceId( PostVisibility ); - this.setPublic = this.setPublic.bind( this ); - this.setPrivate = this.setPrivate.bind( this ); - this.setPasswordProtected = this.setPasswordProtected.bind( this ); - this.updatePassword = this.updatePassword.bind( this ); + const { status, visibility, password } = useSelect( ( select ) => ( { + status: select( editorStore ).getEditedPostAttribute( 'status' ), + visibility: select( editorStore ).getEditedPostVisibility(), + password: select( editorStore ).getEditedPostAttribute( 'password' ), + } ) ); - this.state = { - hasPassword: !! props.password, - showPrivateConfirmDialog: false, - }; - } + const { editPost, savePost } = useDispatch( editorStore ); - setPublic() { - const { visibility, onUpdateVisibility, status } = this.props; + const [ hasPassword, setHasPassword ] = useState( !! password ); + const [ showPrivateConfirmDialog, setShowPrivateConfirmDialog ] = useState( + false + ); - onUpdateVisibility( visibility === 'private' ? 'draft' : status ); - this.setState( { hasPassword: false } ); - } - - setPrivate() { - this.setState( { showPrivateConfirmDialog: true } ); - } - - confirmPrivate = () => { - const { onUpdateVisibility, onSave } = this.props; - - onUpdateVisibility( 'private' ); - this.setState( { - hasPassword: false, - showPrivateConfirmDialog: false, + const setPublic = () => { + editPost( { + status: visibility === 'private' ? 'draft' : status, + password: '', } ); - onSave(); + setHasPassword( false ); }; - handleDialogCancel = () => { - this.setState( { showPrivateConfirmDialog: false } ); + const setPrivate = () => { + setShowPrivateConfirmDialog( true ); }; - setPasswordProtected() { - const { visibility, onUpdateVisibility, status, password } = this.props; - - onUpdateVisibility( - visibility === 'private' ? 'draft' : status, - password || '' - ); - this.setState( { hasPassword: true } ); - } + const confirmPrivate = () => { + editPost( { status: 'private', password: '' } ); + setHasPassword( false ); + setShowPrivateConfirmDialog( false ); + savePost(); + }; - updatePassword( event ) { - const { status, onUpdateVisibility } = this.props; - onUpdateVisibility( status, event.target.value ); - } + const handleDialogCancel = () => { + setShowPrivateConfirmDialog( false ); + }; - render() { - const { visibility, password, instanceId } = this.props; + const setPasswordProtected = () => { + editPost( { + status: visibility === 'private' ? 'draft' : status, + password: password || '', + } ); + setHasPassword( true ); + }; - const visibilityHandlers = { - public: { - onSelect: this.setPublic, - checked: visibility === 'public' && ! this.state.hasPassword, - }, - private: { - onSelect: this.setPrivate, - checked: visibility === 'private', - }, - password: { - onSelect: this.setPasswordProtected, - checked: this.state.hasPassword, - }, - }; + const updatePassword = ( event ) => { + editPost( { password: event.target.value } ); + }; - return [ -
- - { __( 'Post Visibility' ) } + return ( + <> +
{ __( 'Would you like to privately publish this post now?' ) } - , - ]; - } + + + ); } -export default compose( [ - withSelect( ( select ) => { - const { getEditedPostAttribute, getEditedPostVisibility } = select( - editorStore - ); - return { - status: getEditedPostAttribute( 'status' ), - visibility: getEditedPostVisibility(), - password: getEditedPostAttribute( 'password' ), - }; - } ), - withDispatch( ( dispatch ) => { - const { savePost, editPost } = dispatch( editorStore ); - return { - onSave: savePost, - onUpdateVisibility( status, password = '' ) { - editPost( { status, password } ); - }, - }; - } ), - withInstanceId, -] )( PostVisibility ); +function PostVisibilityChoice( { instanceId, value, label, info, ...props } ) { + return ( +
+ + +

+ { info } +

+
+ ); +} diff --git a/packages/editor/src/components/post-visibility/label.js b/packages/editor/src/components/post-visibility/label.js index 79d20d3f45b45..2e50aebe488da 100644 --- a/packages/editor/src/components/post-visibility/label.js +++ b/packages/editor/src/components/post-visibility/label.js @@ -1,12 +1,7 @@ -/** - * External dependencies - */ -import { find } from 'lodash'; - /** * WordPress dependencies */ -import { withSelect } from '@wordpress/data'; +import { useSelect } from '@wordpress/data'; /** * Internal dependencies @@ -14,13 +9,9 @@ import { withSelect } from '@wordpress/data'; import { visibilityOptions } from './utils'; import { store as editorStore } from '../../store'; -function PostVisibilityLabel( { visibility } ) { - const getVisibilityLabel = () => - find( visibilityOptions, { value: visibility } ).label; - - return getVisibilityLabel( visibility ); +export default function PostVisibilityLabel() { + const visibility = useSelect( ( select ) => + select( editorStore ).getEditedPostVisibility() + ); + return visibilityOptions[ visibility ]?.label; } - -export default withSelect( ( select ) => ( { - visibility: select( editorStore ).getEditedPostVisibility(), -} ) )( PostVisibilityLabel ); diff --git a/packages/editor/src/components/post-visibility/style.scss b/packages/editor/src/components/post-visibility/style.scss index d43341ac3d128..3bc59e45e5398 100644 --- a/packages/editor/src/components/post-visibility/style.scss +++ b/packages/editor/src/components/post-visibility/style.scss @@ -1,39 +1,44 @@ -.edit-post-post-visibility__dialog, -.editor-post-visibility__dialog-fieldset { - padding: $grid-unit-05; - padding-top: 0; +.editor-post-visibility__close { + position: absolute; + right: $grid-unit-20; + top: $grid-unit-20; +} + +.editor-post-visibility__fieldset { + padding: $grid-unit-10; - .editor-post-visibility__dialog-legend { + .editor-post-visibility__legend { font-weight: 600; - margin-bottom: 1em; + padding: 1em 0 0 0; + } + + .editor-post-visibility__description { margin-top: 0.5em; - padding: 0; } - .editor-post-visibility__dialog-radio[type="radio"] { + .editor-post-visibility__radio[type="radio"] { @include radio-control; margin-top: 2px; } - .editor-post-visibility__dialog-label { - font-weight: 600; - } + .editor-post-visibility__info { + color: $gray-700; + margin-left: $grid-unit-15 + $radio-input-size-sm; + margin-top: 0.5em; - .editor-post-visibility__dialog-info { - margin-top: 0; - margin-left: $grid-unit-40; + @include break-small { + margin-left: $grid-unit-15 + $radio-input-size; + } } // Remove bottom margin on the last label only. - .editor-post-visibility__choice:last-child .editor-post-visibility__dialog-info { + .editor-post-visibility__choice:last-child .editor-post-visibility__info { margin-bottom: 0; } -} -.editor-post-visibility__dialog-password { - .editor-post-visibility__dialog-password-input[type="text"] { + .editor-post-visibility__password .editor-post-visibility__password-input[type="text"] { @include input-control; - margin-left: $grid-unit * 4.5; - margin-top: $grid-unit-10; + margin-left: $grid-unit * 4; + width: calc(100% - #{$grid-unit * 4}); } } diff --git a/packages/editor/src/components/post-visibility/utils.js b/packages/editor/src/components/post-visibility/utils.js index 76f34703274e1..65f747878e5b9 100644 --- a/packages/editor/src/components/post-visibility/utils.js +++ b/packages/editor/src/components/post-visibility/utils.js @@ -3,22 +3,17 @@ */ import { __ } from '@wordpress/i18n'; -export const visibilityOptions = [ - { - value: 'public', +export const visibilityOptions = { + public: { label: __( 'Public' ), info: __( 'Visible to everyone.' ), }, - { - value: 'private', + private: { label: __( 'Private' ), info: __( 'Only visible to site admins and editors.' ), }, - { - value: 'password', - label: __( 'Password Protected' ), - info: __( - 'Protected with a password you choose. Only those with the password can view this post.' - ), + password: { + label: __( 'Password protected' ), + info: __( 'Only those with the password can view this post.' ), }, -]; +};