Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 298d3eb

Browse files
hvolschenkMatt Goo
authored and
Matt Goo
committed
feature(linear-progress): add component (#425)
1 parent c2033f5 commit 298d3eb

File tree

10 files changed

+4177
-3362
lines changed

10 files changed

+4177
-3362
lines changed

package-lock.json

+3,562-3,362
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"icon-button",
3838
"layout-grid",
3939
"line-ripple",
40+
"linear-progress",
4041
"list",
4142
"menu-surface",
4243
"ripple",
@@ -67,6 +68,7 @@
6768
"@material/icon-button": "^0.40.1",
6869
"@material/layout-grid": "^0.40.1",
6970
"@material/line-ripple": "^0.40.1",
71+
"@material/linear-progress": "^0.41.0",
7072
"@material/list": "^0.40.1",
7173
"@material/menu-surface": "^0.40.1",
7274
"@material/notched-outline": "^0.40.1",

packages/linear-progress/.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
index.js

packages/linear-progress/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# React Linear Progress
2+
3+
A React version of an [MDC Linear Progress](https://github.com/material-components/material-components-web/tree/master/packages/mdc-linear-progress) component.
4+
5+
## Installation
6+
7+
```
8+
npm install @material/react-linear-progress
9+
```
10+
11+
## Usage
12+
13+
### Styles
14+
15+
with Sass:
16+
```js
17+
import '@material/react-linear-progress/index.scss';
18+
```
19+
20+
with CSS:
21+
```js
22+
import '@material/react-linear-progress/dist/linear-progress.css';
23+
```
24+
25+
### Javascript Instantiation
26+
```js
27+
import LinearProgress from '@material/react-linear-progress';
28+
import React from 'react';
29+
30+
class MyApp extends React.Component {
31+
render() {
32+
return (
33+
<LinearProgress
34+
buffer={0.9}
35+
progress={0.8}
36+
/>
37+
);
38+
}
39+
}
40+
```
41+
42+
## Props
43+
44+
Prop Name | Type | Description
45+
--- | --- | ---
46+
buffer | Number | Decimal value between 0 and 1, sets the buffer bar width
47+
bufferingDots | Boolean | Default `false`. Whether to show the buffer dots in the un-progressed section
48+
className | String | Optional. Additional class names to add to the root component
49+
closed | Boolean | Default `false`. When changed to `true`, closes the component
50+
indeterminate | Boolean | Default `false`. When set to `true`, renders the indeterminate variant
51+
progress | Number | Decimal value between 0 and 1, sets the progress bar width
52+
reversed | Boolean | Default `false`. When set to `true`, renders the reversed variant
53+
tag | String | Default `'div'`. The tag type to render
54+
55+
## Sass Mixins
56+
57+
Sass mixins may be available to customize various aspects of the Components. Please refer to the
58+
MDC Web repository for more information on what mixins are available, and how to use them.
59+
60+
[Advanced Sass Mixins](https://github.com/material-components/material-components-web/tree/master/packages/mdc-linear-progress#sass-mixins)

packages/linear-progress/index.js

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import {MDCLinearProgressFoundation} from '@material/linear-progress/dist/mdc.linearProgress';
2+
import classnames from 'classnames';
3+
import PropTypes from 'prop-types';
4+
import React from 'react';
5+
6+
class LinearProgress extends React.Component {
7+
isMounted_ = false;
8+
9+
constructor(props) {
10+
super(props);
11+
this.bufferElement_ = React.createRef();
12+
this.foundation_ = new MDCLinearProgressFoundation(this.adapter);
13+
this.primaryBarElement_ = React.createRef();
14+
this.state = {
15+
classList: new Set(),
16+
};
17+
}
18+
19+
componentDidMount() {
20+
const {buffer, closed, indeterminate, progress, reversed} = this.props;
21+
this.isMounted_ = true;
22+
this.foundation_.init();
23+
this.foundation_.setBuffer(buffer);
24+
this.foundation_.setDeterminate(!indeterminate);
25+
this.foundation_.setProgress(progress);
26+
this.foundation_.setReverse(reversed);
27+
if (closed) {
28+
this.foundation_.close();
29+
}
30+
}
31+
32+
componentDidUpdate(prevProps) {
33+
const {
34+
buffer: prevBuffer,
35+
closed: prevClosed,
36+
indeterminate: prevIndeterminate,
37+
progress: prevProgress,
38+
reversed: prevReversed,
39+
} = prevProps;
40+
const {buffer, closed, indeterminate, progress, reversed} = this.props;
41+
if (buffer !== prevBuffer) {
42+
this.foundation_.setBuffer(buffer);
43+
}
44+
if (closed && !prevClosed) {
45+
this.foundation_.close();
46+
}
47+
if (!closed && prevClosed) {
48+
this.foundation_.open();
49+
}
50+
if (indeterminate !== prevIndeterminate) {
51+
this.foundation_.setDeterminate(!indeterminate);
52+
}
53+
if (progress !== prevProgress) {
54+
this.foundation_.setProgress(progress);
55+
}
56+
if (reversed !== prevReversed) {
57+
this.foundation_.setReverse(reversed);
58+
}
59+
}
60+
61+
componentWillUnmount() {
62+
this.isMounted_ = false;
63+
this.foundation_.destroy();
64+
}
65+
66+
get adapter() {
67+
return {
68+
addClass: (className) => {
69+
if (this.isMounted_) {
70+
const {classList} = this.state;
71+
classList.add(className);
72+
this.setState({classList});
73+
}
74+
},
75+
getBuffer: () => {
76+
return this.bufferElement_.current;
77+
},
78+
getPrimaryBar: () => {
79+
return this.primaryBarElement_.current;
80+
},
81+
hasClass: (className) => {
82+
return this.state.classList.has(className);
83+
},
84+
removeClass: (className) => {
85+
if (this.isMounted_) {
86+
const {classList} = this.state;
87+
classList.delete(className);
88+
this.setState({classList});
89+
}
90+
},
91+
setStyle: (element, propertyName, value) => {
92+
if (this.isMounted_) {
93+
element.style.setProperty(propertyName, value);
94+
}
95+
},
96+
};
97+
}
98+
99+
get classes() {
100+
const {className, indeterminate, reversed} = this.props;
101+
const {classList} = this.state;
102+
return classnames('mdc-linear-progress', Array.from(classList), className, {
103+
'mdc-linear-progress--indeterminate': indeterminate,
104+
'mdc-linear-progress--reversed': reversed,
105+
});
106+
}
107+
108+
render() {
109+
const {
110+
// eslint-disable-next-line no-unused-vars
111+
buffer,
112+
bufferingDots,
113+
// eslint-disable-next-line no-unused-vars
114+
className,
115+
// eslint-disable-next-line no-unused-vars
116+
closed,
117+
// eslint-disable-next-line no-unused-vars
118+
indeterminate,
119+
// eslint-disable-next-line no-unused-vars
120+
progress,
121+
// eslint-disable-next-line no-unused-vars
122+
reversed,
123+
tag: Tag,
124+
...otherProps
125+
} = this.props;
126+
return (
127+
<Tag className={this.classes} role="progressbar" {...otherProps}>
128+
{bufferingDots && <div className="mdc-linear-progress__buffering-dots"></div>}
129+
<div className="mdc-linear-progress__buffer" ref={this.bufferElement_}></div>
130+
<div
131+
className="mdc-linear-progress__bar mdc-linear-progress__primary-bar"
132+
ref={this.primaryBarElement_}
133+
>
134+
<span className="mdc-linear-progress__bar-inner"></span>
135+
</div>
136+
<div className="mdc-linear-progress__bar mdc-linear-progress__secondary-bar">
137+
<span className="mdc-linear-progress__bar-inner"></span>
138+
</div>
139+
</Tag>
140+
);
141+
}
142+
}
143+
144+
LinearProgress.propTypes = {
145+
buffer: PropTypes.number,
146+
bufferingDots: PropTypes.bool,
147+
className: PropTypes.string,
148+
closed: PropTypes.bool,
149+
indeterminate: PropTypes.bool,
150+
progress: PropTypes.number,
151+
reversed: PropTypes.bool,
152+
tag: PropTypes.string,
153+
};
154+
155+
LinearProgress.defaultProps = {
156+
buffer: 0,
157+
bufferingDots: true,
158+
className: '',
159+
closed: false,
160+
indeterminate: false,
161+
progress: 0,
162+
reversed: false,
163+
tag: 'div',
164+
};
165+
166+
export default LinearProgress;

packages/linear-progress/index.scss

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// The MIT License
2+
//
3+
// Copyright (c) 2018 Google, Inc.
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in
13+
// all copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
// THE SOFTWARE.
22+
23+
@import "@material/linear-progress/mdc-linear-progress";

packages/linear-progress/package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "@material/react-linear-progress",
3+
"version": "0.0.0",
4+
"description": "Material Components React Linear Progress",
5+
"license": "MIT",
6+
"main": "dist/index.js",
7+
"keywords": [
8+
"mdc web react",
9+
"material components react",
10+
"material design",
11+
"material linear progress",
12+
"materiallinearprogress"
13+
],
14+
"repository": {
15+
"type": "git",
16+
"url": "https://github.com/material-components/material-components-web-react.git"
17+
},
18+
"dependencies": {
19+
"@material/linear-progress": "^0.41.0",
20+
"classnames": "^2.2.5",
21+
"prop-types": "^15.6.1",
22+
"react": "^16.4.2"
23+
},
24+
"publishConfig": {
25+
"access": "public"
26+
}
27+
}

0 commit comments

Comments
 (0)