npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-self-focused

v0.2.0

Published

make a react application screen reader friendly

Downloads

4

Readme

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.

react-self-focused

Installation

npm i react-self-focused -S

Usage

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-focused component on initial render invokes the componentDidMount and on re-render invokes the componentDidUpdate method of the focus-manager respectively passing the self HTML node as argument .
  • focus-manager carries out the functionality of focusing the desired node.
    • focus-manager utilizes two state variables, namely isFirstRender and nodeToBeFocused.
      • initial value of the isFirstRender is set to true
      • initial value of the nodeToBeFocused is set to null
    • focus-manager on initialization schedules isFirstRender to be set to falsein the with requestAnimationFrame().
    • focus-manager has two private methods namely setFocus and removeTabIndex.
      • setFocus method
        • adds tabindex=-1 to the nodeToBeFocused
        • invokes native focus() method on it
        • attaches removeTabIndex method to the nodeToBeFocused as the click and blur event handler
        • sets nodeToBeFocused to null
      • removeTabIndex method, removes the tabindex, click and blur event handlers from nodeToBeFocused
    • focus-manager exposes two methods, namely componentDidMount and componentDidUpdate, which are consumed by self-focused component.
      • componentDidMount and componentDidUpdate both accept a HTML node as an argument.
      • componentDidMount and componentDidUpdate both bail out if isFirstRender is true.
      • for componentDidMount the very last self-focused div passed to it for the render cycle wins
      • for componentDidUpdate the very first self-focused div passed to componentDidMount for the render cycle wins, if and only if nodeToBeFocused was null when this method was invoked.
      • componentDidMount and componentDidUpdate both schedule the private setFocus method, in the afterRender queue after if nodeToBeFocused was updated.

Contributing

Running tests

  • npm t

Running the example application

License

This project is licensed under the BSD-2-Clause License.