What is @material/linear-progress?
@material/linear-progress is a Material Design component for displaying linear progress indicators. It provides a visual representation of the progress of a task, making it easy to integrate with React applications and ensuring a consistent look and feel with other Material Design components.
What are @material/linear-progress's main functionalities?
Indeterminate Progress
This feature allows you to display an indeterminate progress bar, which is useful when the duration of the task is unknown.
import React from 'react';
import { LinearProgress } from '@material-ui/core';
function IndeterminateProgress() {
return <LinearProgress />;
}
export default IndeterminateProgress;
Determinate Progress
This feature allows you to display a determinate progress bar, which is useful when you can track the progress of a task.
import React, { useState } from 'react';
import { LinearProgress } from '@material-ui/core';
function DeterminateProgress() {
const [progress, setProgress] = useState(0);
React.useEffect(() => {
const timer = setInterval(() => {
setProgress((oldProgress) => {
if (oldProgress === 100) {
return 0;
}
const diff = Math.random() * 10;
return Math.min(oldProgress + diff, 100);
});
}, 500);
return () => {
clearInterval(timer);
};
}, []);
return <LinearProgress variant="determinate" value={progress} />;
}
export default DeterminateProgress;
Buffer Progress
This feature allows you to display a buffer progress bar, which is useful when you want to show both the progress and the buffer of a task.
import React, { useState } from 'react';
import { LinearProgress } from '@material-ui/core';
function BufferProgress() {
const [progress, setProgress] = useState(0);
const [buffer, setBuffer] = useState(10);
React.useEffect(() => {
const timer = setInterval(() => {
setProgress((oldProgress) => {
if (oldProgress === 100) {
return 0;
}
const diff = Math.random() * 10;
return Math.min(oldProgress + diff, 100);
});
setBuffer((oldBuffer) => {
if (oldBuffer === 100) {
return 10;
}
const diff = Math.random() * 10;
return Math.min(oldBuffer + diff, 100);
});
}, 500);
return () => {
clearInterval(timer);
};
}, []);
return <LinearProgress variant="buffer" value={progress} valueBuffer={buffer} />;
}
export default BufferProgress;
Query Progress
This feature allows you to display a query progress bar, which is useful when you want to indicate that a task is being queried.
import React from 'react';
import { LinearProgress } from '@material-ui/core';
function QueryProgress() {
return <LinearProgress variant="query" />;
}
export default QueryProgress;
Other packages similar to @material/linear-progress
react-loader-spinner
react-loader-spinner provides a collection of customizable loading spinners for React applications. It offers a variety of spinner types, including linear progress bars, and is highly customizable in terms of size, color, and animation speed. Compared to @material/linear-progress, it provides more variety in loading indicators but may not adhere strictly to Material Design guidelines.
react-progressbar.js
react-progressbar.js is a React wrapper for ProgressBar.js, a library for creating animated progress bars. It supports linear and circular progress bars with customizable animations and styles. While it offers more animation options compared to @material/linear-progress, it may require more effort to integrate seamlessly with Material Design components.
nprogress
nprogress is a simple and lightweight progress bar library for web applications. It is often used for indicating page load progress and can be easily integrated with React. However, it lacks the variety of progress bar types and customization options available in @material/linear-progress.
Linear Progress
The MDC Linear Progress component is a spec-aligned linear progress indicator component adhering to the
Material Design progress & activity requirements.
Design & API Documentation
Installation
npm install @material/linear-progress
Basic Usage
HTML Structure
<div role="progressbar" class="mdc-linear-progress" aria-label="Example Progress Bar" aria-valuemin="0" aria-valuemax="1" aria-valuenow="0">
<div class="mdc-linear-progress__buffer">
<div class="mdc-linear-progress__buffer-bar"></div>
<div class="mdc-linear-progress__buffer-dots"></div>
</div>
<div class="mdc-linear-progress__bar mdc-linear-progress__primary-bar">
<span class="mdc-linear-progress__bar-inner"></span>
</div>
<div class="mdc-linear-progress__bar mdc-linear-progress__secondary-bar">
<span class="mdc-linear-progress__bar-inner"></span>
</div>
</div>
Accessibility
Progress bars conform to the WAI-ARIA Progressbar Specification. The supported ARIA attributes for this progress bar are:
Attribute | Description |
---|
aria-label | Label indicating how the progress bar should be announced to the user. |
aria-valuemin | The minimum numeric value of the progress bar, which should always be 0 . |
aria-valuemax | The maximum numeric value of the progress bar, which should always be 1 . |
aria-valuenow | A numeric value between aria-valuemin and aria-valuemax indicating the progress value of the primary progress bar. This attribute is removed in indeterminate progress bars. |
Note that aria-label
, aria-valuemin
, and aria-valuemax
are static and must be configured in the HTML. aria-valuenow
is updated dynamically by the foundation when the progress value is updated in determinate progress bars.
RTL Localization
The direction of the progress bar follows the dir
attribute on its nearest ancestor. For example, setting dir="rtl"
on the progress root reverses the direction of the indicator.
Where multiple dir
attributes are specified in the tree, the one on the progress bar root takes precedence.
Styles
@use "@material/linear-progress";
@include linear-progress.core-styles;
JavaScript Instantiation
import { MDCLinearProgress } from '@material/linear-progress';
const linearProgress = new MDCLinearProgress(document.querySelector('.mdc-linear-progress'));
See Importing the JS component for more information on how to import JavaScript.
CSS Modifiers
The provided modifiers are:
Class | Description |
---|
mdc-linear-progress--indeterminate | Puts the linear progress indicator in an indeterminate state. |
mdc-linear-progress--closed | Hides the linear progress indicator. |
Sass Mixins
Mixin | Description |
---|
bar-color($color) | Sets the color of the progress bar |
buffer-color($color) | Sets the color of the buffer bar and dots |
Using the Foundation Class
MDC Linear Progress ships with an MDCLinearProgressFoundation
class that external frameworks and libraries can
use to integrate the component. As with all foundation classes, an adapter object must be provided.
The adapter for linear progress must provide the following functions, with correct signatures:
Method Signature | Description |
---|
addClass(className: string) => void | Adds a class to the root element. |
removeAttribute(attributeName: string) => void | Removes the specified attribute from the root element. |
removeClass(className: string) => void | Removes a class from the root element. |
hasClass(className: string) => boolean | Returns boolean indicating whether the root element has a given class. |
forceLayout() => void | Force-trigger a layout on the root element. This is needed to restart animations correctly. |
setAttribute(attributeName: string, value: string) => void | Sets the specified attribute on the root element. |
setBufferBarStyle(styleProperty: string, value: string) => void | Sets the inline style on the buffer bar. |
setPrimaryBarStyle(styleProperty: string, value: string) => void | Sets the inline style on the primary bar. |
attachResizeObserver(callback: ResizeObserverCallback) => ResizeObserver | null | Returns a ResizeObserver that has had observe called on the root with the given callback (for animation performance gains on modern browsers). null if ResizeObserver is not implemented or polyfilled. |
setStyle(styleProperty: string, value: string) => void | Sets the inline style on the root element. |
getWidth() => number | Returns the width of the root. |
MDCLinearProgressFoundation API
MDC Linear Progress Foundation exposes the following methods:
Method Signature | Description |
---|
setDeterminate(value: boolean) => void | Toggles the component between the determinate and indeterminate state. |
isDeterminate() => boolean | Whether or not the component is in determinate state. |
setProgress(value: number) => void | Sets the progress bar to this value. Value should be between [0, 1]. |
getProgress() => number | The current progress value in the interval [0,1]. |
setBuffer(value: number) => void | Sets the buffer bar to this value. Value should be between [0, 1]. |
getBuffer() => number | The current buffer value in the interval [0,1]. |
open() => void | Puts the component in the open state. |
close() => void | Puts the component in the closed state. |
isClosed() => boolean | Whether or not the progress indicator is closed. |
MDCLinearProgress API
MDC Linear Progress exposes the following methods:
Method Signature | Description |
---|
set determinate(value: boolean) => void | Toggles the component between the determinate and indeterminate state. |
set progress(value: number) => void | Sets the progress bar to this value. Value should be between [0, 1]. |
set buffer(value: number) => void | Sets the buffer bar to this value. Value should be between [0, 1]. |
open() => void | Puts the component in the open state. |
close() => void | Puts the component in the closed state. |
11.0.0 (2021-04-15)
Bug Fixes
- banner: Use role alertdialog. (a07b6d4)
- button: add missing feature-targeting import (71fe9a0)
- button: Fixed button's icon size scaling on browser zoom (bc104ba)
- chips: Expose deprecated resources in top-level TypeScript file (67d780c)
- chips: Fix incorrect references between deprecated and non-deprecated resources (f8579b7)
- chips: Make chips wrap by default (24255c4)
- chips: Remove obsolete chips resources now in chips/deprecated/* (87ac2fd)
- chips: Remove obsolete resources (40dd242)
- chips: rename deprecated trailing action classes (48f4b67)
- chips: Un-remove obsolete chips resources now in chips/deprecated/* (7cf6782)
- chips: Use deprecated chips in autoinit (d2a39d3)
- circular-progress: add annotation (06dead2)
- dialog: Add transparent border to dialog surface for HCM support. (b2fa996)
- dialog: Remove the unnecessary border on the dialog title when not needed, this adds an extra line in the UI on high contrast mode. With margins it is possible to keep the previous spacing and only add the border when needed. (3344d12)
- dom: do not cache focusable elements in focus-trap (7899e0f)
- fab: add alternate decorator only when necessary (0fd56a8)
- fab: Apply extended shape radius in Extended FAB's theme mixin (81911b7)
- list: Correcting the selector mapping for CHILD_ELEMENTS_TO_TOGGLE_TABINDEX and FOCUSABLE_CHILD_ELEMENTS. (8943b99), closes #6829 #6829
- list: do not activate typeahead on certain modifier keys (f1b1fd5)
- progress-indicators: hide from screenreaders on close (d3a6862)
- ripple: Update states-selector() to use
:active:active
to match active specificity styles. (faa7d32) - select: do not conduct anchor typeahead when modifier keys pressed (6f678a9)
- select: set hidden input value before firing change event (2d6ba2c), closes #6904
- shape: duplication bug with nested custom properties (f77a4dd)
- slider: Fire custom
input
event on input change (i.e. value change via keyboard), mirroring the native input
event behavior for range inputs. (ec8f846) - slider: Fix #quantize to use min value as the baseline. (0f358dd)
- slider: Fix JS floating point rounding errors by rounding values to a set number of decimal places based on the step size. (6072ed6)
- slider: Fix track height. (67eb0df)
- slider: Improve HCM borders, add missing @noflip annotations. (e7202cb)
- slider: Modify behavior such that for range sliders, presses in the middle of the range change the value (of the closest thumb). This provides a single-pointer alternative option to an otherwise gesture-based interaction. (0b8cff7)
- slider: Throttle slider UI updates. (7d6a4bb)
- slider: Throw error for invalid initial values based on the step. (3955d8d)
- tab: Update ripple adapter to reflect sass ripple-target. (97c4d40)
- theme: do not emit when theme.property() replacements are null (aa0aaf0)
- theme: parsing error when @import-ing mdc-theme (b62b126)
- theme: replace works for multiple replacements (95322b1)
- update README to correct links. (71e615b)
- tooltip: flip precedence of data-tooltip-id and aria-describedby when finding TT id (b2d22df)
- typography: do not emit styles when setting null from global variable (f5f1b61)
Code Refactoring
- snackbar: Update a11y structure (c60449b)
- tooltip: Moved the anchor element blur event listener from the component to within the foundation. (0b4a4b2)
- typography: Rename typography Sass function from pxToRem() to px-to-rem() (8f0a11e)
Features
- base: add non-statics foundation constructor type (e3ec22f)
- base: add observer mixin (4ceb422)
- chips: Expose "action" component (03d34bb)
- chips: Expose "chip" component (cbc57c6)
- chips: Expose "chipset" component (d6c5bcf)
- chips: Expose top-level resources (fefc668)
- chips: Remove touch target wrapper selector from chip set spacing (367d88b)
- chips: Start deprecation of chip (e683bdf)
- chips: Start deprecation of chip root directory (73a2271)
- chips: Start deprecation of chip set (148e8cf)
- chips: Start deprecation of chip trailing action (9eeb35c)
- chips: Truncate long chip labels by default (f5c6db8)
- dialog: Adding
resize
and orientationchange
event handlers into MDCDialogFoundation
. (1e06534) - dialog: Adds support for "surface-scrim" over full-screen dialogs. This prevents a "double scrim" from appearing when showing a secondary dialog over a full-screen dialog on larger screens. (cddb035)
- dom: add option to skip restoring focus on release focus (5c0ab74)
- dom: add tab key keyboard.ts (dc9c840)
- fab: Add theming API to Extended FABs (f19c86d)
- fab: Added
$focus-outline-width
param to extended-padding() FAB mixin (8ecd7c9) - fab: Added focus outline theme keys to FAB theme mixin (d6d8d04)
- fab: Added theme mixin to primary FAB variant. (f19bbc4)
- fab: border custom prop support & add CPs for padding (a6b3101)
- fix: Ensure that secondary controls do not ripple. (1f636b2)
- fix: Fix divider layout in right-to-left locales. (f524626)
- fix: Remove old MDC list class names, preparing to release evolution. (5f0fc44)
- fix: Remove the "evolution" prefix from list evolution's class names. (0cde52f)
- fix: Simplify divider styles to reflect new design guidance. (f77c508)
- linear-progress: add getBuffer (9c85d50)
- list: Add "deprecated" aliases for old list mixins / variables. (f9cac96)
- list: Add missing "deprecated" aliases for old list mixin. (302c7a9)
- list: Finalize list mixin/variable rename. (c97d7d8)
- list: Rename deprecated MDC list class names. (a678806)
- list: Rename deprecated MDC list class names. (941ca3b)
- list: Update deprecated list class names so evolution can become default. (606e767)
- list: Update styles to reference "deprecated" mixins/variables. (3201cae)
- list: Update styles to remove "evolution" prefix from mixins/variables. (f9c9e39)
- menu: add maxHeight setter (bf670da)
- menu-surface: add option to always horizontally center on viewport (23ea2d8)
- ripple: add active() mixin for styling active styles. (9f2e85f)
- select: allow programmatic change without firing event (79ce087), closes #6166
- slider: Add mixin to customize thumb color in the activated (hover, focus, pressed) state. (94f50b2)
- Add support for "mdc-deprecated-list-*" class names. (9e52f55)
- switch: add high-contrast mode focus shim mixin (c91e8d1)
- theme: add configuration support for custom-properties (1f318ff)
- theme: add create-varname() for custom properties (b522724)
- theme: add key store (07ff0c4)
- tooltip: Adding logic to position the caret relative to the tooltip. (76da787)
- tooltip: Adding touchstart/touchend event listeners to tooltip. This allows tooltips attached to non-focusable elements to be surfaced on mobile. (7cd26af)
- tooltip: Creating an
mdc-tooltip__surface-animation
class that holds all the style properties responsible for animating the tooltip in and out of the page. The existing mdc-tooltip__surface
class will hold all the style properties that impact the visual appearance of the tooltip. (56fc269) - tooltip: Expose method that allows users to register additional scroll handlers on elements in the DOM. This should be used in situations where the tooltip anchor is a child of a scrollable element, and will ensure that the tooltip remains attached to the anchor when this element is scrolled. (24609b8)
BREAKING CHANGES
- typography: Renamed typography Sass function from pxToRem() to px-to-rem()
PiperOrigin-RevId: 368489085
- fix: the old list implementation has been deprecated and now requires that class names use an "mdc-deprecated-list-" prefix. The new implementation (list evolution), no longer uses a prefix ("mdc-evolution-list-" is now just "mdc-list-*").
PiperOrigin-RevId: 364441086
- snackbar: Dom structure change, see README.md
PiperOrigin-RevId: 363926666
- tooltip: Added adapter method:
- registerAnchorEventHandler<K extends EventType>(
evtType: K, handler: SpecificEventListener<K>): void;
- deregisterAnchorEventHandler<K extends EventType>(
evtType: K, handler: SpecificEventListener<K>): void;
PiperOrigin-RevId: 358401984