Skip to content

Material 3

Components

Loading indicator

Loading indicators show the progress of a process for a short wait time.

9:30

Library

Loading indicators show that an activity is in progress during a short wait. Use one when progress cannot be detected, or when the length of the activity does not need to be shown. For a short process, between 200ms and 5s, prefer it to a circular progress indicator.

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

Basic loading indicator

The indicator animates for as long as it is rendered: render it while the activity is in progress, and remove it when the activity ends. Name it with an aria-label.

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

export default function Basic() {
  return <LoadingIndicator aria-label="Loading news article" />
}

It is always indeterminate, so it takes no value, minValue, maxValue, isIndeterminate, formatOptions or valueLabel. To show how far a process has come, use a progress indicator.

The LoadingIndicator comes in two configurations. Without a container (the default), it is for use directly on a surface. Set isContained to show the indicator in a round container, for use over other content.

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

export default function Contained() {
  return (
    <>
      <LoadingIndicator aria-label="Loading news article" />
      <LoadingIndicator aria-label="Refreshing page" isContained />
    </>
  )
}

Uncontained loading indicator

While a page or a container loads, center the indicator on it. Directly on a surface, the indicator needs no container.

My episodes
import { LoadingIndicator } from '@material-web/react'

export default function Centered() {
  return (
    <div className="flex h-72 w-80 flex-col rounded-shape-lg bg-surface-container-highest">
      <div className="p-space-200 text-title-lg">My episodes</div>
      <div className="flex flex-1 items-center justify-center">
        <LoadingIndicator aria-label="Loading my episodes" />
      </div>
    </div>
  )
}

Contained loading indicator

Over other content, such as content that is being refreshed, set isContained so that the indicator stands out from the content behind it. Use the container with pull-to-refresh.

Pilot
Episode 1
The long way home
Episode 2
Static
Episode 3
Low tide
Episode 4
import { LoadingIndicator } from '@material-web/react'

const episodes = ['Pilot', 'The long way home', 'Static', 'Low tide']

export default function OverContent() {
  return (
    <div className="relative w-80 rounded-shape-lg bg-surface-container-highest py-space-100">
      {episodes.map((episode, index) => (
        <div key={episode} className="flex items-center gap-space-200 px-space-200 py-space-100">
          <div className="size-14 shrink-0 rounded-shape-md bg-tertiary-container" />
          <div className="min-w-0">
            <div className="truncate text-body-lg">{episode}</div>
            <div className="text-body-md text-on-surface-variant">Episode {index + 1}</div>
          </div>
        </div>
      ))}
      <div className="absolute inset-x-0 top-space-300 flex justify-center">
        <LoadingIndicator aria-label="Refreshing my episodes" isContained />
      </div>
    </div>
  )
}

Label

Name the indicator with aria-label or aria-labelledby, or with a Label from react-aria-components among its children. Write a label that describes its purpose, such as Loading news article or Refreshing page.

Children are rendered inside the indicator, which cuts off what reaches past its box. To show a label, write it beside the indicator and name the indicator by it with aria-labelledby.

Loading my episodes
import { LoadingIndicator } from '@material-web/react'
import { useId } from 'react'

export default function Labelled() {
  const id = useId()
  return (
    <div className="flex items-center gap-space-200">
      <LoadingIndicator aria-labelledby={id} />
      <span id={id} className="text-body-lg">
        Loading my episodes
      </span>
    </div>
  )
}

Color

The indicator takes the text color: primary, or on-primary-container in its primary-container container. To use other color roles, pass their classes in className: a text color for the indicator, and a background color for the container.

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

export default function Color() {
  return (
    <>
      <LoadingIndicator aria-label="Loading, tertiary" className="text-tertiary" />
      <LoadingIndicator aria-label="Loading, error" className="text-error" />
      <LoadingIndicator
        aria-label="Loading, tertiary container"
        isContained
        className="bg-tertiary-container text-on-tertiary-container"
      />
    </>
  )
}

Sizes

Set size in dp, from 24 to 240. The default is 48, and a size outside the range is brought back to its nearest end. The container scales with the indicator. The size does not grow or shrink with the text size around it.

To size the indicators inside an element, leave size out and set the --loading-indicator-size custom property, a plain number, on that element.

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

export default function Sizes() {
  return (
    <>
      <LoadingIndicator aria-label="Loading, 24dp" size={24} />
      <LoadingIndicator aria-label="Loading, 48dp" size={48} />
      <LoadingIndicator aria-label="Loading, 96dp" size={96} />
      <LoadingIndicator aria-label="Loading, 96dp, contained" size={96} isContained />
      <LoadingIndicator aria-label="Loading, 240dp" size={240} />
    </>
  )
}

In a button

In a button whose action takes a few seconds, give the indicator the label's color with className="text-current".

The button's label already says what is happening, so hide the indicator from assistive technology by wrapping it in an element with aria-hidden, and still give the indicator an aria-label.

Note: An aria-hidden set on the indicator itself is not applied.

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

export default function InButton() {
  return (
    <>
      <Button>
        <span aria-hidden className="flex">
          <LoadingIndicator aria-label="Checking for updates" size={24} className="text-current" />
        </span>
        Checking for updates
      </Button>
      <Button variant="tonal">
        <span aria-hidden className="flex">
          <LoadingIndicator aria-label="Validating" size={24} className="text-current" />
        </span>
        Validating
      </Button>
      <Button variant="outlined">
        <span aria-hidden className="flex">
          <LoadingIndicator aria-label="Saving" size={24} className="text-current" />
        </span>
        Saving
      </Button>
    </>
  )
}

While the button's action is in progress, set its isPending instead: a pending button shows a loading indicator of its own, alone, and ignores presses. See Loading.

Delaying appearance

A loading indicator is for a wait of 200ms or more. To keep it from flashing on an activity that ends sooner, render it only once the activity has run for that long. In this example, the indicator appears 200ms after a load starts, so the shorter load never shows it.

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

export default function Delay() {
  const [status, setStatus] = useState<'idle' | 'loading' | 'slow'>('idle')

  const load = (duration: number) => {
    if (status !== 'idle') return
    setStatus('loading')
    // The indicator appears only if the activity is still running after 200ms.
    const delay = setTimeout(() => setStatus('slow'), 200)
    setTimeout(() => {
      clearTimeout(delay)
      setStatus('idle')
    }, duration)
  }

  return (
    <div className="flex flex-col items-center gap-space-300">
      <div className="size-12">
        {status === 'slow' && <LoadingIndicator aria-label="Loading results" />}
      </div>
      <div className="flex gap-space-100">
        <Button variant="tonal" onPress={() => load(100)}>
          Load for 100ms
        </Button>
        <Button variant="tonal" onPress={() => load(3000)}>
          Load for 3s
        </Button>
      </div>
    </div>
  )
}

Customization

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

  • Size: Set size, or the --loading-indicator-size custom property on an element around the indicators, as in Sizes.
  • Color: Pass a text color class, and a background color class on a contained indicator, as in Color.
  • Functions: className, style and children can be functions of the indicator's state, in which isIndeterminate is true and percentage and valueText are undefined. See Children as a function.
  • Every indicator: To restyle all the indicators at once, change the theme.

Limitations

Hidden indicators

The indicator does not apply an aria-hidden of its own. To hide it from assistive technology, set aria-hidden on an element around it, as in a button.