Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Add accessibleFocus utility.
This gives us the ability to augment focused styles and make them
highly visible without affecting input devices that don't navigate
around the UI using the keyboard.
  • Loading branch information
mtias committed May 19, 2017
commit 4baa54c4a360c7d4846738a8ad34b0e83f3b0b85
2 changes: 1 addition & 1 deletion components/icon-button/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
}

&:focus:before {
.has-accessible-focus &:focus:before {
content: '';
position: absolute;
top: 0;
Expand Down
2 changes: 1 addition & 1 deletion components/toolbar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
margin-left: 3px;
}

&:focus:before {
.has-accessible-focus &:focus:before {
top: -4px;
right: -4px;
bottom: -4px;
Expand Down
24 changes: 24 additions & 0 deletions editor/accessible-focus/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// keyCodes for tab, space, left, up, right, down respectively
const keyboardNavigationKeycodes = [ 9, 32, 37, 38, 39, 40 ];
let keyboardNavigation = false;

function accessibleFocus() {
document.addEventListener( 'keydown', function( event ) {
if ( keyboardNavigation ) {
return;
}
if ( keyboardNavigationKeycodes.indexOf( event.keyCode ) !== -1 ) {
keyboardNavigation = true;
document.documentElement.classList.add( 'has-accessible-focus' );
}
} );
document.addEventListener( 'mouseup', function() {
if ( ! keyboardNavigation ) {
return;
}
keyboardNavigation = false;
document.documentElement.classList.remove( 'has-accessible-focus' );
} );
}

export default accessibleFocus;
2 changes: 1 addition & 1 deletion editor/block-switcher/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
margin: 3px;
padding: 6px;

&:focus:before {
.has-accessible-focus &:focus:before {
top: -3px;
right: -3px;
bottom: -3px;
Expand Down
3 changes: 3 additions & 0 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Provider as SlotFillProvider } from 'react-slot-fill';
import './assets/stylesheets/main.scss';
import Layout from './layout';
import { createReduxStore } from './state';
import accessibleFocus from './accessible-focus';

/**
* Initializes and returns an instance of Editor.
Expand All @@ -25,6 +26,8 @@ export function createEditorInstance( id, post ) {
blocks: wp.blocks.parse( post.content.raw ),
} );

accessibleFocus();

wp.element.render(
<ReduxProvider store={ store }>
<SlotFillProvider>
Expand Down