-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Improve block inserter keyboard navigation #26938
Merged
Merged
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0df4171
Try new block inserter keyboard navigation
diegohaz 4f906dd
Merge branch 'master' into try/block-inserter-keyboard-navigation
diegohaz 13d209d
Refactor code
diegohaz 5c71072
Merge branch 'master' into try/block-inserter-keyboard-navigation
diegohaz 543ef7c
Pass aria-orientation to listbox
diegohaz ce34a54
Merge branch 'master' into try/block-inserter-keyboard-navigation
diegohaz eca11f9
Update package.json
diegohaz fcd6573
Address review comments
diegohaz bcc8d85
Merge branch 'master' into try/block-inserter-keyboard-navigation
diegohaz 3155cee
Add announcement
diegohaz dc52e50
Merge branch 'master' into try/block-inserter-keyboard-navigation
diegohaz eddebc4
Merge branch 'master' into try/block-inserter-keyboard-navigation
diegohaz 8518ec3
Merge branch 'master' into try/block-inserter-keyboard-navigation
diegohaz 40a0043
Import composite from @wordpress/components
diegohaz 2f7889a
Fix import names
diegohaz 1b1d02f
Merge branch 'master' into try/block-inserter-keyboard-navigation
diegohaz cb7b4d7
Fix keyboard navigation on search results
diegohaz 454a69f
Fix e2e test
diegohaz c0d52d1
Announce instructions after block and group name
diegohaz 5f3bc81
Merge branch 'trunk' into try/block-inserter-keyboard-navigation
diegohaz cfbf51b
Merge branch 'trunk' into try/block-inserter-keyboard-navigation
diegohaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
packages/block-editor/src/components/block-types-list/style.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/block-editor/src/components/inserter-listbox/context.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext } from '@wordpress/element'; | ||
|
||
const InserterListboxContext = createContext(); | ||
|
||
export default InserterListboxContext; |
40 changes: 40 additions & 0 deletions
40
packages/block-editor/src/components/inserter-listbox/group.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { forwardRef, useEffect, useState } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { speak } from '@wordpress/a11y'; | ||
|
||
function InserterListboxGroup( props, ref ) { | ||
const [ shouldSpeak, setShouldSpeak ] = useState( false ); | ||
|
||
useEffect( () => { | ||
if ( shouldSpeak ) { | ||
speak( | ||
__( 'Use left and right arrow keys to move through blocks' ) | ||
); | ||
} | ||
}, [ shouldSpeak ] ); | ||
|
||
return ( | ||
<div | ||
ref={ ref } | ||
role="listbox" | ||
aria-orientation="horizontal" | ||
onFocus={ () => { | ||
setShouldSpeak( true ); | ||
} } | ||
onBlur={ ( event ) => { | ||
const focusingOutsideGroup = ! event.currentTarget.contains( | ||
event.relatedTarget | ||
); | ||
if ( focusingOutsideGroup ) { | ||
setShouldSpeak( false ); | ||
} | ||
} } | ||
{ ...props } | ||
/> | ||
); | ||
} | ||
|
||
export default forwardRef( InserterListboxGroup ); |
27 changes: 27 additions & 0 deletions
27
packages/block-editor/src/components/inserter-listbox/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __unstableUseCompositeState as useCompositeState } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import InserterListboxContext from './context'; | ||
|
||
export { default as InserterListboxGroup } from './group'; | ||
export { default as InserterListboxRow } from './row'; | ||
export { default as InserterListboxItem } from './item'; | ||
|
||
function InserterListbox( { children } ) { | ||
const compositeState = useCompositeState( { | ||
shift: true, | ||
wrap: 'horizontal', | ||
} ); | ||
return ( | ||
<InserterListboxContext.Provider value={ compositeState }> | ||
{ children } | ||
</InserterListboxContext.Provider> | ||
); | ||
} | ||
|
||
export default InserterListbox; |
52 changes: 52 additions & 0 deletions
52
packages/block-editor/src/components/inserter-listbox/item.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
Button, | ||
__unstableCompositeItem as CompositeItem, | ||
} from '@wordpress/components'; | ||
import { forwardRef, useContext } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import InserterListboxContext from './context'; | ||
|
||
function InserterListboxItem( | ||
{ isFirst, as: Component, children, ...props }, | ||
ref | ||
) { | ||
const state = useContext( InserterListboxContext ); | ||
return ( | ||
<CompositeItem | ||
ref={ ref } | ||
state={ state } | ||
role="option" | ||
// Use the CompositeItem `focusable` prop over Button's | ||
// isFocusable. The latter was shown to cause an issue | ||
// with tab order in the inserter list. | ||
focusable | ||
{ ...props } | ||
> | ||
{ ( htmlProps ) => { | ||
const propsWithTabIndex = { | ||
...htmlProps, | ||
tabIndex: isFirst ? 0 : htmlProps.tabIndex, | ||
}; | ||
if ( Component ) { | ||
return ( | ||
<Component { ...propsWithTabIndex }> | ||
{ children } | ||
</Component> | ||
); | ||
} | ||
if ( typeof children === 'function' ) { | ||
return children( propsWithTabIndex ); | ||
} | ||
return <Button { ...propsWithTabIndex }>{ children }</Button>; | ||
} } | ||
</CompositeItem> | ||
); | ||
} | ||
|
||
export default forwardRef( InserterListboxItem ); |
24 changes: 24 additions & 0 deletions
24
packages/block-editor/src/components/inserter-listbox/row.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { forwardRef, useContext } from '@wordpress/element'; | ||
import { __unstableCompositeGroup as CompositeGroup } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import InserterListboxContext from './context'; | ||
|
||
function InserterListboxRow( props, ref ) { | ||
const state = useContext( InserterListboxContext ); | ||
return ( | ||
<CompositeGroup | ||
state={ state } | ||
role="presentation" | ||
ref={ ref } | ||
{ ...props } | ||
/> | ||
); | ||
} | ||
|
||
export default forwardRef( InserterListboxRow ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InserterListItem
isn't exposed as part of the public API so it's fine to change 👍🏻