Button

A button allows a user to perform an action, with mouse, touch, and keyboard interactions.

isDisabled 
Example
Button.tsx
Button.css
theme.css
import {Button} from './Button';

<Button>Vanilla</Button>

Features

On the surface, building a custom styled button seems simple. However, there are many cross browser inconsistencies in interactions and accessibility features to consider. Button handles all of these interactions for you, so you can focus on the styling.

  • Styleable – Hover, press, and keyboard focus states are provided for easy styling. These states only apply when interacting with an appropriate input device, unlike CSS pseudo classes.
  • Accessible – Uses a native <button> element under the hood, with support for the Space and Enter keys.
  • Cross-browser – Mouse, touch, keyboard, and focus interactions are normalized to ensure consistency across browsers and devices.

Anatomy

Buttons consist of a clickable area usually containing a textual label or an icon that users can click to perform an action. In addition, keyboard users may activate buttons using the Space or Enter keys.

If a visual label is not provided (e.g. an icon only button), then an aria-label or aria-labelledby prop must be passed to identify the button to assistive technology.

Examples

Ripple Button A button with an animated ripple effect styled with Tailwind CSS.

Events

Button supports user interactions via mouse, keyboard, and touch. You can handle all of these via the onPress prop. This is similar to the standard onClick event, but normalized to support all interaction methods equally. In addition, the onPressStart, onPressEnd, and onPressChange events are fired as the user interacts with the button.

Each of these handlers receives a , which exposes information about the target and the type of event that triggered the interaction. See usePress for more details.

Ready to be pressed.

import {Button} from 'react-aria-components';
import {useState} from 'react';

function Example() {
  let [pointerType, setPointerType] = useState('');

  return (
    <>
      <Button
        onPressStart={e => setPointerType(e.pointerType)}
        onPressEnd={() => setPointerType('')}>
        Press me
      </Button>
      <p>{pointerType ? `You are pressing the button with a ${pointerType}!` : 'Ready to be pressed.'}</p>
    </>
  );
}

Props

NameType
isPendingboolean
Whether the button is in a pending state. This disables press and hover events while retaining focusability, and announces the pending state to screen readers.
isDisabledboolean
Whether the button is disabled.
children<>
The children of the component. A function may be provided to alter the children based on component state.

Default className: react-aria-Button

Render PropCSS SelectorDescription
isHoveredCSS Selector: [data-hovered] Whether the button is currently hovered with a mouse.
isPressedCSS Selector: [data-pressed] Whether the button is currently in a pressed state.
isFocusedCSS Selector: [data-focused] Whether the button is focused, either via a mouse or keyboard.
isFocusVisibleCSS Selector: [data-focus-visible] Whether the button is keyboard focused.
isDisabledCSS Selector: [data-disabled] Whether the button is disabled.
isPendingCSS Selector: [data-pending] Whether the button is currently in a pending state.