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 1 commit
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
Prev Previous commit
Add focusEditorOnEscape to NavigableToolbar to return focus to editor…
… by default on escape keypress

Not committed to this, but it does work since we've removed bubblesVirtually from the toolbar slots and the inline rich editor doesn't use slots for its tools. I like that this brings this behavior by default to the toolbars, but unsure about if it's a good idea overall.
  • Loading branch information
jeryj committed Sep 12, 2023
commit a9b07109a01d4c3a2696d776f853ba8630aca5b2
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
} from '@wordpress/components';
import { next, previous } from '@wordpress/icons';
import { useViewportMatch } from '@wordpress/compose';
import { ESCAPE } from '@wordpress/keycodes';

/**
* Internal dependencies
Expand All @@ -39,15 +38,13 @@ function UnforwardBlockContextualToolbar(
const {
blockType,
blockEditingMode,
lastFocus,
hasParents,
showParentSelector,
selectedBlockClientId,
} = useSelect( ( select ) => {
const {
getBlockName,
getBlockParents,
getLastFocus,
getSelectedBlockClientIds,
getBlockEditingMode,
} = select( blockEditorStore );
Expand All @@ -65,7 +62,6 @@ function UnforwardBlockContextualToolbar(
_selectedBlockClientId &&
getBlockType( getBlockName( _selectedBlockClientId ) ),
blockEditingMode: getBlockEditingMode( _selectedBlockClientId ),
lastFocus: getLastFocus(),
hasParents: parents.length,
showParentSelector:
parentBlockType &&
Expand Down Expand Up @@ -183,12 +179,6 @@ function UnforwardBlockContextualToolbar(
className={ classes }
/* translators: accessibility text for the block toolbar */
aria-label={ __( 'Block tools' ) }
handleOnKeyDown={ ( event ) => {
if ( event.keyCode === ESCAPE && lastFocus?.current ) {
event.preventDefault();
lastFocus.current.focus();
}
} }
{ ...props }
>
{ ! isCollapsed && <BlockToolbar hideDragHandle={ isFixed } /> }
Expand Down
40 changes: 40 additions & 0 deletions packages/block-editor/src/components/navigable-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import {
useEffect,
useCallback,
} from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import { focus } from '@wordpress/dom';
import { ESCAPE } from '@wordpress/keycodes';
import { useShortcut } from '@wordpress/keyboard-shortcuts';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';

function hasOnlyToolbarItem( elements ) {
const dataProp = 'toolbarItem';
return ! elements.some( ( element ) => ! ( dataProp in element.dataset ) );
Expand Down Expand Up @@ -165,6 +172,7 @@ function UnforwardNavigableToolbar(
shouldUseKeyboardFocusShortcut = true,
__experimentalInitialIndex: initialIndex,
__experimentalOnIndexChange: onIndexChange,
focusEditorOnEscape = true,
...props
},
ref
Expand All @@ -182,6 +190,13 @@ function UnforwardNavigableToolbar(
shouldUseKeyboardFocusShortcut
);

const { lastFocus } = useSelect( ( select ) => {
const { getLastFocus } = select( blockEditorStore );
return {
lastFocus: getLastFocus(),
};
}, [] );

useEffect( () => {
const navigableToolbarRef = toolbarRef.current;

Expand All @@ -201,6 +216,31 @@ function UnforwardNavigableToolbar(
}
}, [ handleOnKeyDown, toolbarRef ] );


// TODO: Not sure if this is a good idea... but it's working for now and gets the behavior we're after with the fewest lines of code, but also is limiting.
useEffect( () => {
const navigableToolbarRef = toolbarRef.current;

if ( focusEditorOnEscape ) {
const handleKeyDown = ( event ) => {
if ( event.keyCode === ESCAPE && lastFocus?.current ) {
// Focus the last focused element when pressing escape.
event.preventDefault();
lastFocus.current.focus();
}
};

navigableToolbarRef.addEventListener( 'keydown', handleKeyDown );

return () => {
navigableToolbarRef.removeEventListener(
'keydown',
handleKeyDown
);
};
}
}, [ focusEditorOnEscape, toolbarRef, lastFocus ] );

if ( isAccessibleToolbar ) {
return (
<Toolbar
Expand Down