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-selffocus-element

v1.0.0

Published

A react component to focus on an element for accessibility.

Downloads

85

Readme

react-selffocus-element A project badge featuring the A11Y logo and the text "Built for Accessibility"

Build Status Coverage Status PRs Welcome GitHub issues

NPM

A React component to focus on an element when it is mounted. This is essential for seeking attention on a particular element. This can also help in getting attention of screen readers like VoiceOver, JAWS, NVDA, and friends.

Purpose

As soon there is some user event (e.g click) which cause rendering of a section on React app. For eg, a nav link may cause a subsection to be mounted. At few instances and to help a11y, that particular section might require focus to seek user attention.

In case of a11y, for section that are not role=document and do not contain heading section fail to get screen-reader's attention and are ignored until user manually tabs over them. Such case required a user focus, react-selffocus-element helps in solving this for react based apps.

For few scenarios, aria-live or alert does not make sense to seek attention for screen reader because that may not be same control. e.g. Seeking a focus on list or tree item helps them navigate as their respective roles without making them alert.

Install

$ npm install react-selffocus-element --save

or

yarn add react-selffocus-element

Example

  1. <SelfFocus>

    import SelfFocus from 'react-selffocus-element';
    
    ...
    render(){
        return (
            <SelfFocus>
                This will only be content that will be focused on component mount.
            </SelfFocus>
        )
    }
    ...

    This is render div(by default) tag with autofocus. This element will also be focus-able by default.

    Rendered DOM

    <div tabindex="0" >This will only be content that will be focused on component mount.</div>
  2. With Custom Tag and TabIndex

    import SelfFocus from 'react-selffocus-element';
    
    ...
    render(){
        return (
            <SelfFocus tag="p" tabIndex={-1}>
                This will only be content for custom tag and will be focused on component mount.
            </SelfFocus>
        )
    }
    ...

    This is render p(tag prop) tag with autofocus. This element will be focus-able based on tabIndex prop. It is recommended that value of this prop should be 0 (natural tab order) or -1 (not tabbable).

    Rendered DOM

     <p tabindex="0" >This will only be content for custom tag and will be focused on component mount.</div>

APIs

SelfFocus Component

Import mechanism

import SelfFocus from 'react-selffocus-element'

Properties

| prop | type | description | default value | | ------------------ | --------------- | ------------------------------------------- | ------------- | | children (default) | -- | Inner children for selfFocus Component | null | | tag | htmlTag(String) | Component/Node to be rendered for focussing | div | | className | string | additional Classname for particular div | <empty> | | tabIndex | string/number | tabbable order - 0/-1 | 0 |

FAQ

  1. I do not see focused element with outline. How can it be controlled?

    One should use additional custom css to achieve outline, which is normally in this form,

    *:focus {
        outline-style: auto !important;
        outline: auto !important;
        outline-color: #2793f8 !important;
    }

    Also note that outline behavior for screen reader will also rely on screen reader and browser ( for eg, on electron running on window will be default render yellow border unless overwritten by css)

  2. Should I use it for form input tag?

    This component can be used for input tags but default autoFocus prop support provided by React should be used in conjunction with input tags. This will help browser functionalities to work as per focus specifications.

  1. What about role and aria-\* attributes for that elements

    You can specify role and all aria-* attributes on SelfFocus component and would be available on parent element.

    e.g.

    <SelfFocus tag="p" role="alert">

    This will render a p tag with role as alert

  2. What about other props that my component requires?

    You can pass any key-value prop to SelfFocus and it will be rendered on main parent element. This is also how aria-* and role is supported.

  3. Does this work on ComponentDidUpdate?

    No. There is no use case of focusing again on element after some state/prop change. In addition, there may be componentDidUpdate functions triggered when it does not require focusing. Hence, it is not currently supported.

License

Open Source Love

Refer LICENSE file in this repository.