Skip to content

Material 3

Components

FAB

Floating action buttons (FABs) help people take primary actions.

AAli ConnorsBrunch this weekend?
SSandra AdamsPhotos from the summer trip
TTrevor HansenBirthday gift ideas
BBritta HoltRecipe to try on Friday
JJonas MeyerSee you at eight

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

  1. label
SlotChildUse
labelA TextMakes 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.

SizeHeightIcon
baseline56dp24dp
medium80dp28dp
large96dp36dp
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.

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 isExpanded to the Fab as its isExtended. An extended FAB widens the expanded rail.
  • Toolbar: Put a Fab beside a floating toolbar, not in it, 8dp away (gap-space-100). Choose its color, such as secondary-container or tertiary-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 Fab with a tooltip, put it first in the TooltipTrigger.

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: or pressed:. In your own CSS, select the same states by their data attributes: data-hovered, data-pressed, data-focused, data-focus-visible and data-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

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.