Repository navigation
Block Editor: Introduce block locking UI #39183
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
Changes from all commits
6411375
337b785
f003c72
63b38b4
37a6f6b
2304b51
8a95eff
c382ea7
e8e76ce
829522b
5cd6447
79a225b
12ae953
ed8833c
e652fef
442b0fd
9bac66b
9c05927
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export { default as BlockLockMenuItem } from './menu-item'; | ||
| export { default as BlockLockModal } from './modal'; | ||
| export { default as BlockLockToolbar } from './toolbar'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useReducer } from '@wordpress/element'; | ||
| import { MenuItem } from '@wordpress/components'; | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { lock, unlock } from '@wordpress/icons'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import BlockLockModal from './modal'; | ||
| import { store as blockEditorStore } from '../../store'; | ||
|
|
||
| export default function BlockLockMenuItem( { clientId } ) { | ||
| const { isLocked } = useSelect( | ||
| ( select ) => { | ||
| const { | ||
| canMoveBlock, | ||
| canRemoveBlock, | ||
| getBlockRootClientId, | ||
| } = select( blockEditorStore ); | ||
| const rootClientId = getBlockRootClientId( clientId ); | ||
|
|
||
| return { | ||
| isLocked: | ||
| ! canMoveBlock( clientId, rootClientId ) || | ||
| ! canRemoveBlock( clientId, rootClientId ), | ||
| }; | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
|
|
||
| const [ isModalOpen, toggleModal ] = useReducer( | ||
| ( isActive ) => ! isActive, | ||
| false | ||
| ); | ||
|
|
||
| const label = isLocked ? __( 'Unlock' ) : __( 'Lock' ); | ||
|
|
||
| return ( | ||
| <> | ||
| <MenuItem icon={ isLocked ? unlock : lock } onClick={ toggleModal }> | ||
| { label } | ||
| </MenuItem> | ||
| { isModalOpen && ( | ||
| <BlockLockModal clientId={ clientId } onClose={ toggleModal } /> | ||
| ) } | ||
| </> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import { useEffect, useState } from '@wordpress/element'; | ||
| import { | ||
| Button, | ||
| CheckboxControl, | ||
| Flex, | ||
| FlexItem, | ||
| Icon, | ||
| Modal, | ||
| } from '@wordpress/components'; | ||
| import { dragHandle, trash } from '@wordpress/icons'; | ||
| import { useInstanceId } from '@wordpress/compose'; | ||
| import { useDispatch, useSelect } from '@wordpress/data'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import useBlockDisplayInformation from '../use-block-display-information'; | ||
| import { store as blockEditorStore } from '../../store'; | ||
|
|
||
| export default function BlockLockModal( { clientId, onClose } ) { | ||
| const [ lock, setLock ] = useState( { move: false, remove: false } ); | ||
| const { canMove, canRemove } = useSelect( | ||
| ( select ) => { | ||
| const { | ||
| canMoveBlock, | ||
| canRemoveBlock, | ||
| getBlockRootClientId, | ||
| } = select( blockEditorStore ); | ||
| const rootClientId = getBlockRootClientId( clientId ); | ||
|
|
||
| return { | ||
| canMove: canMoveBlock( clientId, rootClientId ), | ||
| canRemove: canRemoveBlock( clientId, rootClientId ), | ||
| }; | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
| const { updateBlockAttributes } = useDispatch( blockEditorStore ); | ||
| const blockInformation = useBlockDisplayInformation( clientId ); | ||
| const instanceId = useInstanceId( | ||
| BlockLockModal, | ||
| 'block-editor-block-lock-modal__options-title' | ||
| ); | ||
|
|
||
| useEffect( () => { | ||
| setLock( { | ||
| move: ! canMove, | ||
| remove: ! canRemove, | ||
| } ); | ||
| }, [ canMove, canRemove ] ); | ||
|
|
||
| const isAllChecked = Object.values( lock ).every( Boolean ); | ||
|
|
||
| let ariaChecked; | ||
| if ( isAllChecked ) { | ||
| ariaChecked = 'true'; | ||
| } else if ( Object.values( lock ).some( Boolean ) ) { | ||
| ariaChecked = 'mixed'; | ||
| } else { | ||
| ariaChecked = 'false'; | ||
| } | ||
|
|
||
| return ( | ||
| <Modal | ||
| title={ sprintf( | ||
| /* translators: %s: Name of the block. */ | ||
| __( 'Lock %s' ), | ||
| blockInformation.title | ||
| ) } | ||
| overlayClassName="block-editor-block-lock-modal" | ||
| closeLabel={ __( 'Close' ) } | ||
| onRequestClose={ onClose } | ||
| > | ||
| <form | ||
| onSubmit={ ( event ) => { | ||
| event.preventDefault(); | ||
| updateBlockAttributes( [ clientId ], { lock } ); | ||
| onClose(); | ||
| } } | ||
| > | ||
| <p> | ||
| { __( | ||
| 'Choose specific attributes to restrict or lock all available options.' | ||
| ) } | ||
| </p> | ||
| <div | ||
| role="group" | ||
| aria-labelledby={ instanceId } | ||
| className="block-editor-block-lock-modal__options" | ||
| > | ||
| <CheckboxControl | ||
| className="block-editor-block-lock-modal__options-title" | ||
| label={ | ||
| <span id={ instanceId }>{ __( 'Lock all' ) }</span> | ||
| } | ||
| checked={ isAllChecked } | ||
| aria-checked={ ariaChecked } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you switch to using this? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#attr-indeterminate Seems like aria-checked is not taking effect due to the missing checkbox role.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback, @alexstine. I didn't know the I wonder if this needs to be supported by the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you can pass additional props but might be nice to have the support.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to MDN:
So passing it as prop/attribute won't work.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, so this has to be queried by ID or class directly? Unbelievable. 👎 What happens if you add
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's possible to support this feature in custom components. Do you mind creating a new issue for
I assumed inputs with type="checkbox" would have this role automatically assigned.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Mamaduka I will open an issue. For now, how hard would it be to convert this to radio buttons?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sorry if I'm missing something here, but how would this help us? There's at least one more place that uses
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Mamaduka It would help because only one radio button at a time can be selected. So you could have this for example: Label: Lock block removal?
Different than the original design but it would meet A11Y needs. Just another option if the checkbox feature takes too long.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the ping. We're definitely going to take a look and report back |
||
| onChange={ ( newValue ) => | ||
| setLock( { | ||
| move: newValue, | ||
| remove: newValue, | ||
| } ) | ||
| } | ||
| /> | ||
| <ul className="block-editor-block-lock-modal__checklist"> | ||
| <li className="block-editor-block-lock-modal__checklist-item"> | ||
| <CheckboxControl | ||
| label={ | ||
| <> | ||
| { __( 'Disable movement' ) } | ||
| <Icon icon={ dragHandle } /> | ||
| </> | ||
| } | ||
| checked={ lock.move } | ||
| onChange={ ( move ) => | ||
| setLock( ( prevLock ) => ( { | ||
| ...prevLock, | ||
| move, | ||
| } ) ) | ||
| } | ||
| /> | ||
| </li> | ||
| <li className="block-editor-block-lock-modal__checklist-item"> | ||
| <CheckboxControl | ||
| label={ | ||
| <> | ||
| { __( 'Prevent removal' ) } | ||
| <Icon icon={ trash } /> | ||
| </> | ||
| } | ||
| checked={ lock.remove } | ||
| onChange={ ( remove ) => | ||
| setLock( ( prevLock ) => ( { | ||
| ...prevLock, | ||
| remove, | ||
| } ) ) | ||
| } | ||
| /> | ||
| </li> | ||
| </ul> | ||
| </div> | ||
| <Flex | ||
| className="block-editor-block-lock-modal__actions" | ||
| justify="flex-end" | ||
| expanded={ false } | ||
| > | ||
| <FlexItem> | ||
| <Button variant="tertiary" onClick={ onClose }> | ||
| { __( 'Cancel' ) } | ||
| </Button> | ||
| </FlexItem> | ||
| <FlexItem> | ||
| <Button variant="primary" type="submit"> | ||
| { __( 'Apply' ) } | ||
| </Button> | ||
| </FlexItem> | ||
| </Flex> | ||
| </form> | ||
| </Modal> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| .block-editor-block-lock-modal { | ||
| z-index: z-index(".block-editor-block-lock-modal"); | ||
|
|
||
| .components-modal__frame { | ||
| @include break-small() { | ||
| max-width: $break-mobile; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this pattern for defining a max width copied from some place? Maybe we should support defining a width as a prop to avoid recreating the same things over and over in different modals.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is the new recommended max-width for action modals. It might be a good idea to make it default or, as you suggested, allow setting it via prop. cc @jasmussen
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely good to absorb in the mechanics of the modal component to streamline the use and avoid arbitrary values.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm just worried about this introducing a regression for @mirka , do you think we could apply our style deprecation strategy for this?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Are there other notable kinds of modals other than "action" modals? Or can we take this statement to mean that this is the new recommended max-width for basically every modal in the codebase? If it's the latter, we'd probably want to initiate a style deprecation to make this the new default.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also treat these as a "compact" variation you opt-in into and leave the default as the almost full-width one
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even for the template part placeholder modal, I had to tweak the width in a similar way (I copied the code from the preferences modal). I think at the component level we should come up with conventions (props, variations or something like that) that everyone should follow and address all these use-cases. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| .block-editor-block-lock-modal__checklist { | ||
| margin: 0; | ||
| } | ||
|
|
||
| .block-editor-block-lock-modal__options-title { | ||
|
Mamaduka marked this conversation as resolved.
Resolved
|
||
| border-bottom: 1px solid $gray-300; | ||
| padding: $grid-unit-15 0; | ||
|
|
||
| .components-checkbox-control__label { | ||
| font-weight: 600; | ||
| } | ||
|
|
||
| .components-base-control__field { | ||
| align-items: center; | ||
| display: flex; | ||
| margin: 0; | ||
| } | ||
| } | ||
| .block-editor-block-lock-modal__checklist-item { | ||
| border-bottom: 1px solid $gray-300; | ||
| margin-bottom: 0; | ||
| padding: $grid-unit-15 0 $grid-unit-15 $grid-unit-15; | ||
|
|
||
| .components-base-control__field { | ||
| align-items: center; | ||
| display: flex; | ||
| margin: 0; | ||
| } | ||
|
|
||
| .components-checkbox-control__label { | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: space-between; | ||
| flex-grow: 1; | ||
|
|
||
| svg { | ||
| margin-right: $grid-unit-15; | ||
| fill: $gray-900; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| .block-editor-block-lock-modal__actions { | ||
| margin-top: $grid-unit-30; | ||
| } | ||
|
|
||
| .block-editor-block-lock-toolbar { | ||
| .components-button.has-icon { | ||
| min-width: $button-size-small + $grid-unit-15 !important; | ||
| padding-left: 0 !important; | ||
|
|
||
| &:focus::before { | ||
| left: 0 !important; | ||
| right: $grid-unit-15 !important; | ||
|
Mamaduka marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; | ||
| import { useReducer } from '@wordpress/element'; | ||
| import { lock } from '@wordpress/icons'; | ||
| import { useSelect } from '@wordpress/data'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import BlockLockModal from './modal'; | ||
| import useBlockDisplayInformation from '../use-block-display-information'; | ||
| import { store as blockEditorStore } from '../../store'; | ||
|
|
||
| export default function BlockLockToolbar( { clientId } ) { | ||
| const blockInformation = useBlockDisplayInformation( clientId ); | ||
| const { canMove, canRemove } = useSelect( | ||
| ( select ) => { | ||
| const { canMoveBlock, canRemoveBlock } = select( blockEditorStore ); | ||
|
|
||
| return { | ||
| canMove: canMoveBlock( clientId ), | ||
| canRemove: canRemoveBlock( clientId ), | ||
| }; | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
|
|
||
| const [ isModalOpen, toggleModal ] = useReducer( | ||
| ( isActive ) => ! isActive, | ||
| false | ||
| ); | ||
|
|
||
| if ( canMove && canRemove ) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <ToolbarGroup className="block-editor-block-lock-toolbar"> | ||
| <ToolbarButton | ||
| icon={ lock } | ||
| label={ sprintf( | ||
| /* translators: %s: block name */ | ||
| __( 'Unlock %s' ), | ||
| blockInformation.title | ||
| ) } | ||
| onClick={ toggleModal } | ||
| /> | ||
| </ToolbarGroup> | ||
| { isModalOpen && ( | ||
| <BlockLockModal clientId={ clientId } onClose={ toggleModal } /> | ||
| ) } | ||
| </> | ||
| ); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.