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.
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.
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.
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.
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.
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.
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,defaultSelectedandonChangeare type errors. - A
textbutton 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.
| Size | Height | Icon |
|---|---|---|
xs | 32dp | 20dp |
s | 40dp | 20dp |
m | 56dp | 24dp |
l | 96dp | 32dp |
xl | 136dp | 40dp |
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>Links
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-2ordensity-3to 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:orselected:. See States. - Functions:
className,styleand the children can be functions of the button's state:isHovered,isPressed,isFocused,isFocusVisible,isDisabledandisPending. A toggle also hasisSelected. 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
Link role
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.
Use cases
Material lists what people should be able to do with assistive technology:
- Use a button to perform an action
- Navigate to and activate a button
Interaction and style
- States: Hovering shows a state layer, and a press shows a ripple. Keyboard focus shows a focus ring, and a press with a mouse or touch does not.
- Touch target: Every size has a touch target of at least 48dp, an extra small button included.
- Color contrast: An enabled button needs 3:1 contrast with the background: its container for the
elevated,filledandtonalvariants, and its label foroutlinedandtext. Keep that contrast when you change its colors. - Text size: A label stays on one line, and the button grows wider with it. Keep labels concise, so that buttons still fit when people enlarge the text.
- Motion: With reduced motion, the changes of shape and color are instant.
- Forced colors: In forced colors, every variant but
textkeeps an edge, and a selected toggle button is filled with the system's highlight color.
Focus
- A disabled button cannot be focused.
- A pending button keeps the focus, so a keyboard user stays on it while the action runs.
Keyboard navigation
| Keys | Actions |
|---|---|
| Tab | Move the focus to a button |
| Space or Enter | Press the focused button, or select or deselect a toggle button |
Labeling elements
- The label names the button. Material advises a name that matches the visible text, such as Done, Send or Reply. To add context, set an
aria-labelthat includes the visible text. - For a button with an icon alone, use an icon button with an
aria-label. - A toggle button reports whether it is selected with
aria-pressed. - A pending button reports that it is unavailable with
aria-disabled. Its loading indicator is hidden from assistive technology, so the label still names the button. - When a focused button becomes pending, or stops being pending, screen readers announce it.
variant'text' | 'elevated' | 'filled' | 'tonal' | 'outlined'Default'filled'Five color options: elevated, filled, tonal, outlined, and text
elevated: The elevated button style is the same as the tonal button, but with a shadow. To avoid overusing shadows, use the elevated style only when absolutely necessary, such as when the button requires visual separation from a visually prominent background.filled: The filled button style has the most visual impact after the FAB, and should be used for important, final actions that complete a flow, like Save, Join now, or Confirm.tonal: The tonal button style is useful in contexts where a lower-priority button requires slightly more emphasis than an outline would give, such as Next in an onboarding flow.outlined: The outlined style is ideal for medium-emphasis buttons which contain actions that are important, but aren’t the primary action in a product.text: The text button style should be used for the lowest priority actions, especially when presenting multiple options. There is no toggle text button.
size's' | 'xs' | 'm' | 'l' | 'xl'Default's'Five size recommendations: extra small, small, medium, large, and extra large
shape'round' | 'square'Default'round'Two shape options: round and square
round: Buttons with a round shape have containers with fully rounded corners.square: Buttons with a square shape have containers with more subtle rounding that changes based on button size.
Corner sizes of a square button: XS 12dp, S 12dp, M 16dp, L 28dp, XL 28dp.
In addition to changing shape when pressed, toggle buttons also change the resting shape from round (unselected) to square (selected). If the resting unselected shape is square, the selected shape should be round.
hasShapeMorphbooleanDefaultfalseShape morphs when pressed
When pressed, buttons can morph to become more square. Both round and square buttons should have the same pressed shape. The corner radius value differs for each button size.
Corner sizes of the pressed state: XS 8dp, S 8dp, M 12dp, L 16dp, XL 16dp.
In addition to changing shape when pressed, toggle buttons also change the resting shape from round (unselected) to square (selected).
isPendingbooleanWhether the button is in a pending state. This disables press and hover events while retaining focusability, and announces the pending state to screen readers.
isSelectablebooleanDefaultfalseTwo variants: default and toggle
Toggle buttons should be used for binary selections, such as Save or Favorite. When toggle buttons are pressed, they can change color, shape, and labels.
By default, toggle buttons change from round to square when selected.
There is no toggle text button.
Props of Button (50)
formstringThe
<form>element to associate the button with. The value of this attribute must be the id of a<form>in the same document. See MDN.idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
onClick(e: MouseEvent<FocusableElement>) => voidNot recommended – use
onPressinstead.onClickis an alias foronPressprovided for compatibility with other libraries.onPressprovides additional event details for non-mouse interactions.isDisabledbooleanWhether the button is disabled.
onPress(e: PressEvent) => voidHandler that is called when the press is released over the target.
onPressStart(e: PressEvent) => voidHandler that is called when a press interaction starts.
onPressEnd(e: PressEvent) => voidHandler that is called when a press interaction ends, either over the target or when the pointer leaves the target.
onPressChange(isPressed: boolean) => voidHandler that is called when the press state changes.
onPressUp(e: PressEvent) => voidHandler that is called when a press is released over the target, regardless of whether it started on the target or not.
autoFocusbooleanWhether the element should receive focus on render.
onFocus(e: FocusEvent<Target>) => voidHandler that is called when the element receives focus.
onBlur(e: FocusEvent<Target>) => voidHandler that is called when the element loses focus.
onFocusChange(isFocused: boolean) => voidHandler that is called when the element's focus status changes.
onKeyDown(e: KeyboardEvent) => voidHandler that is called when a key is pressed.
onKeyUp(e: KeyboardEvent) => voidHandler that is called when a key is released.
hrefstringA URL to link to if elementType="a".
targetstringThe target window for the link.
relstringThe relationship between the linked resource and the current page. See MDN.
elementTypeT | JSXElementConstructor<any>Default'button'The HTML element or React element used to render the button, e.g. 'div', 'a', or
RouterLink.aria-disabled'true' | 'false' | booleanIndicates whether the element is disabled to users of assistive technology.
aria-expanded'true' | 'false' | booleanIndicates whether the element, or another grouping element it controls, is currently expanded or collapsed.
aria-haspopup'dialog' | 'menu' | 'true' | 'false' | 'grid' | 'listbox' | 'tree' | booleanIndicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element.
aria-controlsstringIdentifies the element (or elements) whose contents or presence are controlled by the current element.
aria-pressed'true' | 'false' | 'mixed' | booleanIndicates the current "pressed" state of toggle buttons.
aria-current'time' | 'true' | 'false' | 'page' | 'step' | 'location' | 'date' | booleanIndicates whether this element represents the current item within a container or set of related elements.
type'button' | 'submit' | 'reset'Default'button'The behavior of the button when used in an HTML form.
preventFocusOnPressbooleanWhether to prevent focus from moving to the button when pressing it.
Caution, this can make the button inaccessible and should only be used when alternative keyboard interaction is provided, such as ComboBox's MenuTrigger or a NumberField's increment/decrement control.
formActionButtonHTMLAttributes<HTMLButtonElement>['formAction']The URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner.
formEncTypestringIndicates how to encode the form data that is submitted.
formMethodstringIndicates the HTTP method used to submit the form.
formNoValidatebooleanIndicates that the form is not to be validated when it is submitted.
formTargetstringOverrides the target attribute of the button's form owner.
namestringSubmitted as a pair with the button's value as part of the form data.
valuestringThe value associated with the button's name when it's submitted with the form data.
excludeFromTabOrderbooleanWhether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available.
onHoverStart(e: HoverEvent) => voidHandler that is called when a hover interaction starts.
onHoverEnd(e: HoverEvent) => voidHandler that is called when a hover interaction ends.
onHoverChange(isHovering: boolean) => voidHandler that is called when the hover state changes.
slotstring | nullA slot name for the component. Slots allow the component to receive props from a parent component. An explicit
nullvalue indicates that the local props completely override all props received from a parent.refRef<ComponentRef<T> & Element>classNameClassNameOrFunction<ButtonRenderProps>styleStyleOrFunction<ButtonRenderProps>childrenChildrenOrFunction<ButtonRenderProps>isSelectedbooleandefaultSelectedbooleanonChangenever