Style the components the way you style your own markup: with Tailwind's utility classes and the classes of the theme. Add classes to one component, style its states, or change the theme to restyle them all.
Classes
Pass classes to a component in className. A class of yours replaces the component's class for the same property: bg-tertiary replaces the button's bg-primary.
import { Button } from '@material-web/react'
export default function ClassName() {
return (
<>
<Button>Default</Button>
<Button className="bg-tertiary text-on-tertiary">Tertiary</Button>
<Button className="rounded-shape-md">Medium corners</Button>
<Button className="w-48">48 wide</Button>
</>
)
}- Element: The classes go on the component's root element. For a
Dialogor a modal sheet, that is its container, inside the scrim. - Parts: A component made of several parts takes
classNameon each one, such asTabs,TabListandTab. - Children: The children you write take classes of their own, such as an
Iconor aTextin a slot. See Composition. - Window size: Add a breakpoint variant for another class in a larger window, as in
expanded-screen:w-80. - Inline styles: Pass
stylefor a value that no class provides. - Inner elements: To restyle an element inside a component that
classNamedoes not reach, such as the track of aSwitch, set the color roles it uses. See Theme.
States
A component reports its states, such as hovered, pressed or selected. Style a state with a Tailwind variant, with a function passed as className or style, or with a data attribute in your own CSS.
| State | Variant | Function | Attribute |
|---|---|---|---|
| Hovered | hover: | isHovered | data-hovered |
| Pressed | pressed: | isPressed | data-pressed |
| Focused | focus: | isFocused | data-focused |
| Focused from the keyboard | focus-visible: | isFocusVisible | data-focus-visible |
| Focus inside | focus-within: | isFocusWithin | data-focus-within |
| Selected | selected: | isSelected | data-selected |
| Indeterminate | indeterminate: | isIndeterminate | data-indeterminate |
| Disabled | disabled: | isDisabled | data-disabled |
| Read-only | read-only: | isReadOnly | data-readonly |
| Required | required: | isRequired | data-required |
| Invalid | invalid: | isInvalid | data-invalid |
| Open | open: | isOpen | data-open |
| Dragging | dragging: | isDragging | data-dragging |
| Drop target | drop-target: | isDropTarget | data-drop-target |
| Opening, closing | entering:, exiting: | isEntering, isExiting | data-entering, data-exiting |
A component reports only the states that apply to it. For the states of each component, see Composition.
Variants
Put a state's variant before a class to apply the class in that state.
import { Button } from '@material-web/react'
export default function StateVariants() {
return (
<>
<Button className="hover:bg-tertiary hover:text-on-tertiary">hover:</Button>
<Button className="pressed:bg-tertiary pressed:text-on-tertiary">pressed:</Button>
<Button isSelectable className="selected:bg-tertiary selected:text-on-tertiary">
selected:
</Button>
</>
)
}- Combined states: To style a selected component in another state, name both variants, as in
selected:disabled:. - Related elements: Each variant has Tailwind's
group-*,peer-*,not-*andhas-*forms, such asgroup-selected:. - Controls with a label: A
Checkbox, aRadio, aSwitchand aTextFieldreport hover, press and focus on an element inside them, and their other states on the root element. For those three states, use thehas-*form on the component, as inhas-hover:bg-surface-container. - Tailwind's own names: On a component, use
pressed:,selected:andnot-disabled:in place of Tailwind'sactive:,checked:andenabled:. - Text controls: Use
populated:for a text control that holds text and is not showing its placeholder. - Other elements: On an element that is not a component, a variant that Tailwind also has, such as
hover:ordisabled:, matches the element's pseudo-class.
The variants are those of React Aria's Tailwind plugin, which lists the variant of every state.
Render props
Pass a function as className or style to compute the value from the component's state, as with React Aria's render props. The function receives the states as one object.
<Button className={({ isPressed }) => (isPressed ? 'bg-tertiary text-on-tertiary' : '')}>
Save
</Button>- No state: On a component that reports no state, such as a
Cardor anIcon,classNameandstyletake a value only. - Controls with a label: On a
Checkbox, aRadio, aSwitchand aTextField, the function receives the states of the root element, without hover, press and focus. To read those, pass a function as children.
Data attributes
In your own CSS, select a state by its data attribute. See React Aria's States guide.
.save-button[data-pressed] {
background-color: var(--md-sys-color-tertiary);
color: var(--md-sys-color-on-tertiary);
}Theme
The components take their colors, typefaces, corners and motion from the theme. To restyle every component at once, change the theme:
| To change | See |
|---|---|
| The colors | Seed color, Custom colors |
| The light or dark scheme, of the page or an element | Light and dark |
| The contrast level | Contrast |
| The typefaces and the icon font | Custom fonts |
| The font weights and the type styles | Custom weights, Custom type styles |
| The corners | Custom shapes |
| The motion scheme | Motion scheme |
To restyle one component, set the color roles it uses on the component itself. A color role is a custom property, and a value set on an element applies to everything inside it, including the elements that className does not reach:
.danger {
--md-sys-color-primary: var(--md-sys-color-error);
--md-sys-color-on-primary: var(--md-sys-color-on-error);
}<Switch className="danger" />Note: A menu, a dialog, a modal sheet, a tooltip and a snackbar are rendered at the end of the page, not inside the element they are written in. A class or a data-theme on an element around them does not reach them. Set it on the overlay itself, or on the root element.
Density
Add density-1, density-2 or density-3 to a component, or to any element around it, to make it more compact. Each step takes 4dp off the height of a component that follows the density: a Button, an IconButton, a SplitButton, a Chip, a ListItem, a MenuItem, a Tab and a TextField.
import { Button } from '@material-web/react'
export default function Density() {
return (
<>
<Button>Default</Button>
<Button className="density-1">density-1</Button>
<Button className="density-2">density-2</Button>
<div className="flex gap-space-100 density-3">
<Button variant="tonal">In a</Button>
<Button variant="tonal">density-3 box</Button>
</div>
</>
)
}- List items: A
ListItemgets no shorter than 48dp. - Menus: A menu is rendered at the end of the page. Add the class to its
MenuPopover, or to an element that contains it, such as the root element. - Your own elements: To make an element of your own follow the density, see Spacing.
Custom components
Define the variants of your own component with the exported tv, a tailwind-variants function, and type its props with VariantProps. Use it in place of tailwind-variants' own tv, which lets one theme class replace another, such as text-primary, a color, and text-label-md, a text style.
import { tv, type VariantProps } from '@material-web/react'
const badge = tv({
base: 'rounded-shape-full px-space-100 py-space-25 text-label-md',
variants: {
tone: {
neutral: 'bg-surface-container-highest text-on-surface',
primary: 'bg-primary text-on-primary',
error: 'bg-error text-on-error',
},
},
defaultVariants: { tone: 'neutral' },
})
type BadgeProps = VariantProps<typeof badge> & { children: string }
function Badge({ tone, children }: BadgeProps) {
return <span className={badge({ tone })}>{children}</span>
}
export default function CustomComponent() {
return (
<>
<Badge>Draft</Badge>
<Badge tone="primary">New</Badge>
<Badge tone="error">Overdue</Badge>
</>
)
}To join class lists without variants, use the exported twMerge, a tailwind-merge function that knows the theme's classes as tv does. A later class replaces an earlier one of the same utility, as in twMerge(box, 'rounded-shape-xl').
For the rest of a component, use what the library's own components use:
| For | Use |
|---|---|
| A state layer and a press ripple | The Ripple component |
| A focus indicator | The focus-ring class |
| A 48dp target for a small control | The touch-target class |
| A transition between states | A transition preset, such as transition-shape-color |
| The look of a button on another element | The exported buttonStyles, called with a variant, a size and a shape of the Button |
In this example, buttonStyles gives the <label> of a file input the look of a tonal button:
import { buttonStyles } from '@material-web/react'
export default function ButtonStyles() {
return (
<label
className={buttonStyles({
variant: 'tonal',
size: 'm',
className: 'has-[:focus-visible]:focus-indicator',
})}
>
Upload
<input type="file" className="sr-only" />
</label>
)
}