What is react-aria?
The react-aria package provides a collection of React hooks that help you build accessible user interfaces. It abstracts away the complexity of managing ARIA roles, properties, and keyboard interactions, making it easier to create components that are accessible to all users.
What are react-aria's main functionalities?
Button
The useButton hook provides the necessary ARIA attributes and event handlers to make a button accessible. It manages focus, keyboard interactions, and ARIA roles.
import { useButton } from 'react-aria';
import { useRef } from 'react';
function MyButton(props) {
let ref = useRef();
let { buttonProps } = useButton(props, ref);
return (
<button {...buttonProps} ref={ref}>
{props.children}
</button>
);
}
Checkbox
The useCheckbox hook provides the necessary ARIA attributes and event handlers to make a checkbox accessible. It manages state, keyboard interactions, and ARIA roles.
import { useCheckbox } from 'react-aria';
import { useRef } from 'react';
function MyCheckbox(props) {
let ref = useRef();
let { inputProps } = useCheckbox(props, ref);
return (
<label>
<input {...inputProps} ref={ref} />
{props.children}
</label>
);
}
Dialog
The useDialog hook provides the necessary ARIA attributes and event handlers to make a dialog accessible. It manages focus trapping, keyboard interactions, and ARIA roles.
import { useDialog } from 'react-aria';
import { useRef } from 'react';
function MyDialog(props) {
let ref = useRef();
let { dialogProps } = useDialog(props, ref);
return (
<div {...dialogProps} ref={ref}>
{props.children}
</div>
);
}
Other packages similar to react-aria
react-a11y
react-a11y is a package that provides accessibility warnings in the console during development. It helps developers identify and fix accessibility issues in their React components. Unlike react-aria, it does not provide hooks or components to manage ARIA attributes and interactions.
reakit
Reakit is a low-level component library for building accessible web apps. It provides primitive components that can be composed to create more complex UI elements. Reakit focuses on flexibility and composability, similar to react-aria, but offers a different API and set of components.
React ARIA
Utilities
Usage
npm install react-aria --save
bower install react-aria --save
Example
import { AriaManager, AriaToggle, AriaPopover, AriaItem } from 'react-aria'
class Dropdown extends Component {
state = {
selection: null
}
_handleSelection = (value, e) => {
this.setState({ selection: value })
}
render() {
return (
<AriaManager onItemSelection={this._handleSelection}>
{ isOpen =>
<div>
<AriaToggle>
{this.state.selection || 'Select A Dropdown Item'}
</AriaToggle>
{ isOpen &&
<AriaPopover role="menu">
<AriaItem>Apples</AriaItem>
<AriaItem>Pears</AriaItem>
<AriaItem>Oranges</AriaItem>
</AriaPopover>
}
</div>
}
</AriaManager>
)
}
}
Running Locally
clone repo
git clone git@github.com:souporserious/react-aria.git
move into folder
cd ~/react-aria
install dependencies
npm install
run dev mode
npm run dev
open your browser and visit: http://localhost:8080/
Thank You
Huge thank you to David Clark and all of his ARIA work. Most of the code in here is heavily inspired by what he has done.