Use a floating action button (FAB) for the most important action on a screen. FABs come in two types:
- A FAB, with an icon alone
- An extended FAB, with a label beside the icon
To open related actions from a FAB, use a FAB menu, whose FAB is a FabMenuButton. For actions of lower emphasis, use a button.
import { Fab, Icon } from '@material-web/react'
import { Text } from 'react-aria-components'Basic FAB
Write the icon as the FAB's child. Add a Text with slot="label" to make an extended FAB.
import { Fab, Icon } from '@material-web/react'
import { Text } from 'react-aria-components'
export default function Basic() {
return (
<>
<Fab aria-label="Compose a new message">
<Icon name="edit" />
</Fab>
<Fab>
<Icon name="edit" />
<Text slot="label">Compose</Text>
</Fab>
</>
)
}Set isDisabled to disable the FAB. It cannot be pressed or focused.
import { Fab, Icon } from '@material-web/react'
import { Text } from 'react-aria-components'
export default function Disabled() {
return (
<>
<Fab aria-label="Compose a new message" isDisabled>
<Icon name="edit" />
</Fab>
<Fab isDisabled>
<Icon name="edit" />
<Text slot="label">Compose</Text>
</Fab>
</>
)
}Extended FAB
Put a Text with slot="label" in the FAB to make an extended FAB. The label names the FAB. size sets the height, and the width follows the label, which stays on one line. The baseline size is the small extended FAB.
import { Fab, Icon } from '@material-web/react'
import { Text } from 'react-aria-components'
export default function Extended() {
return (
<>
<Fab size="baseline">
<Icon name="edit" />
<Text slot="label">Compose</Text>
</Fab>
<Fab size="medium">
<Icon name="edit" />
<Text slot="label">Compose</Text>
</Fab>
<Fab size="large">
<Icon name="edit" />
<Text slot="label">Compose</Text>
</Fab>
</>
)
}The icon is optional on an extended FAB.
import { Fab } from '@material-web/react'
import { Text } from 'react-aria-components'
export default function LabelOnly() {
return (
<Fab color="tertiary-container">
<Text slot="label">New event</Text>
</Fab>
)
}Collapsing and extending
Set isExtended to false to hide the label, and to true (the default) to show it. Keep the same children in both states, and change isExtended, such as on scroll. A collapsed FAB is not named by its label, so give a FAB that collapses an aria-label as well.
In this example, the extended FAB collapses while the list scrolls down, and extends again once it scrolls back up.
- Message 1
- Message 2
- Message 3
- Message 4
- Message 5
- Message 6
- Message 7
- Message 8
- Message 9
- Message 10
- Message 11
- Message 12
- Message 13
- Message 14
- Message 15
- Message 16
- Message 17
- Message 18
- Message 19
- Message 20
- Message 21
- Message 22
- Message 23
- Message 24
import { Fab, Icon } from '@material-web/react'
import { useRef, useState, type UIEvent } from 'react'
import { Text } from 'react-aria-components'
const messages = Array.from({ length: 24 }, (_, index) => `Message ${index + 1}`)
export default function Collapse() {
const [isExtended, setExtended] = useState(true)
const lastTop = useRef(0)
const onScroll = (event: UIEvent<HTMLUListElement>) => {
const top = event.currentTarget.scrollTop
if (Math.abs(top - lastTop.current) < 8) return
setExtended(top < lastTop.current)
lastTop.current = top
}
return (
<div className="relative h-96">
<ul
tabIndex={0}
className="focus-ring-inset m-0 box-border h-full list-none overflow-y-auto p-space-100"
onScroll={onScroll}
>
{messages.map((message) => (
<li key={message} className="p-space-200 text-body-lg text-on-surface">
{message}
</li>
))}
</ul>
<Fab
isExtended={isExtended}
aria-label="Compose"
className="absolute right-space-200 bottom-space-200"
>
<Icon name="edit" />
<Text slot="label">Compose</Text>
</Fab>
</div>
)
}Slots
- label
| Slot | Child | Use |
|---|---|---|
label | A Text | Makes the FAB an extended FAB, and names it. See Extended FAB. |
Note: A Text in a FAB must have slot="label". A Text with another slot, or with none, throws an error.
Icons
Any icon works as the FAB's child: Icon, an icon from an SVG library, or your own component. An Icon is sized to the FAB. Give any other icon the size listed under Sizes. See Composition.
Handling presses
Handle a press with onPress, in place of onClick. A FAB takes the same press events as a Button. See Handling presses.
Loading
Set isPending while the FAB's action is in progress, as on a button. A pending FAB shows a loading indicator in place of its icon and label, sized to the FAB. It keeps its size and colors, so the layout around it does not shift.
import { Fab, Icon } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'
export default function Loading() {
const [isUploading, setUploading] = useState(false)
const upload = async () => {
setUploading(true)
await new Promise((resolve) => setTimeout(resolve, 3000))
setUploading(false)
}
return (
<Fab isPending={isUploading} onPress={upload}>
<Icon name="upload" />
<Text slot="label">Upload</Text>
</Fab>
)
}- Presses: A pending FAB ignores presses and shows no hover. As a submit button, it does not submit its form, and as a link, it does not navigate.
- 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
Set color to one of six: primary-container (the default), secondary-container, tertiary-container, primary, secondary or tertiary. They provide the same legibility and functionality, so choose one by style alone.
import { Fab, Icon } from '@material-web/react'
const colors = [
'primary-container',
'secondary-container',
'tertiary-container',
'primary',
'secondary',
'tertiary',
] as const
export default function Colors() {
return (
<>
{colors.map((color) => (
<Fab key={color} color={color} aria-label={`Create, ${color}`}>
<Icon name="add" />
</Fab>
))}
</>
)
}Sizes
For a larger FAB, set size to baseline (the default), medium or large. An Icon child is sized to match. Material Design recommends medium the most.
| Size | Height | Icon |
|---|---|---|
baseline | 56dp | 24dp |
medium | 80dp | 28dp |
large | 96dp | 36dp |
import { Fab, Icon } from '@material-web/react'
export default function Sizes() {
return (
<>
<Fab size="baseline" aria-label="Create, baseline">
<Icon name="add" />
</Fab>
<Fab size="medium" aria-label="Create, medium">
<Icon name="add" />
</Fab>
<Fab size="large" aria-label="Create, large">
<Icon name="add" />
</Fab>
</>
)
}Placement
A FAB does not position itself. Place it with your layout, such as className="fixed right-space-200 bottom-space-200 z-fab" for the corner of the window, or absolute in a positioned box, as in the example above. See Stacking order for z-fab.
A FAB rests at elevation level 3, and rises to level 4 on hover.
Rendering non-native buttons
Set elementType to render the FAB as an element other than a <button>. The element gets the button role and a tab stop, as on a button.
Links
Set elementType="a" to render the FAB 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 { Fab, Icon } from '@material-web/react'
import { Text } from 'react-aria-components'
export default function LinkFab() {
return (
<Fab
color="secondary-container"
elementType="a"
href="https://m3.material.io/components/floating-action-button"
target="_blank"
rel="noreferrer"
>
<Icon name="open_in_new" />
<Text slot="label">Read the spec</Text>
</Fab>
)
}With other components
- Navigation rail: In the header of a navigation rail, pass the rail's
isExpandedto theFabas itsisExtended. An extended FAB widens the expanded rail. - Toolbar: Put a
Fabbeside a floating toolbar, not in it, 8dp away (gap-space-100). Choose itscolor, such assecondary-containerortertiary-container. - Snackbar: Show a snackbar above a FAB. Set
className="bottom-22"on the snackbar's region to put it above a FAB that is 16dp from the corner. - Tooltip: To describe a
Fabwith a tooltip, put it first in theTooltipTrigger.
Customization
Pass classes in className to restyle one FAB. A class of yours replaces the FAB's own class for the same property, so a container color and its on-color replace those of color. See Styling for the classes, the states and the theme.
- States: Style a state with its variant, such as
hover:orpressed:. In your own CSS, select the same states by their data attributes:data-hovered,data-pressed,data-focused,data-focus-visibleanddata-disabled. See States. - Every FAB: To restyle all the FABs at once, change the theme.
Children as a function
Pass a function as children to render them from the FAB's state: isHovered, isPressed, isFocused, isFocusVisible, isDisabled and isPending. The function must be the only child. To return several children, wrap them in a fragment. The label is still a Text with slot="label".
import { Fab, Icon } from '@material-web/react'
import { Text } from 'react-aria-components'
export default function FunctionChildren() {
return (
<Fab>
{({ isHovered, isPressed }) => (
<>
<Icon name="edit" className={isHovered || isPressed ? 'icon-filled' : undefined} />
<Text slot="label">Compose</Text>
</>
)}
</Fab>
)
}className and style can be functions of the same state. The state's type is exported as FabRenderProps. See Children as a function.
Limitations
Link role
A FAB 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.
Disabled FABs
A disabled FAB cannot be focused and reports no hover, so it shows no tooltip.
Pending content
A pending FAB shows only its loading indicator. Children written for the pending state, such as an Uploading… 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, with a FAB and with an extended FAB:
- Navigate to and activate the FAB
- Perform an action with the FAB
- Expand and minimize an extended FAB
- Navigate to and activate the extended FAB
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 is 56dp or larger in both directions, above the 48dp minimum of a touch target, at every density.
- Color contrast: The icon needs 3:1 contrast with the container. Every
colorkeeps the icon at 3:1 or more, and an extended FAB's label at 4.5:1 or more, in the light and dark schemes at every contrast level. Keep that contrast when you change its colors. - Text size: An extended FAB's label stays on one line, and the FAB grows wider with it.
- Motion: With reduced motion, collapsing and extending, and the changes of color and shadow, are instant.
- Forced colors: In forced colors, the FAB keeps an edge.
- Placement: Place the FAB with your layout, as under Placement. Material advises the lower right corner of compact and medium windows, and the upper left region of expanded ones, such as the header of a navigation rail, where a screen reader reaches it sooner. Don't let the FAB cover a control and its focus ring entirely: it can cover part of the control while the focus ring stays visible.
Focus
- The FAB is one tab stop. An extended FAB's icon and label are one focusable element, as Material advises.
- Material advises putting the FAB early in the focus order: on a phone, after the app bar and the navigation bar, before the rest of the content. The focus follows the order the elements are written in, wherever they are placed on the screen, so write the
Fabthere and place it with itsclassName. - A disabled FAB cannot be focused. Material advises against disabling a FAB: when its action is unavailable, leave the FAB out in place of setting
isDisabled. - A pending FAB keeps the focus, so a keyboard user stays on it while the action runs.
Keyboard navigation
| Keys | Actions |
|---|---|
| Tab | Move the focus to the FAB |
| Space or Enter | Press the focused FAB |
Labeling elements
- The label names an extended FAB. The icon is hidden from assistive technology, so the label alone names it.
- Give a FAB with an icon alone an
aria-labelor anaria-labelledbythat describes the action it performs, such as Compose a new message, as Material advises. The types do not require one. - A collapsed FAB hides its label from assistive technology, so it needs an
aria-labeltoo. - An
aria-labelnames an extended FAB in place of its label. Material advises one that starts with the first word of the label, such as Create a new invite for the label Create, and an icon that matches the label, such as a Compose icon for the label Compose. - Material advises a tooltip that shows the FAB's label while it has the focus. Put the
Fabfirst in aTooltipTrigger: keyboard focus opens its tooltip at once. The tooltip describes the FAB and does not name it, so a FAB with an icon alone still needs itsaria-label. An extended FAB needs no tooltip, since its label is visible. - A pending FAB reports that it is unavailable with
aria-disabled. Its loading indicator is hidden from assistive technology, so its label oraria-labelstill names it. - When a focused FAB becomes pending, or stops being pending, screen readers announce it.
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.
color'primary-container' | 'secondary-container' | 'tertiary-container' | 'primary' | 'secondary' | 'tertiary'Default'primary-container'Six color options: the primary, secondary and tertiary containers, and the primary, secondary and tertiary roles themselves
FABs can use several combinations of color and on-color styles, such as primary and on-primary. The following color mappings provide the same legibility and functionality, so the color mapping you use depends on style alone.
size'baseline' | 'medium' | 'large'Default'baseline'There are three FAB sizes:
FAB
Medium FAB (most recommended)
Large FAB
Props of Fab (48)
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>isExtendedboolean