Skip to content
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

Proof of Concept: Improve Block Toolbar Semantics/Accessibility #53779

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5d7bda7
Move Selected Block Tools into the header toolbar in the DOM but pres…
jeryj Aug 9, 2023
c593e05
Render selected block toolbar as a fill within a slot in the header t…
jeryj Aug 17, 2023
4e0c909
Pass isFixed to ContextualToolbar for styling
jeryj Aug 9, 2023
53df7d3
Add block popover to edit site header slot
jeryj Aug 17, 2023
f0da0e8
poc: adjust styles for top toolbar in happy path on desktop and tablet
jeryj Aug 9, 2023
9fc803c
Use SCSS variables for calculating top toolbar height
jeryj Aug 10, 2023
5257349
Remove unnecessary z-index
jeryj Aug 10, 2023
9bd48f9
Show the toolbar as fixed when on tablet breakpoint even if using pop…
jeryj Aug 10, 2023
7fc9bb1
Add classname to selected block tools slot and use bubblesVirtually
jeryj Aug 17, 2023
96b184b
POC: Remove calculated width from toolbar
jeryj Aug 17, 2023
fd31f65
Move lastFocus into redux store
jeryj Aug 25, 2023
c3c76b9
Register global shortcut for moving focus back to the last focused bl…
jeryj Aug 25, 2023
e747b88
Start on escape to return to block selection
jeryj Aug 25, 2023
3b696bc
Add move focus to selected block global shortcut to site editor
jeryj Aug 25, 2023
732fabb
Rename editor focus-editor shortcut
jeryj Aug 25, 2023
9acb93d
Handle Escape on Toolbar Option 1: Add event listeners to all childre…
jeryj Sep 1, 2023
b5f5248
Handle Escape on Toolbar Option 2: Use addEventListener on the select…
jeryj Sep 1, 2023
401cf4c
Handle Escape on Toolbar Option 3: Remove bubblesVirtually from block…
jeryj Sep 1, 2023
25a79f2
POC: DON'T MERGE THIS. Allow tab keypress formatting in rich text fie…
jeryj Sep 6, 2023
384e717
Add inline rich tools to header popover
jeryj Sep 11, 2023
399afc6
Remove z-index hack for using editor block tools in header
jeryj Sep 11, 2023
051fd41
Add menubar role to toolbar group
jeryj Sep 11, 2023
44851dd
Fix accidental duplicate block iserter in site editor document toolbar
jeryj Sep 11, 2023
5fc387c
Make entire header bar menubar role
jeryj Sep 12, 2023
a9b0710
Add focusEditorOnEscape to NavigableToolbar to return focus to editor…
jeryj Sep 12, 2023
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
24 changes: 24 additions & 0 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,18 @@ _Properties_
- _isDisabled_ `boolean`: Whether or not the user should be prevented from inserting this item.
- _frecency_ `number`: Heuristic that combines frequency and recency.

### getLastFocus

Returns the element of the last element that had focus when focus left the editor canvas.

_Parameters_

- _state_ `Object`: Block editor state.

_Returns_

- `Object`: Element.

### getLastMultiSelectedBlockClientId

Returns the client ID of the last block in the multi-selection set, or null if there is no multi-selection.
Expand Down Expand Up @@ -1651,6 +1663,18 @@ _Parameters_
- _clientId_ `string`: The block's clientId.
- _hasControlledInnerBlocks_ `boolean`: True if the block's inner blocks are controlled.

### setLastFocus

Action that sets the element that had focus when focus leaves the editor canvas.

_Parameters_

- _lastFocus_ `Object`: The last focused element.

_Returns_

- `Object`: Action object.

### setNavigationMode

Action that enables or disables the navigation mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function BlockControlsSlot( { group = 'default', ...props } ) {
return null;
}

const slot = <Slot { ...props } bubblesVirtually fillProps={ fillProps } />;
const slot = <Slot { ...props } fillProps={ fillProps } />;

if ( group === 'default' ) {
return slot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
useLayoutEffect,
useEffect,
useRef,
useState,
} from '@wordpress/element';
import { forwardRef, useEffect, useRef, useState } from '@wordpress/element';
import { hasBlockSupport, store as blocksStore } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import {
Expand All @@ -31,7 +26,10 @@ import BlockToolbar from '../block-toolbar';
import { store as blockEditorStore } from '../../store';
import { useHasAnyBlockControls } from '../block-controls/use-has-block-controls';

function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
function UnforwardBlockContextualToolbar(
{ focusOnMount, isFixed, ...props },
ref
) {
// When the toolbar is fixed it can be collapsed
const [ isCollapsed, setIsCollapsed ] = useState( false );
const toolbarButtonRef = useRef();
Expand Down Expand Up @@ -82,77 +80,79 @@ function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
setIsCollapsed( false );
}, [ selectedBlockClientId ] );

const isLargerThanTabletViewport = useViewportMatch( 'large', '>=' );
const isFullscreen =
document.body.classList.contains( 'is-fullscreen-mode' );

useLayoutEffect( () => {
// don't do anything if not fixed toolbar
if ( ! isFixed || ! blockType ) {
return;
}

const blockToolbar = document.querySelector(
'.block-editor-block-contextual-toolbar'
);

if ( ! blockToolbar ) {
return;
}

if ( ! isLargerThanTabletViewport ) {
// set the width of the toolbar to auto
blockToolbar.style = {};
return;
}

if ( isCollapsed ) {
// set the width of the toolbar to auto
blockToolbar.style.width = 'auto';
return;
}

// get the width of the pinned items in the post editor
const pinnedItems = document.querySelector(
'.edit-post-header__settings'
);

// get the width of the left header in the site editor
const leftHeader = document.querySelector(
'.edit-site-header-edit-mode__end'
);

const computedToolbarStyle = window.getComputedStyle( blockToolbar );
const computedPinnedItemsStyle = pinnedItems
? window.getComputedStyle( pinnedItems )
: false;
const computedLeftHeaderStyle = leftHeader
? window.getComputedStyle( leftHeader )
: false;

const marginLeft = parseFloat( computedToolbarStyle.marginLeft );
const pinnedItemsWidth = computedPinnedItemsStyle
? parseFloat( computedPinnedItemsStyle.width )
: 0;
const leftHeaderWidth = computedLeftHeaderStyle
? parseFloat( computedLeftHeaderStyle.width )
: 0;

// set the new witdth of the toolbar
blockToolbar.style.width = `calc(100% - ${
leftHeaderWidth +
pinnedItemsWidth +
marginLeft +
( pinnedItems || leftHeader ? 2 : 0 ) + // Prevents button focus border from being cut off
( isFullscreen ? 0 : 160 ) // the width of the admin sidebar expanded
}px)`;
}, [
isFixed,
isLargerThanTabletViewport,
isCollapsed,
isFullscreen,
blockType,
] );
// TODO: Do we need all of this width calculation??

// const isLargerThanTabletViewport = useViewportMatch( 'large', '>=' );
// const isFullscreen =
// document.body.classList.contains( 'is-fullscreen-mode' );

// useLayoutEffect( () => {
// // don't do anything if not fixed toolbar
// if ( ! isFixed || ! blockType ) {
// return;
// }

// const blockToolbar = document.querySelector(
// '.block-editor-block-contextual-toolbar'
// );

// if ( ! blockToolbar ) {
// return;
// }

// if ( ! isLargerThanTabletViewport ) {
// // set the width of the toolbar to auto
// blockToolbar.style = {};
// return;
// }

// if ( isCollapsed ) {
// // set the width of the toolbar to auto
// blockToolbar.style.width = 'auto';
// return;
// }

// // get the width of the pinned items in the post editor
// const pinnedItems = document.querySelector(
// '.edit-post-header__settings'
// );

// // get the width of the left header in the site editor
// const leftHeader = document.querySelector(
// '.edit-site-header-edit-mode__end'
// );

// const computedToolbarStyle = window.getComputedStyle( blockToolbar );
// const computedPinnedItemsStyle = pinnedItems
// ? window.getComputedStyle( pinnedItems )
// : false;
// const computedLeftHeaderStyle = leftHeader
// ? window.getComputedStyle( leftHeader )
// : false;

// const marginLeft = parseFloat( computedToolbarStyle.marginLeft );
// const pinnedItemsWidth = computedPinnedItemsStyle
// ? parseFloat( computedPinnedItemsStyle.width ) + 10 // 10 is the pinned items padding
// : 0;
// const leftHeaderWidth = computedLeftHeaderStyle
// ? parseFloat( computedLeftHeaderStyle.width )
// : 0;

// // set the new witdth of the toolbar
// blockToolbar.style.width = `calc(100% - ${
// leftHeaderWidth +
// pinnedItemsWidth +
// marginLeft +
// ( pinnedItems || leftHeader ? 2 : 0 ) + // Prevents button focus border from being cut off
// ( isFullscreen ? 0 : 160 ) // the width of the admin sidebar expanded
// }px)`;
// }, [
// isFixed,
// isLargerThanTabletViewport,
// isCollapsed,
// isFullscreen,
// blockType,
// ] );

const isToolbarEnabled =
! blockType ||
Expand All @@ -174,6 +174,7 @@ function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {

return (
<NavigableToolbar
ref={ ref }
focusOnMount={ focusOnMount }
className={ classes }
/* translators: accessibility text for the block toolbar */
Expand Down Expand Up @@ -209,4 +210,4 @@ function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
);
}

export default BlockContextualToolbar;
export default forwardRef( UnforwardBlockContextualToolbar );
Loading