-
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
Accessibility: Adding Keyboard Shorcuts to navigate the editor regions #3084
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
17a8573
Accessibility: Adding Keyboard Shorcuts to navigate the editor regions
youknowriad d3ca867
Components: Extract the region navigation into a reusable Higher-Orde…
youknowriad ca0cb25
Accessibility: Use command + ` shortcut for region navigations
youknowriad 90955c5
Accessibility: Using a flashing focus when navigating regions
youknowriad 1372763
Accessibility: Ctrl instead of command to navigate regions
youknowriad b5fc689
iCode Style: Fix mixed indentation style
youknowriad 21070a4
Drop focus animation when navigating regions
youknowriad e893cb0
Fix the editor content region focus style when scrolling
youknowriad 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# navigateRegions | ||
|
||
`navigateRegions` is a React [higher-order component](https://facebook.github.io/react/docs/higher-order-components.html) adding keyboard navigation to switch between the different DOM elements marked as "regions" (role="region"). These regions should be focusable (By adding a tabIndex attribute for example) | ||
|
||
## Example: | ||
|
||
```jsx | ||
function MyLayout() { | ||
return ( | ||
<div> | ||
<div role="region" tabIndex="-1">Header</div> | ||
<div role="region" tabIndex="-1">Content</div> | ||
<div role="region" tabIndex="-1">Sidebar</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default navigateRegions( MyLayout ); | ||
``` |
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,77 @@ | ||
/** | ||
* External Dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress Dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal Dependencies | ||
*/ | ||
import './style.scss'; | ||
import KeyboardShortcuts from '../../keyboard-shortcuts'; | ||
|
||
function navigateRegions( WrappedComponent ) { | ||
return class extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.bindContainer = this.bindContainer.bind( this ); | ||
this.focusNextRegion = this.focusRegion.bind( this, 1 ); | ||
this.focusPreviousRegion = this.focusRegion.bind( this, -1 ); | ||
this.onClick = this.onClick.bind( this ); | ||
this.state = { | ||
isFocusingRegions: false, | ||
}; | ||
} | ||
|
||
bindContainer( ref ) { | ||
this.container = ref; | ||
} | ||
|
||
focusRegion( offset ) { | ||
const regions = [ ...this.container.querySelectorAll( '[role="region"]' ) ]; | ||
if ( ! regions.length ) { | ||
return; | ||
} | ||
let nextRegion = regions[ 0 ]; | ||
const selectedIndex = regions.indexOf( document.activeElement ); | ||
if ( selectedIndex !== -1 ) { | ||
let nextIndex = selectedIndex + offset; | ||
nextIndex = nextIndex === -1 ? regions.length - 1 : nextIndex; | ||
nextIndex = nextIndex === regions.length ? 0 : nextIndex; | ||
nextRegion = regions[ nextIndex ]; | ||
} | ||
|
||
nextRegion.focus(); | ||
this.setState( { isFocusingRegions: true } ); | ||
} | ||
|
||
onClick() { | ||
this.setState( { isFocusingRegions: false } ); | ||
} | ||
|
||
render() { | ||
const className = classnames( 'components-navigate-regions', { | ||
'is-focusing-regions': this.state.isFocusingRegions, | ||
} ); | ||
|
||
// Disable reason: Clicking the editor should dismiss the regions focus style | ||
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */ | ||
return ( | ||
<div ref={ this.bindContainer } className={ className } onClick={ this.onClick }> | ||
<KeyboardShortcuts shortcuts={ { | ||
'ctrl+`': this.focusNextRegion, | ||
'ctrl+shift+`': this.focusPreviousRegion, | ||
} } /> | ||
<WrappedComponent { ...this.props } /> | ||
</div> | ||
); | ||
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */ | ||
} | ||
}; | ||
} | ||
|
||
export default navigateRegions; |
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,12 @@ | ||
.components-navigate-regions.is-focusing-regions [role="region"] { | ||
&:focus:after { | ||
content: ''; | ||
position: absolute; | ||
top: 0; | ||
bottom: 0; | ||
left: 0; | ||
right: 0; | ||
pointer-events: none; | ||
border: 4px solid $blue-medium-400; | ||
} | ||
} |
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
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
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
} | ||
|
||
.editor-layout__content { | ||
position: relative; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
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
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
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.
Looks much better, thanks.