Skip to content

Commit

Permalink
Make the site editor header toolbar use the ARIA toolbar pattern (#41902
Browse files Browse the repository at this point in the history
)

* Implement site editor header toolbar as ARIA toolbar.

* Adjust site header toolbar style.
  • Loading branch information
afercia committed Jun 28, 2022
1 parent 9603bfb commit 5ea47b7
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 17 deletions.
46 changes: 36 additions & 10 deletions packages/edit-site/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useViewportMatch } from '@wordpress/compose';
import {
ToolSelector,
__experimentalPreviewOptions as PreviewOptions,
NavigableToolbar,
} from '@wordpress/block-editor';
import { useSelect, useDispatch } from '@wordpress/data';
import { PinnedItems } from '@wordpress/interface';
Expand Down Expand Up @@ -107,47 +108,72 @@ export default function Header( {

return (
<div className="edit-site-header">
<div className="edit-site-header_start">
<NavigableToolbar
className="edit-site-header_start"
aria-label={ __( 'Document tools' ) }
>
<div className="edit-site-header__toolbar">
<Button
<ToolbarItem
ref={ inserterButton }
as={ Button }
className="edit-site-header-toolbar__inserter-toggle"
variant="primary"
isPressed={ isInserterOpen }
className="edit-site-header-toolbar__inserter-toggle"
disabled={ ! isVisualMode }
onMouseDown={ preventDefault }
onClick={ openInserter }
disabled={ ! isVisualMode }
icon={ plus }
/* translators: button label text should, if possible, be under 16
characters. */
label={ _x(
'Toggle block inserter',
'Generic label for block inserter button'
) }
showTooltip={ ! showIconLabels }
>
{ showIconLabels &&
( ! isInserterOpen ? __( 'Add' ) : __( 'Close' ) ) }
</Button>
</ToolbarItem>
{ isLargeViewport && (
<>
<ToolbarItem
as={ ToolSelector }
showTooltip={ ! showIconLabels }
variant={
showIconLabels ? 'tertiary' : undefined
}
disabled={ ! isVisualMode }
/>
<UndoButton />
<RedoButton />
<Button
<ToolbarItem
as={ UndoButton }
showTooltip={ ! showIconLabels }
variant={
showIconLabels ? 'tertiary' : undefined
}
/>
<ToolbarItem
as={ RedoButton }
showTooltip={ ! showIconLabels }
variant={
showIconLabels ? 'tertiary' : undefined
}
/>
<ToolbarItem
as={ Button }
className="edit-site-header-toolbar__list-view-toggle"
disabled={ ! isVisualMode }
icon={ listView }
disabled={ ! isVisualMode }
isPressed={ isListViewOpen }
/* translators: button label text should, if possible, be under 16 characters. */
label={ __( 'List View' ) }
onClick={ toggleListView }
shortcut={ listViewShortcut }
showTooltip={ ! showIconLabels }
/>
</>
) }
</div>
</div>
</NavigableToolbar>

<div className="edit-site-header_center">
<DocumentActions
Expand Down
58 changes: 53 additions & 5 deletions packages/edit-site/src/components/header/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ $header-toolbar-min-width: 335px;
@include reduce-motion("transition");
}

.edit-site-header_start,
.edit-site-header_start {
display: flex;
border: none;
}

.edit-site-header_end {
display: flex;
justify-content: flex-end;
}

.edit-site-header_center {
Expand All @@ -33,10 +38,6 @@ $header-toolbar-min-width: 335px;
// See https://dev.w3.org/csswg/css-flexbox/#min-size-auto
min-width: 0;
}

.edit-site-header_end {
justify-content: flex-end;
}
}

// Keeps the document title centered when the sidebar is open
Expand Down Expand Up @@ -152,6 +153,49 @@ body.is-navigation-sidebar-open {
}
}

.edit-site-header_start {
display: flex;
border: none;

// The Toolbar component adds different styles to buttons, so we reset them
// here to the original button styles
.edit-site-header__toolbar > .components-button.has-icon,
.edit-site-header__toolbar > .components-dropdown > .components-button.has-icon {
height: $button-size;
min-width: $button-size;
padding: 6px;

&.is-pressed {
background: $gray-900;
}

&:focus:not(:disabled) {
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color), inset 0 0 0 $border-width $white;
outline: 1px solid transparent;
}

&::before {
display: none;
}
}

.edit-site-header__toolbar > .edit-site-header-toolbar__inserter-toggle.has-icon {
margin-right: $grid-unit-10;
// Special dimensions for this button.
min-width: 32px;
width: 32px;
height: 32px;
padding: 0;
}

.edit-site-header__toolbar > .edit-site-header-toolbar__inserter-toggle.has-text.has-icon {
width: auto;
padding: 0 $grid-unit-10;
}


}

// Button text label styles

.show-icon-labels .edit-site-header {
Expand Down Expand Up @@ -192,4 +236,8 @@ body.is-navigation-sidebar-open {
height: 36px;
padding: 0 6px;
}

.edit-site-header_start .edit-site-header__toolbar > * + * {
margin-left: $grid-unit-10;
}
}
7 changes: 6 additions & 1 deletion packages/edit-site/src/components/header/undo-redo/redo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { redo as redoIcon, undo as undoIcon } from '@wordpress/icons';
import { displayShortcut } from '@wordpress/keycodes';
import { store as coreStore } from '@wordpress/core-data';
import { forwardRef } from '@wordpress/element';

export default function RedoButton() {
function RedoButton( props, ref ) {
const hasRedo = useSelect(
( select ) => select( coreStore ).hasRedo(),
[]
);
const { redo } = useDispatch( coreStore );
return (
<Button
{ ...props }
ref={ ref }
icon={ ! isRTL() ? redoIcon : undoIcon }
label={ __( 'Redo' ) }
shortcut={ displayShortcut.primaryShift( 'z' ) }
Expand All @@ -27,3 +30,5 @@ export default function RedoButton() {
/>
);
}

export default forwardRef( RedoButton );
7 changes: 6 additions & 1 deletion packages/edit-site/src/components/header/undo-redo/undo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { undo as undoIcon, redo as redoIcon } from '@wordpress/icons';
import { displayShortcut } from '@wordpress/keycodes';
import { store as coreStore } from '@wordpress/core-data';
import { forwardRef } from '@wordpress/element';

export default function UndoButton() {
function UndoButton( props, ref ) {
const hasUndo = useSelect(
( select ) => select( coreStore ).hasUndo(),
[]
);
const { undo } = useDispatch( coreStore );
return (
<Button
{ ...props }
ref={ ref }
icon={ ! isRTL() ? undoIcon : redoIcon }
label={ __( 'Undo' ) }
shortcut={ displayShortcut.primary( 'z' ) }
Expand All @@ -27,3 +30,5 @@ export default function UndoButton() {
/>
);
}

export default forwardRef( UndoButton );

0 comments on commit 5ea47b7

Please sign in to comment.