Show an element's state with the Material 3 state layer opacities, the disabled opacities and the focus ring. Each opacity works as a class and as the opacity of any color, a focus ring is one class, and Tailwind's state variants pick the state.
State layers
The theme's states follow Material's state layers. A state layer shows an element's state, hovered, focused, pressed or dragged, as its content color over it at a fixed opacity. Use each opacity as a class, as in opacity-state-hover, or as a color's opacity modifier, as in bg-on-surface/state-hover.
state-hover8%state-focus10%state-pressed10%state-dragged16%disabled-container12%disabled-content38%| Name and token | Opacity | Use |
|---|---|---|
state-hover--md-sys-state-hover-state-layer-opacity | 8% | A cursor is placed above the element |
state-focus--md-sys-state-focus-state-layer-opacity | 10% | The element is highlighted, with a keyboard or voice |
state-pressed--md-sys-state-pressed-state-layer-opacity | 10% | The element is tapped or clicked |
state-dragged--md-sys-state-dragged-state-layer-opacity | 16% | The element is pressed and moved |
disabled-content--md-sys-state-disabled-content-opacity | 38% | The text and icons of a disabled element |
disabled-container--md-sys-state-disabled-container-opacity | 12% | The container of a disabled element |
An opacity is one of Tailwind's opacity values, and is used like any other:
-
Layer color: A state layer takes the color of the element's content. On
bg-secondary-container text-on-secondary-container, the layer ison-secondary-container. -
Without a fill: On an element with no container color, such as a text button, set the layer as the background in the state, as in
text-primary hover:bg-primary/state-hover. -
Over a fill: On a filled element, draw the layer over the fill, in the content color. A pseudo-element with
bg-currentdoes so:<button class="relative bg-secondary-container text-on-secondary-container before:absolute before:inset-0 before:rounded-inherit before:bg-current before:opacity-0 hover:before:opacity-state-hover focus-visible:before:opacity-state-focus active:before:opacity-state-pressed" > Save </button> -
Disabled: For a disabled element, set its content to on-surface at 38% with
text-on-surface/disabled-content, and its container to on-surface at 12% withbg-on-surface/disabled-container. A disabled element shows no state layer. -
Merged classes: tailwind-merge does not know the opacities, and keeps both of two opacity classes. To merge them, use the
tvor thetwMergethat@material-web/reactexports, where the later class replaces the earlier one.
To add a state layer and a press ripple to a component of your own, use the Ripple component.
Focus ring
Add focus-ring to show the Material focus indicator while the element has keyboard focus: a 3dp outline in the secondary color, 2dp outside the element. Use focus-ring-inset to show it 3dp inside, for an element whose edge is clipped by its container. Tab to the buttons below.
import { twMerge } from '@material-web/react'
const box =
'cursor-pointer rounded-shape-md border-0 bg-surface-container-high px-space-200 py-space-150 text-label-lg text-on-surface'
export default function FocusRing() {
return (
<>
<button type="button" className={twMerge(box, 'focus-ring')}>
focus-ring
</button>
<button type="button" className={twMerge(box, 'focus-ring-inset')}>
focus-ring-inset
</button>
<span className={twMerge(box, 'focus-indicator cursor-default')}>focus-indicator</span>
</>
)
}| Class | Shown | Ring |
|---|---|---|
focus-ring | While the element has keyboard focus | 2dp outside the element |
focus-ring-inset | While the element has keyboard focus | 3dp inside the element |
focus-ring-icon | While the element has keyboard focus | Across the element's edge, 2dp in and 1dp out |
focus-indicator | Always | 2dp outside the element |
focus-indicator-inset | Always | 3dp inside the element |
- Keyboard focus:
focus-visible:matches keyboard focus: the browser's:focus-visible, or, with React Aria's Tailwind plugin, thedata-focus-visibleof a React Aria component. The ring follows it, so a pointer press shows no ring. - A part of an element: Use
focus-indicatorandfocus-indicator-insetfor the ring alone, with no condition, on a part that shows the keyboard focus of the element it is in, as ingroup-focus-visible:focus-indicatoron a part of agroup. - Icon targets: Use
focus-ring-iconfor the ring of a 24dp round icon target inside a component, such as a chip's remove button. It straddles the target's edge, 2dp in and 1dp out. - Stacking: A ringed element is raised over its siblings (
z-focus-ring), so that nothing next to it covers its ring. See Stacking order.
State variants
The theme's state variants are Tailwind's own. To style React Aria components, which report an element's state as a data attribute, load React Aria's Tailwind plugin, tailwindcss-react-aria-components, after the theme. The theme's utilities then follow its variants too. @material-web/react already loads the plugin.
@import 'tailwindcss';
@import '@material-web/tailwind';
@plugin 'tailwindcss-react-aria-components';With the plugin, a variant matches a data attribute on a React Aria component (data-rac). On any other element, it matches its pseudo-class, as Tailwind's own variants do.
| Variant | On a React Aria component | On any other element |
|---|---|---|
hover: | data-hovered | :hover |
focus: | data-focused, which is any focus | :focus |
focus-visible: | data-focus-visible | :focus-visible |
focus-within: | data-focus-within | :focus-within |
disabled: | data-disabled | :disabled |
indeterminate:, invalid:, open: | data-indeterminate, data-invalid, data-open | Tailwind's own |
pressed: | data-pressed | data-pressed |
selected: | data-selected | data-selected |
dragging: | data-dragging | data-dragging |
entering:, exiting: | data-entering and data-exiting, on an overlay that is about to show or is hiding | The same attributes |
- Attribute variants:
pressed:,selected:,dragging:,entering:andexiting:match the attribute alone, as the plugin's other variants do (expanded:,placement-top:, ...). - Order: Tailwind sorts the attribute variants after its own variants,
disabled:included. For a style that must win over aselected:one, name both, as inselected:disabled:. active:,checked:andenabled::active:matches the plugin'sdata-active, andchecked:andenabled:match Tailwind's own pseudo-classes. On a React Aria component, useselected:andnot-disabled:instead.
See Styling for examples.
Custom states
Every opacity, and each measure of the focus ring, is a custom property, so your own CSS can set it after the import. A value set before the import is replaced by the theme's.
@import 'tailwindcss';
@import '@material-web/tailwind';
:root {
--md-sys-state-hover-state-layer-opacity: 0.1;
--md-sys-state-focus-indicator-thickness: 2px;
--md-sys-state-focus-indicator-inner-offset: -2px;
}| Token and theme variable | Default | Sets |
|---|---|---|
--md-sys-state-hover-state-layer-opacity--opacity-state-hover | 0.08 | The hover layer of the components, and state-hover |
--md-sys-state-focus-state-layer-opacity--opacity-state-focus | 0.1 | The focus layer of the components, and state-focus |
--md-sys-state-pressed-state-layer-opacity--opacity-state-pressed | 0.1 | The pressed layer of the components, and state-pressed |
--md-sys-state-dragged-state-layer-opacity--opacity-state-dragged | 0.16 | The dragged layer of the components, and state-dragged |
--md-sys-state-disabled-content-opacity--opacity-disabled-content | 0.38 | The text and icons of a disabled component, and disabled-content |
--md-sys-state-disabled-container-opacity--opacity-disabled-container | 0.12 | The container of a disabled component, and disabled-container |
--md-sys-state-focus-indicator-thickness | 3px | The width of every focus ring |
--md-sys-state-focus-indicator-outer-offset | 2px | The gap between an element and its focus-ring |
--md-sys-state-focus-indicator-inner-offset | -3px | The place of a focus-ring-inset, inside the element's edge |
- Theme variables: Under each opacity token is its Tailwind theme variable, which gives the token its classes: the token as a percentage. In your own CSS, read the token, as in
var(--md-sys-state-hover-state-layer-opacity): a theme variable has the root's value, and does not follow a token set on an element. The focus ring's tokens have no theme variable. - Opacity values: Set an opacity as a number from 0 to 1, not as a percentage.
- Inset rings: For an inset ring against the element's edge, set the inner offset to the thickness, as a negative length.
- Ring color: A focus ring is in the
secondarycolor role. - One part of the page: Set a token on an element, in place of the root, to change the states inside it only.