Skip to content

Material 3

Get started

Styling

Customize components with Tailwind classes.

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 Dialog or a modal sheet, that is its container, inside the scrim.
  • Parts: A component made of several parts takes className on each one, such as Tabs, TabList and Tab.
  • Children: The children you write take classes of their own, such as an Icon or a Text in 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 style for a value that no class provides.
  • Inner elements: To restyle an element inside a component that className does not reach, such as the track of a Switch, 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.

StateVariantFunctionAttribute
Hoveredhover:isHovereddata-hovered
Pressedpressed:isPresseddata-pressed
Focusedfocus:isFocuseddata-focused
Focused from the keyboardfocus-visible:isFocusVisibledata-focus-visible
Focus insidefocus-within:isFocusWithindata-focus-within
Selectedselected:isSelecteddata-selected
Indeterminateindeterminate:isIndeterminatedata-indeterminate
Disableddisabled:isDisableddata-disabled
Read-onlyread-only:isReadOnlydata-readonly
Requiredrequired:isRequireddata-required
Invalidinvalid:isInvaliddata-invalid
Openopen:isOpendata-open
Draggingdragging:isDraggingdata-dragging
Drop targetdrop-target:isDropTargetdata-drop-target
Opening, closingentering:, exiting:isEntering, isExitingdata-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-* and has-* forms, such as group-selected:.
  • Controls with a label: A Checkbox, a Radio, a Switch and a TextField report hover, press and focus on an element inside them, and their other states on the root element. For those three states, use the has-* form on the component, as in has-hover:bg-surface-container.
  • Tailwind's own names: On a component, use pressed:, selected: and not-disabled: in place of Tailwind's active:, checked: and enabled:.
  • 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: or disabled:, 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 Card or an Icon, className and style take a value only.
  • Controls with a label: On a Checkbox, a Radio, a Switch and a TextField, 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 changeSee
The colorsSeed color, Custom colors
The light or dark scheme, of the page or an elementLight and dark
The contrast levelContrast
The typefaces and the icon fontCustom fonts
The font weights and the type stylesCustom weights, Custom type styles
The cornersCustom shapes
The motion schemeMotion 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 ListItem gets 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.

DraftNewOverdue
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:

ForUse
A state layer and a press rippleThe Ripple component
A focus indicatorThe focus-ring class
A 48dp target for a small controlThe touch-target class
A transition between statesA transition preset, such as transition-shape-color
The look of a button on another elementThe 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>
  )
}