React Aria Example: Building a Custom Button
You'll learn how to use React Aria's useButton hook to create a fully accessible button component with proper keyboard handling, ARIA attributes, and event management—all without prescribing any styles.
The Hook-Based Approach to Accessibility
Unlike Radix which provides ready-made components, React Aria gives you hooks that return prop objects. The useButton hook is a perfect example: you call it with your component's props and a ref, and it hands back an object containing all the accessibility props your button needs.
import { useButton } from '@react-aria/button';
import { useRef } from 'react';
function CustomButton(props) {
const ref = useRef(null);
const { buttonProps } = useButton(props, ref);
return (
<button
{...buttonProps}
ref={ref}
className="my-custom-styles"
>
{props.children}
</button>
);
}
What You Get From buttonProps
When you spread {...buttonProps}, you're applying:
- onClick: Click handler that respects disabled state
- onKeyDown: Keyboard support (Space/Enter trigger the action)
- aria-pressed: For toggle buttons, indicates pressed state
- disabled: Proper disabled attribute handling
- tabIndex: Focus management
- role: If not using a native button element
The beauty is you still own the styling completely—React Aria only handles the interactive behavior and accessibility.
Think of It Like a Prop Getter
This pattern should feel familiar: useButton is essentially a prop getter function. You pass in your configuration, get back the necessary props, and spread them onto your element. The hook manages all the cross-browser quirks and accessibility requirements you'd otherwise implement manually.
Key Takeaway: useButton(props, ref) returns buttonProps containing all the onClick, keyboard, and ARIA attributes needed for an accessible button—you just spread them onto your styled element.