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
1 change: 1 addition & 0 deletions packages/base-styles/_z-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ $z-layers: (
// Should be above the popover (dropdown)
".reusable-blocks-menu-items__convert-modal": 1000001,
".edit-site-create-template-part-modal": 1000001,
".block-editor-block-lock-modal": 1000001,

// Note: The ConfirmDialog component's z-index is being set to 1000001 in packages/components/src/confirm-dialog/styles.ts
// because it uses emotion and not sass. We need it to render on top its parent popover.
Expand Down
3 changes: 3 additions & 0 deletions packages/block-editor/src/components/block-lock/index.js
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';
52 changes: 52 additions & 0 deletions packages/block-editor/src/components/block-lock/menu-item.js
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 } />
) }
</>
);
}
165 changes: 165 additions & 0 deletions packages/block-editor/src/components/block-lock/modal.js
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( () => {
Comment thread
Mamaduka marked this conversation as resolved.
Resolved
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 }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

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.

Thanks for the feedback, @alexstine. I didn't know the indeterminate attribute.

I wonder if this needs to be supported by the CheckboxControl component itself.

cc @ciampo, @mirka

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

https://github.com/WordPress/gutenberg/blob/trunk/packages/components/src/checkbox-control/index.js#L54

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.

According to MDN:

This is set using the HTMLInputElement object's indeterminate property via JavaScript (it cannot be set using an HTML attribute).

So passing it as prop/attribute won't work.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 role="checkbox"?

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.

It's possible to support this feature in custom components. Do you mind creating a new issue for indeterminate support?

What happens if you add role="checkbox"?

I assumed inputs with type="checkbox" would have this role automatically assigned.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

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.

For now, how hard would it be to convert this to radio buttons?

Sorry if I'm missing something here, but how would this help us?

There's at least one more place that uses aria-checked="mixed" and would benefit from the CheckboxControl component. So maybe we should go with component enhancement instead of hacking our way around.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?
Radio buttons:

  • Lock block removal.
  • Do not lock block removal.

Different than the original design but it would meet A11Y needs.

Just another option if the checkbox feature takes too long.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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>
);
}
67 changes: 67 additions & 0 deletions packages/block-editor/src/components/block-lock/style.scss
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

@Mamaduka Mamaduka Mar 15, 2022 •

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.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

@ciampo ciampo Mar 15, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm just worried about this introducing a regression for Modal users that needed a different maxwidth`.

@mirka , do you think we could apply our style deprecation strategy for this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is the new recommended max-width for action modals

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 {
Comment thread
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;
Comment thread
Mamaduka marked this conversation as resolved.
Resolved
}
}
}
58 changes: 58 additions & 0 deletions packages/block-editor/src/components/block-lock/toolbar.js
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 } />
) }
</>
);
}
Loading