Skip to content

Material 3

Components

Button

Buttons prompt most actions in a UI.

AAli Connors@ali-connorsThanks for getting the groceries. I’ll do them next week.$159.32

Buttons communicate actions that people can take. They are typically placed throughout the UI, in places like:

Compared with the other buttons:

  • Icon button: For a button with an icon and no label, use an icon button. It has no visible text, so it needs an aria-label.
  • FAB: For the most important action on a screen, use a FAB.
  • Split button: For an action with a menu of related options, use a split button.
  • Button group: To select one of several options, or to group related actions, use a button group.
import { Button } from '@material-web/react'

Basic button

Write the label as the button's children.

import { Button, Icon } from '@material-web/react'

export default function Basic() {
  return (
    <>
      <Button variant="text">Cancel</Button>
      <Button>
        <Icon name="check" />
        Save
      </Button>
    </>
  )
}

The Button comes in five variants: elevated, filled (the default), tonal, outlined and text.

import { Button } from '@material-web/react'

export default function Variants() {
  return (
    <>
      <Button variant="elevated">Elevated</Button>
      <Button variant="filled">Filled</Button>
      <Button variant="tonal">Tonal</Button>
      <Button variant="outlined">Outlined</Button>
      <Button variant="text">Text</Button>
    </>
  )
}

Each variant is shown below enabled, disabled with isDisabled, and as a link with elementType="a". A disabled button cannot be pressed or focused.

Elevated button

An elevated button is a tonal button with a shadow. To avoid overusing shadows, use it only when the button needs visual separation from a visually prominent background.

Link
import { Button } from '@material-web/react'

export default function Elevated() {
  return (
    <>
      <Button variant="elevated">Elevated</Button>
      <Button variant="elevated" isDisabled>
        Disabled
      </Button>
      <Button variant="elevated" elementType="a" href="#elevated-button">
        Link
      </Button>
    </>
  )
}

Filled button

A filled button has the most visual impact after the FAB. Use it for important, final actions that complete a flow, such as Save, Join now or Confirm.

Link
import { Button } from '@material-web/react'

export default function Filled() {
  return (
    <>
      <Button>Filled</Button>
      <Button isDisabled>Disabled</Button>
      <Button elementType="a" href="#filled-button">
        Link
      </Button>
    </>
  )
}

Tonal button

A tonal button suits a lower-priority action that needs slightly more emphasis than an outline gives, such as Next in an onboarding flow.

Link
import { Button } from '@material-web/react'

export default function Tonal() {
  return (
    <>
      <Button variant="tonal">Tonal</Button>
      <Button variant="tonal" isDisabled>
        Disabled
      </Button>
      <Button variant="tonal" elementType="a" href="#tonal-button">
        Link
      </Button>
    </>
  )
}

Outlined button

An outlined button is for medium-emphasis actions: ones that are important, but are not the primary action in a product.

Link
import { Button } from '@material-web/react'

export default function Outlined() {
  return (
    <>
      <Button variant="outlined">Outlined</Button>
      <Button variant="outlined" isDisabled>
        Disabled
      </Button>
      <Button variant="outlined" elementType="a" href="#outlined-button">
        Link
      </Button>
    </>
  )
}

Text button

A text button is for the lowest-priority actions, especially when presenting several options, such as the actions of a dialog. It has no container, and it cannot be a toggle.

Link
import { Button } from '@material-web/react'

export default function Text() {
  return (
    <>
      <Button variant="text">Text</Button>
      <Button variant="text" isDisabled>
        Disabled
      </Button>
      <Button variant="text" elementType="a" href="#text-button">
        Link
      </Button>
    </>
  )
}

Icons

Write an icon as a child, before or after the label. An Icon is sized to the button and is decorative, so the label still names the button.

import { Button, Icon } from '@material-web/react'

export default function WithIcon() {
  return (
    <>
      <Button>
        <Icon name="add" />
        Create
      </Button>
      <Button variant="tonal" size="m">
        <Icon name="edit" />
        Edit
      </Button>
      <Button variant="outlined">
        Send
        <Icon name="send" className="rtl:icon-mirrored" />
      </Button>
    </>
  )
}

Any icon works: Icon, an icon from an SVG library, or your own component. Give an icon that is not an Icon the size listed under Sizes. See Composition.

Handling presses

Handle a press with onPress, in place of onClick. It is called for a press with a mouse, a touch, the keyboard or assistive technology, and receives an event whose pointerType tells them apart.

Not pressed yet
import { Button } from '@material-web/react'
import { useState } from 'react'

export default function Press() {
  const [pointerType, setPointerType] = useState<string>()
  return (
    <>
      <Button onPress={(event) => setPointerType(event.pointerType)}>Press me</Button>
      <span className="text-body-md text-on-surface-variant">
        {pointerType ? `Pressed with: ${pointerType}` : 'Not pressed yet'}
      </span>
    </>
  )
}

The button takes the rest of React Aria's press events too, such as onPressStart and onPressEnd. The Props tab lists them under React Aria props.

Toggle button

Set isSelectable to make a toggle button. Control its selection with isSelected and onChange, or set its initial state with defaultSelected. A selected button changes its colors and swaps its shape: a round button becomes square, and a square one round.

import { Button, Icon } from '@material-web/react'
import { useState } from 'react'

export default function Toggle() {
  const [isSaved, setSaved] = useState(false)
  return (
    <>
      <Button variant="tonal" isSelectable isSelected={isSaved} onChange={setSaved}>
        <Icon name="bookmark" className={isSaved ? 'icon-filled' : undefined} />
        {isSaved ? 'Saved' : 'Save'}
      </Button>
      <Button variant="outlined" isSelectable defaultSelected>
        Following
      </Button>
      <Button variant="filled" shape="square" isSelectable>
        Square
      </Button>
    </>
  )
}
  • Without isSelectable, isSelected, defaultSelected and onChange are type errors.
  • A text button cannot be a toggle.
  • To let people choose among several toggle buttons, put them in a button group.

Loading

Set isPending while the button's action is in progress, as with React Aria's pending buttons. A pending button shows a loading indicator in place of its label and icons, sized to the button. It keeps its size and colors, so the layout around it does not shift.

A toggle button can be pending too, such as while a change to its selection is saved. In this example, Follow is pending after a press, and shows its new state once the change is saved.

import { Button } from '@material-web/react'
import { useState } from 'react'

export default function Loading() {
  const [isSaving, setSaving] = useState(false)
  const [isFollowing, setFollowing] = useState(false)
  const [isFollowPending, setFollowPending] = useState(false)
  const save = async () => {
    setSaving(true)
    await new Promise((resolve) => setTimeout(resolve, 3000))
    setSaving(false)
  }
  const follow = async (isSelected: boolean) => {
    setFollowPending(true)
    await new Promise((resolve) => setTimeout(resolve, 3000))
    setFollowing(isSelected)
    setFollowPending(false)
  }
  return (
    <>
      <Button size="m" isPending={isSaving} onPress={save}>
        Save
      </Button>
      <Button
        size="m"
        isSelectable
        isSelected={isFollowing}
        isPending={isFollowPending}
        onChange={follow}
      >
        Follow
      </Button>
    </>
  )
}
  • Presses: A pending button ignores presses and shows no hover. As a submit button, it does not submit its form, and as a link, it does not navigate.
  • Selection: A pending toggle button keeps its selection, and the colors and shape that show it. Pressing it does not toggle it.
  • Focus: It keeps the focus, so a keyboard user stays on it while the action runs.
  • Children: Its children are hidden while it is pending. See Pending content.

Color

A variant sets the button's colors. To use other color roles, pass their classes in className: a container color, and the on-color of that container for the label. They replace the variant's own colors, and the hover, focus and press states follow the label color.

import { Button } from '@material-web/react'

export default function Colors() {
  return (
    <>
      <Button variant="text" className="text-tertiary">
        Tertiary
      </Button>
      <Button variant="tonal" className="bg-tertiary-container text-on-tertiary-container">
        Tertiary container
      </Button>
      <Button className="bg-error text-on-error">Error</Button>
    </>
  )
}

Sizes

For larger or smaller buttons, set size to xs, s (the default), m, l or xl. The label, the padding and an Icon child grow with the button.

SizeHeightIcon
xs32dp20dp
s40dp20dp
m56dp24dp
l96dp32dp
xl136dp40dp
import { Button } from '@material-web/react'

export default function Sizes() {
  return (
    <>
      <Button size="xs">Extra small</Button>
      <Button size="s">Small</Button>
      <Button size="m">Medium</Button>
      <Button size="l">Large</Button>
      <Button size="xl">Extra large</Button>
    </>
  )
}

Shapes

Set shape to round (the default) or square. With hasShapeMorph, the corners tighten while the button is pressed. Press and hold the last two buttons to see it.

import { Button } from '@material-web/react'

export default function Shapes() {
  return (
    <>
      <Button shape="round">Round</Button>
      <Button shape="square">Square</Button>
      <Button shape="round" hasShapeMorph>
        Round, morphs
      </Button>
      <Button shape="square" hasShapeMorph>
        Square, morphs
      </Button>
    </>
  )
}

Rendering non-native buttons

Set elementType to render the button as an element other than a <button>. The element gets the button role and a tab stop, and Space or Enter presses it as on a native button.

<Button elementType="div" onPress={save}>
  Save
</Button>

Set elementType="a" to render the button as a link, with href, target and rel. The browser follows it as a native link, without a client-side router. To follow it through a router, see Third-party routing library.

import { Button, Icon } from '@material-web/react'

export default function LinkButton() {
  return (
    <Button
      variant="tonal"
      elementType="a"
      href="https://m3.material.io/components/buttons"
      target="_blank"
      rel="noreferrer"
    >
      Read the spec
      <Icon name="open_in_new" />
    </Button>
  )
}

Third-party routing library

The browser follows a link button with a full page load. To navigate with a client-side router instead, call the router in onClick, which receives the browser's click event. Leave a click that opens a new tab or window to the browser. With React Router:

import { Button } from '@material-web/react'
import type { ReactNode } from 'react'
import { useHref, useNavigate } from 'react-router'

function ButtonLink({ to, children }: { to: string; children: ReactNode }) {
  const navigate = useNavigate()
  return (
    <Button
      elementType="a"
      href={useHref(to)}
      onClick={(event) => {
        // A click with a modifier key, or with another mouse button, stays the browser's.
        if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
          return
        event.preventDefault()
        navigate(to)
      }}
    >
      {children}
    </Button>
  )
}

Note: RouterProvider does not route a button's href. It routes the links of items, such as a ListItem or a Tab. See Routing.

File upload

To choose files with a button, wrap it in React Aria's FileTrigger, which opens the browser's file picker on a press. Read the chosen files in onSelect. Set allowsMultiple to accept several files, and acceptedFileTypes to limit their types.

import { Button, Icon } from '@material-web/react'
import { useState } from 'react'
import { FileTrigger } from 'react-aria-components'

export default function FileUpload() {
  const [names, setNames] = useState<string[]>([])
  return (
    <>
      <FileTrigger
        allowsMultiple
        onSelect={(files) => setNames(Array.from(files ?? [], (file) => file.name))}
      >
        <Button variant="tonal">
          <Icon name="upload" />
          Upload files
        </Button>
      </FileTrigger>
      {names.length > 0 && (
        <span className="text-body-md text-on-surface-variant">{names.join(', ')}</span>
      )}
    </>
  )
}

Customization

Pass classes in className to restyle one button. A class of yours replaces the button's own class for the same property. See Styling for the classes, the states and the theme.

import { Button, Icon } from '@material-web/react'

export default function Custom() {
  return (
    <>
      <Button className="density-2">Compact</Button>
      <Button variant="outlined" className="hover:bg-primary hover:text-on-primary">
        Filled on hover
      </Button>
      <Button variant="tonal">
        {({ isHovered }) => (
          <>
            <Icon name="favorite" className={isHovered ? 'icon-filled' : undefined} />
            Favorite
          </>
        )}
      </Button>
    </>
  )
}
  • Density: Add density-1, density-2 or density-3 to the button, or to an element around it. Each step makes the button 4dp shorter. See Density.
  • States: Style a state with its variant, such as hover:, pressed: or selected:. See States.
  • Functions: className, style and the children can be functions of the button's state: isHovered, isPressed, isFocused, isFocusVisible, isDisabled and isPending. A toggle also has isSelected. See Children as a function.
  • Every button: To restyle all the buttons at once, change the theme.

Complex button

For a button that the variants do not cover, build your own from React Aria's Button. Position it with relative and add a Ripple as its direct child, for the hover and focus layer and the press ripple. Add the focus-ring class for the focus indicator.

import { Icon, Ripple } from '@material-web/react'
import { Button } from 'react-aria-components'

export default function Complex() {
  return (
    <Button className="relative focus-ring box-border flex w-72 cursor-pointer items-center gap-space-200 rounded-shape-lg border-0 bg-surface-container-high p-space-200 text-start font-plain text-on-surface outline-none">
      <Ripple />
      <span className="grid size-14 flex-none place-items-center rounded-shape-md bg-tertiary-container text-on-tertiary-container icon-24">
        <Icon name="hiking" />
      </span>
      <span className="flex flex-col">
        <span className="text-title-md">Hiking</span>
        <span className="text-body-md text-on-surface-variant">12 trails nearby</span>
      </span>
    </Button>
  )
}

To give another element the look of a Button, call the exported buttonStyles with a variant, a size and a shape. See Custom components.

Limitations

A button rendered as an <a> keeps the button role, so assistive technology announces it as a button and not as a link. A disabled one has no href. Where the role matters, render a link of your own and give it the look of a button with buttonStyles. See Custom components.

Disabled buttons

A disabled button cannot be focused and reports no hover, so it shows no tooltip.

Pending content

A pending button shows only its loading indicator. Children written for the pending state, such as a Saving… label, are hidden too. For a button that shows other content while pending, build a complex button.