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.
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.
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.
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-sizecustom 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,styleandchildrencan be functions of the indicator's state, in whichisIndeterminateistrueandpercentageandvalueTextare 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.
Use cases
Material lists what people should be able to do with assistive technology:
- Navigate to the loading indicator
- Understand what progress the indicator is communicating
- Initiate a content refresh without relying on a gesture
Interaction and style
- Color contrast: The indicator needs 3:1 contrast with the surface behind it, and the container does not. Keep that contrast when you change its colors, and against the button's container when it is in a button.
- Motion: The indicator keeps moving with reduced motion, as its motion is the only sign of the activity.
- Forced colors: In forced colors, the indicator is drawn in the system's text color.
- Refreshing: A swipe alone, such as a pull to refresh, is not accessible. Give people a way to refresh with a single pointer too, such as a refresh button in the app bar, in a menu or beside the content.
Labeling elements
- The indicator has the
progressbarrole. It is not a tab stop, and it reports no value. - The indicator needs a name: set
aria-labeloraria-labelledby, or put aLabelamong its children. Material advises a name that says what is loading, such as Loading news article or Refreshing page. - To hide the indicator from assistive technology, set
aria-hiddenon an element around it.
isContainedbooleanDefaultfalseThe container should be visible when the loading indicator is placed over other content. This helps it stand out better by giving it a stronger contrast. It’s not needed when the loading indicator is placed directly on a surface.
When the container is visible, the active indicator should change color from primary to on-primary-container.
The container should be used with pull-to-refresh behavior.
sizenumberDefault48Loading indicators default to 48dp, but the size is flexible. It should be between 24dp to 240dp, depending on the placement and the breakpoint. Avoid exceeding the minimum and maximum sizes.
The ratio between the container and the active indicator stays the same when resizing the loading indicator.
Props of LoadingIndicator (11)
classNameClassNameOrFunction<ProgressBarRenderProps>Default'react-aria-ProgressBar'The CSS className for the element. A function may be provided to compute the class based on component state.
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.
childrenChildrenOrFunction<T>The children of the component. A function may be provided to alter the children based on component state.
styleCSSProperties | (values: ProgressBarRenderProps & { defaultStyle: CSSProperties; }) => CSSProperties | undefinedThe inline style for the element. A function may be provided to compute the style based on component state.
renderDOMRenderFunction<'div', ProgressBarRenderProps>Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.
Requirements:
- You must render the expected element type (e.g. if
<button>is expected, you cannot render an<a>). - Only a single root DOM element can be rendered (no fragments).
- You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
- You must render the expected element type (e.g. if
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<HTMLDivElement>
LoadingIndicator also takes the 70 attributes and events of the element it renders.