react-self-focused v0.2.0
react-self-focused
Making React Applications’ UI Transitions Screen Reader Friendly.
When UI transitions happen in a SPA (or in any dynamic web application), there is visual feedback; however, for users of screen reading software, there is no spoken feedback by default. Traditionally, screen reading software automatically reads out the content of a web page during page load or page refresh. In single page applications, the UI transitions happen by rewriting the current page, rather than loading entirely new pages from a server; this makes it difficult for screen reading software users to be aware of UI changes (e.g., clicking on the navigation bar to load a new route).
If the corresponding HTML node of the dynamic content can be focused programmatically, screen reading software will start speaking the textual content of that node. Focusing the corresponding HTML node of the dynamic content can be considered guided focus management. Not only will it facilitate the announcement of the textual content of the focused HTML node, but it will also serve as a guided context switch. Any subsequent “tab” key press will focus the next focusable element within/after this context. However, keeping track of the HTML nodes to be focused can be tedious, repetitive, and error-prone since there could be hundreds of nodes that need to be programmatically focused in a SPA.
For react applications, this component solves the problem.

Installation
npm i react-self-focused -SUsage
Wrap all the routable components by self-focused.
import SelfFocused from 'react-self-focused';
<SelfFocused>
<!-- block to be rendered -->
</SelfFocused>Since the div will be focused, it will have a focus outline/highlight, if that is not desired, please add the following styles:
.self-focused:focus {
outline: none
}Implementation overview
self-focusedcomponent on initial render invokes thecomponentDidMountand on re-render invokes thecomponentDidUpdatemethod of thefocus-managerrespectively passing the self HTML node as argument .focus-managercarries out the functionality of focusing the desired node.focus-managerutilizes two state variables, namelyisFirstRenderandnodeToBeFocused.- initial value of the
isFirstRenderis set totrue - initial value of the
nodeToBeFocusedis set tonull
- initial value of the
focus-manageron initialization schedulesisFirstRenderto be set tofalsein the withrequestAnimationFrame().focus-managerhas two private methods namelysetFocusandremoveTabIndex.setFocusmethod- adds
tabindex=-1to thenodeToBeFocused - invokes native
focus()method on it - attaches
removeTabIndexmethod to thenodeToBeFocusedas theclickandblurevent handler - sets
nodeToBeFocusedtonull
- adds
removeTabIndexmethod, removes thetabindex,clickandblurevent handlers fromnodeToBeFocused
focus-managerexposes two methods, namelycomponentDidMountandcomponentDidUpdate, which are consumed byself-focusedcomponent.componentDidMountandcomponentDidUpdateboth accept a HTML node as an argument.componentDidMountandcomponentDidUpdateboth bail out ifisFirstRenderis true.- for
componentDidMountthe very lastself-focuseddiv passed to it for the render cycle wins - for
componentDidUpdatethe very firstself-focuseddiv passed tocomponentDidMountfor the render cycle wins, if and only ifnodeToBeFocusedwas null when this method was invoked. componentDidMountandcomponentDidUpdateboth schedule the privatesetFocusmethod, in theafterRenderqueue after ifnodeToBeFocusedwas updated.
Contributing
Running tests
npm t
Running the example application
npm start- Visit the example application at http://localhost:8080.
License
This project is licensed under the BSD-2-Clause License.