An accessible visibility React component

In Cache-Cache CSS (translated by yours truly on this very blog as CSS hide-and-seek), Gaël Poupard offers a bulletproof solution to visually hide some content while keeping it accessible to assistive technologies.

In this post, I want to show a teeny-tiny React component to make it more explicit and convenient to use the original utility class.

const VisuallyHidden = ({ as: Component, ...props }) => (
Component {...props} className="sr-only" />
)

VisuallyHidden.defaultProps = {
as: 'span'
}

And here is how you would use it (taking the example from Accessible page title in a single-page React application).

const TitleAnnouncer = props => {
const [title, setTitle] = React.useState('')
// More React code…

return VisuallyHidden as='p' tabIndex={-1}>{title}VisuallyHidden>
}

A few comments about the component:

Depending on the way you author styles in your application, you could author the relevant styles differently (pure CSS, inline styles, CSS-in-JS…).

The as prop is intended to provide a way to change the underlying DOM element that is rendered. We found that span is a good default in most cases, but you might want a p (like we do in our example), a div or something else.

Finally, we spread the props so that it is possible to pass other DOM attributes to the underlying element (e.g. tabIndex). Note that we spread before the className prop so we don’t inadvertently override it.

Feel free to play with the code on CodeSandbox.

In Cache-Cache CSS (translated by yours truly on this very blog as CSS hide-and-seek), Gaël Poupard offers a bulletproof solution to visually hide some content while keeping it accessible to assistive technologies.

In this post, I want to show a teeny-tiny React component to make it more explicit and convenient to use the original utility class.

const VisuallyHidden = ({ as: Component, ...props }) => (
<Component {...props} className="sr-only" />
)

VisuallyHidden.defaultProps = {
as: 'span'
}

And here is how you would use it (taking the example from Accessible page title in a single-page React application).

const TitleAnnouncer = props => {
const [title, setTitle] = React.useState('')
// More React code…

return <VisuallyHidden as='p' tabIndex={-1}>{title}VisuallyHidden>
}

A few comments about the component:

Depending on the way you author styles in your application, you could author the relevant styles differently (pure CSS, inline styles, CSS-in-JS…).

The as prop is intended to provide a way to change the underlying DOM element that is rendered. We found that span is a good default in most cases, but you might want a p (like we do in our example), a div or something else.

Finally, we spread the props so that it is possible to pass other DOM attributes to the underlying element (e.g. tabIndex). Note that we spread before the className prop so we don’t inadvertently override it.

Feel free to play with the code on CodeSandbox.


Print Share Comment Cite Upload Translate
APA
Hugo “Kitty” Giraudel | Sciencx (2024-03-28T09:53:21+00:00) » An accessible visibility React component. Retrieved from https://www.scien.cx/2020/01/16/an-accessible-visibility-react-component/.
MLA
" » An accessible visibility React component." Hugo “Kitty” Giraudel | Sciencx - Thursday January 16, 2020, https://www.scien.cx/2020/01/16/an-accessible-visibility-react-component/
HARVARD
Hugo “Kitty” Giraudel | Sciencx Thursday January 16, 2020 » An accessible visibility React component., viewed 2024-03-28T09:53:21+00:00,<https://www.scien.cx/2020/01/16/an-accessible-visibility-react-component/>
VANCOUVER
Hugo “Kitty” Giraudel | Sciencx - » An accessible visibility React component. [Internet]. [Accessed 2024-03-28T09:53:21+00:00]. Available from: https://www.scien.cx/2020/01/16/an-accessible-visibility-react-component/
CHICAGO
" » An accessible visibility React component." Hugo “Kitty” Giraudel | Sciencx - Accessed 2024-03-28T09:53:21+00:00. https://www.scien.cx/2020/01/16/an-accessible-visibility-react-component/
IEEE
" » An accessible visibility React component." Hugo “Kitty” Giraudel | Sciencx [Online]. Available: https://www.scien.cx/2020/01/16/an-accessible-visibility-react-component/. [Accessed: 2024-03-28T09:53:21+00:00]
rf:citation
» An accessible visibility React component | Hugo “Kitty” Giraudel | Sciencx | https://www.scien.cx/2020/01/16/an-accessible-visibility-react-component/ | 2024-03-28T09:53:21+00:00
https://github.com/addpipe/simple-recorderjs-demo