Ripples add a state layer and press feedback to interactive elements. Add a Ripple to an interactive element of your own to give it the same hover and focus state layer and press ripple as the components. It works in a React Aria component and in any other element.
A Ripple draws two things:
- State layer: An overlay that shows while the element is hovered, focused from the keyboard or dragged.
- Press ripple: A circle that grows from the pointer when the element is pressed.
The components draw their own state layer and press ripple: for a button, use a button or an icon button instead.
import { Ripple } from '@material-web/react'Basic ripple
Place the Ripple as a direct child of the interactive element, its host, or of a wrapper around a control, and position that element with relative. The state layer and the press ripple fill the host and take its text color.
import { Ripple } from '@material-web/react'
export default function Basic() {
return (
<button
type="button"
className="relative focus-ring h-10 cursor-pointer rounded-shape-full border-0 bg-primary px-space-300 text-label-lg text-on-primary outline-none"
>
<Ripple />
Label
</button>
)
}A Ripple shows the states only: give the host its role, its press handling and a focus ring. For a whole button built this way, see Complex button.
Set variant to default (the default) or to a color role: primary, error, on-surface, on-surface-variant or on-secondary-container.
| Variant | Hover and focus layer | Press ripple |
|---|---|---|
default | The host's text color | The host's text color |
| A color role | The host's text color | The color role |
Use a color role when the press ripple and the layer differ in color, as on an unselected selection control that is pressed in the color it is about to take.
import { Icon, Ripple } from '@material-web/react'
const host =
'relative focus-ring grid size-10 cursor-pointer place-items-center rounded-shape-full border-0 bg-transparent text-on-surface outline-none icon-24'
export default function Variant() {
return (
<>
<button type="button" aria-label="Favorite" className={host}>
<Ripple />
<Icon name="favorite" />
</button>
<button type="button" aria-label="Favorite, pressed in primary" className={host}>
<Ripple variant="primary" />
<Icon name="favorite" />
</button>
<button type="button" aria-label="Delete, pressed in error" className={host}>
<Ripple variant="error" />
<Icon name="delete" />
</button>
</>
)
}States
The state layer shows the states of its parent, read the same way as the theme's state variants:
| State | On a React Aria component | On any other element | Layer |
|---|---|---|---|
| Hovered | data-hovered | :hover | 8% |
| Focused | data-focus-visible | :focus-visible | 10% |
| Dragged | data-dragging | data-dragging | 16% |
| Disabled | data-disabled | :disabled | None |
- Hover: The hover layer shows only on a device that hovers. It does not show while the pointer is on a control inside the host that has its own
Ripple, such as a chip's remove button. - Disabled: Set
aria-disabled="true"to disable the layer on either kind of element. - Press: A press ripples for the pointer's main button only, at 10%. A press on a control inside another host that has a
Rippletoo ripples in the inner one alone.
Given states
A Ripple in a part of the host, such as the round part of a switch or a checkbox, cannot read the host's states. Pass them as isHovered, isFocusVisible and isDisabled, from a React Aria component's render props. Focus shows over hover, and nothing shows while disabled. These props override the states of the ripple's parent. Without them, the layer shows its parent's states.
Give that part pointer-events-none, so that a press on the host ripples in it. In this example, a press anywhere on the toggle button ripples in its round part.
import { Icon, Ripple } from '@material-web/react'
import { ToggleButton } from 'react-aria-components'
export default function GivenStates() {
return (
<ToggleButton className="focus-ring flex cursor-pointer items-center gap-space-50 rounded-shape-full border-0 bg-transparent p-0 pe-space-200 text-label-lg text-on-surface outline-none">
{({ isSelected, isHovered, isFocusVisible, isDisabled }) => (
<>
<span className="pointer-events-none relative grid size-10 place-items-center rounded-full icon-24">
<Ripple isHovered={isHovered} isFocusVisible={isFocusVisible} isDisabled={isDisabled} />
<Icon name="favorite" className={isSelected ? 'icon-filled' : undefined} />
</span>
Favorite
</>
)}
</ToggleButton>
)
}Disabled
A disabled host shows neither the state layer nor the press ripple. Disable it with a native disabled, with data-disabled on a React Aria component, or with aria-disabled="true".
import { Ripple } from '@material-web/react'
export default function Disabled() {
return (
<button
type="button"
disabled
className="relative h-10 rounded-shape-full border-0 bg-on-surface/disabled-container px-space-300 text-label-lg text-on-surface/disabled-content"
>
<Ripple />
Label
</button>
)
}On an element around the host, data-disabled and aria-disabled="true" stop the press ripple alone. To hide the state layer there as well, pass isDisabled.
Color
The state layer and the press ripple take the host's text color, so the same Ripple suits any container. Set the container color and its content color on the host. See State layers.
import { Ripple, twMerge } from '@material-web/react'
const host =
'relative focus-ring h-10 cursor-pointer rounded-shape-full border-0 px-space-300 text-label-lg outline-none'
export default function Colors() {
return (
<>
<button type="button" className={twMerge(host, 'bg-primary text-on-primary')}>
<Ripple />
On primary
</button>
<button
type="button"
className={twMerge(host, 'bg-secondary-container text-on-secondary-container')}
>
<Ripple />
On secondary container
</button>
<button type="button" className={twMerge(host, 'bg-transparent text-primary')}>
<Ripple />
Primary
</button>
</>
)
}Shapes
Set the shape on the host. The state layer and the press ripple follow its corners.
import { Ripple, twMerge } from '@material-web/react'
import { Button } from 'react-aria-components'
const host =
'relative focus-ring flex h-30 w-60 cursor-pointer items-start border-0 bg-surface-container-highest p-space-200 text-title-md text-on-surface outline-none'
export default function Shape() {
return (
<>
<Button className={twMerge(host, 'rounded-shape-md')}>
<Ripple />
Medium corners
</Button>
<Button className={twMerge(host, 'rounded-shape-xl')}>
<Ripple />
Extra large corners
</Button>
</>
)
}Customization
Ripple takes no className and no style. It follows its host and the theme:
- Color: Set the host's text color, or a
variantfor the press ripple. See Color. - Shape: Set the corners on the host. See Shapes.
- Opacity: The layer and the press ripple use the theme's state layer opacities. To change them, see Custom states.
Limitations
Host position
The state layer and the press ripple fill the nearest positioned element. Without relative on the host, they cover an element further out.
Keyboard presses
A press from the keyboard draws no press ripple. The host shows its focus layer.
Interaction and style
- States: The focus layer shows for keyboard focus only. It does not include a focus ring: give the host the
focus-ringclass, which draws Material's focus indicator. - Motion: With reduced motion, the press ripple shows at its full size at once.
Labeling elements
Rippleis hidden from assistive technology and takes no pointer events. Give the host its role, its name, and its keyboard and press handling.
variant'default' | 'primary' | 'error' | 'on-surface' | 'on-surface-variant' | 'on-secondary-container'defaultdraws both the hover and focus layer and the press circle in the host's text color. A color role (primary,error,on-surface, ...) is the press circle's instead, when it differs from the layer's.isHoveredbooleanThe host's states, for a state layer in a part of the host (a switch's halo, a checkbox's round host in its label), which cannot see them: a React Aria component's render props. They outweigh the part's own states. Left out, the layer shows its parent's.
Props of Ripple (2)
isFocusVisiblebooleanisDisabledboolean