Skip to content

Commit 06998a3

Browse files
allan-chencopybara-github
authored andcommitted
refactor(circular-progress): reorganize render function calling structure
PiperOrigin-RevId: 333121473
1 parent a0793c9 commit 06998a3

File tree

2 files changed

+50
-68
lines changed

2 files changed

+50
-68
lines changed

packages/circular-progress-four-color/mwc-circular-progress-four-color-base.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ export class CircularProgressFourColorBase extends CircularProgressBase {
2323
protected renderIndeterminateContainer() {
2424
return html`
2525
<div class="mdc-circular-progress__indeterminate-container">
26-
${
27-
this.renderIndeterminateSpinnerLayer('mdc-circular-progress__color-1')}
28-
${this.renderIndeterminateSpinnerLayer('mdc-circular-progress__color-2')}
29-
${this.renderIndeterminateSpinnerLayer('mdc-circular-progress__color-3')}
30-
${this.renderIndeterminateSpinnerLayer('mdc-circular-progress__color-4')}
26+
<div class="mdc-circular-progress__spinner-layer mdc-circular-progress__color-1">
27+
${this.renderIndeterminateSpinnerLayer()}
28+
</div>
29+
<div class="mdc-circular-progress__spinner-layer mdc-circular-progress__color-2">
30+
${this.renderIndeterminateSpinnerLayer()}
31+
</div>
32+
<div class="mdc-circular-progress__spinner-layer mdc-circular-progress__color-3">
33+
${this.renderIndeterminateSpinnerLayer()}
34+
</div>
35+
<div class="mdc-circular-progress__spinner-layer mdc-circular-progress__color-4">
36+
${this.renderIndeterminateSpinnerLayer()}
37+
</div>
3138
</div>`;
3239
}
3340
}

packages/circular-progress/mwc-circular-progress-base.ts

Lines changed: 38 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
17-
import {html, internalProperty, LitElement, property} from 'lit-element';
17+
import {html, LitElement, property} from 'lit-element';
1818
import {classMap} from 'lit-html/directives/class-map';
1919
import {ifDefined} from 'lit-html/directives/if-defined';
2020
import {styleMap} from 'lit-html/directives/style-map';
@@ -31,14 +31,6 @@ export class CircularProgressBase extends LitElement {
3131

3232
@property({type: String}) ariaLabel = '';
3333

34-
@internalProperty() containerSideLength = 0;
35-
36-
@internalProperty() circleRadius = 0;
37-
38-
@internalProperty() determinateStrokeDashOffset = 0;
39-
40-
@internalProperty() strokeWidth = 0;
41-
4234
open() {
4335
this.closed = false;
4436
}
@@ -57,9 +49,10 @@ export class CircularProgressBase extends LitElement {
5749
'mdc-circular-progress--indeterminate': this.indeterminate,
5850
};
5951

52+
const containerSideLength = 48 + this.density * 4;
6053
const styles = {
61-
'width': `${this.containerSideLength}px`,
62-
'height': `${this.containerSideLength}px`,
54+
'width': `${containerSideLength}px`,
55+
'height': `${containerSideLength}px`,
6356
};
6457

6558
return html`
@@ -81,18 +74,24 @@ export class CircularProgressBase extends LitElement {
8174
* @soyCompatible
8275
*/
8376
private renderDeterminateContainer() {
84-
const center = this.containerSideLength / 2;
77+
const sideLength = 48 + this.density * 4;
78+
const center = sideLength / 2;
79+
const circleRadius = this.density >= -3 ? 18 + this.density * 11 / 6 :
80+
12.5 + (this.density + 3) * 5 / 4;
81+
const circumference = 2 * 3.1415926 * circleRadius;
82+
const determinateStrokeDashOffset = (1 - this.progress) * circumference;
83+
const strokeWidth = this.density >= -3 ? 4 + this.density * (1 / 3) :
84+
3 + (this.density + 3) * (1 / 6);
8585

8686
return html`
8787
<div class="mdc-circular-progress__determinate-container">
8888
<svg class="mdc-circular-progress__determinate-circle-graphic"
89-
viewBox="0 0 ${this.containerSideLength} ${
90-
this.containerSideLength}">
89+
viewBox="0 0 ${sideLength} ${sideLength}">
9190
<circle class="mdc-circular-progress__determinate-circle"
92-
cx="${center}" cy="${center}" r="${this.circleRadius}"
93-
stroke-dasharray="${2 * 3.1415926 * this.circleRadius}"
94-
stroke-dashoffset="${this.determinateStrokeDashOffset}"
95-
stroke-width="${this.strokeWidth}"></circle>
91+
cx="${center}" cy="${center}" r="${circleRadius}"
92+
stroke-dasharray="${2 * 3.1415926 * circleRadius}"
93+
stroke-dashoffset="${determinateStrokeDashOffset}"
94+
stroke-width="${strokeWidth}"></circle>
9695
</svg>
9796
</div>`;
9897
}
@@ -103,59 +102,56 @@ export class CircularProgressBase extends LitElement {
103102
protected renderIndeterminateContainer() {
104103
return html`
105104
<div class="mdc-circular-progress__indeterminate-container">
106-
${this.renderIndeterminateSpinnerLayer()}
105+
<div class="mdc-circular-progress__spinner-layer">
106+
${this.renderIndeterminateSpinnerLayer()}
107+
</div>
107108
</div>`;
108109
}
109110

110111
/**
111112
* @soyCompatible
112113
*/
113-
protected renderIndeterminateSpinnerLayer(classes = '') {
114-
const center = this.containerSideLength / 2;
115-
const circumference = 2 * 3.1415926 * this.circleRadius;
114+
protected renderIndeterminateSpinnerLayer() {
115+
const sideLength = 48 + this.density * 4;
116+
const center = sideLength / 2;
117+
const circleRadius = this.density >= -3 ? 18 + this.density * 11 / 6 :
118+
12.5 + (this.density + 3) * 5 / 4;
119+
const circumference = 2 * 3.1415926 * circleRadius;
116120
const halfCircumference = 0.5 * circumference;
121+
const strokeWidth = this.density >= -3 ? 4 + this.density * (1 / 3) :
122+
3 + (this.density + 3) * (1 / 6);
117123

118124
return html`
119-
<div class="mdc-circular-progress__spinner-layer ${classes}">
120125
<div class="mdc-circular-progress__circle-clipper mdc-circular-progress__circle-left">
121126
<svg class="mdc-circular-progress__indeterminate-circle-graphic"
122-
viewBox="0 0 ${this.containerSideLength} ${
123-
this.containerSideLength}">
124-
<circle cx="${center}" cy="${center}" r="${this.circleRadius}"
127+
viewBox="0 0 ${sideLength} ${sideLength}">
128+
<circle cx="${center}" cy="${center}" r="${circleRadius}"
125129
stroke-dasharray="${circumference}"
126130
stroke-dashoffset="${halfCircumference}"
127-
stroke-width="${this.strokeWidth}"></circle>
131+
stroke-width="${strokeWidth}"></circle>
128132
</svg>
129133
</div><div class="mdc-circular-progress__gap-patch">
130134
<svg class="mdc-circular-progress__indeterminate-circle-graphic"
131-
viewBox="0 0 ${this.containerSideLength} ${
132-
this.containerSideLength}">
133-
<circle cx="${center}" cy="${center}" r="${this.circleRadius}"
135+
viewBox="0 0 ${sideLength} ${sideLength}">
136+
<circle cx="${center}" cy="${center}" r="${circleRadius}"
134137
stroke-dasharray="${circumference}"
135138
stroke-dashoffset="${halfCircumference}"
136-
stroke-width="${this.strokeWidth * 0.8}"></circle>
139+
stroke-width="${strokeWidth * 0.8}"></circle>
137140
</svg>
138141
</div><div class="mdc-circular-progress__circle-clipper mdc-circular-progress__circle-right">
139142
<svg class="mdc-circular-progress__indeterminate-circle-graphic"
140-
viewBox="0 0 ${this.containerSideLength} ${
141-
this.containerSideLength}">
142-
<circle cx="${center}" cy="${center}" r="${this.circleRadius}"
143+
viewBox="0 0 ${sideLength} ${sideLength}">
144+
<circle cx="${center}" cy="${center}" r="${circleRadius}"
143145
stroke-dasharray="${circumference}"
144146
stroke-dashoffset="${halfCircumference}"
145-
stroke-width="${this.strokeWidth}"></circle>
147+
stroke-width="${strokeWidth}"></circle>
146148
</svg>
147-
</div>
148-
</div>`;
149+
</div>`;
149150
}
150151

151152
update(changedProperties: Map<string, string>) {
152153
super.update(changedProperties);
153154

154-
this.containerSideLength = this.getContainerSideLength();
155-
this.circleRadius = this.getCircleRadius();
156-
this.determinateStrokeDashOffset = this.getDeterminateStrokeDashOffset();
157-
this.strokeWidth = this.getStrokeWidth();
158-
159155
// Bound progress value in interval [0, 1].
160156
if (changedProperties.has('progress')) {
161157
if (this.progress > 1) {
@@ -167,25 +163,4 @@ export class CircularProgressBase extends LitElement {
167163
}
168164
}
169165
}
170-
171-
private getContainerSideLength() {
172-
return 48 + this.density * 4;
173-
}
174-
175-
private getDeterminateStrokeDashOffset(): number {
176-
const circleRadius = this.getCircleRadius();
177-
const circumference = 2 * 3.1415926 * circleRadius;
178-
179-
return (1 - this.progress) * circumference;
180-
}
181-
182-
private getCircleRadius() {
183-
return this.density >= -3 ? 18 + this.density * 11 / 6 :
184-
12.5 + (this.density + 3) * 5 / 4;
185-
}
186-
187-
private getStrokeWidth() {
188-
return this.density >= -3 ? 4 + this.density * (1 / 3) :
189-
3 + (this.density + 3) * (1 / 6);
190-
}
191166
}

0 commit comments

Comments
 (0)