Skip to content

Commit acf1b86

Browse files
authored
refactor(material-experimental/mdc-progress-bar): remove wrapper element for progress bar (#22441)
* refactor(material-experimental/mdc-progress-bar): remove wrapper element for progress bar * fixup! refactor(material-experimental/mdc-progress-bar): remove wrapper element for progress bar * fix(material-experimental/mdc-progress-bar): add more specific selectors to disable animations in noop animation mode
1 parent c12f168 commit acf1b86

File tree

4 files changed

+38
-35
lines changed

4 files changed

+38
-35
lines changed

src/material-experimental/mdc-progress-bar/progress-bar.html

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
All children need to be hidden for screen readers in order to support ChromeVox.
33
More context in the issue: https://github.com/angular/components/issues/22165.
44
-->
5-
<div class="mdc-linear-progress" aria-hidden="true">
6-
<div class="mdc-linear-progress__buffer">
7-
<div class="mdc-linear-progress__buffer-bar"></div>
8-
<div class="mdc-linear-progress__buffer-dots"></div>
9-
</div>
10-
<div class="mdc-linear-progress__bar mdc-linear-progress__primary-bar">
11-
<span class="mdc-linear-progress__bar-inner"></span>
12-
</div>
13-
<div class="mdc-linear-progress__bar mdc-linear-progress__secondary-bar">
14-
<span class="mdc-linear-progress__bar-inner"></span>
15-
</div>
5+
<div class="mdc-linear-progress__buffer" aria-hidden="true">
6+
<div class="mdc-linear-progress__buffer-bar"></div>
7+
<div class="mdc-linear-progress__buffer-dots"></div>
8+
</div>
9+
<div class="mdc-linear-progress__bar mdc-linear-progress__primary-bar" aria-hidden="true">
10+
<span class="mdc-linear-progress__bar-inner"></span>
11+
</div>
12+
<div class="mdc-linear-progress__bar mdc-linear-progress__secondary-bar" aria-hidden="true">
13+
<span class="mdc-linear-progress__bar-inner"></span>
1614
</div>
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
@use '@material/linear-progress' as mdc-linear-progress;
22
@use '../mdc-helpers/mdc-helpers';
33

4-
@include mdc-linear-progress.core-styles($query:
5-
mdc-helpers.$mat-base-styles-without-animation-query);
4+
@include mdc-linear-progress.core-styles($query: mdc-helpers.$mat-base-styles-query);
65

76
.mat-mdc-progress-bar {
87
// Explicitly set to `block` since the browser defaults custom elements to `inline`.
98
display: block;
109

1110
&._mat-animation-noopable {
11+
.mdc-linear-progress__buffer-dots,
12+
.mdc-linear-progress__primary-bar,
13+
.mdc-linear-progress__secondary-bar,
14+
.mdc-linear-progress__bar-inner.mdc-linear-progress__bar-inner {
15+
// Disable the loading animations.
16+
animation: none;
17+
}
18+
1219
.mdc-linear-progress__primary-bar {
1320
// There's a `transitionend` event that depends on this element. Add a very short
1421
// transition when animations are disabled so that the event can still fire.
1522
transition: transform 1ms;
1623
}
1724
}
18-
19-
&:not(._mat-animation-noopable) {
20-
@include mdc-linear-progress.core-styles($query: animation);
21-
}
2225
}

src/material-experimental/mdc-progress-bar/progress-bar.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ describe('MDC-based MatProgressBar', () => {
2323
const fixture = createComponent(BasicProgressBar);
2424
const host = fixture.nativeElement as Element;
2525
const element = host.children[0];
26-
expect(element.children.length).toBe(1);
26+
const children = element.children;
27+
expect(children.length).toBe(3);
2728

28-
const div = element.querySelector('div')!;
29-
expect(div).toBeTruthy();
30-
expect(div.getAttribute('aria-hidden')).toBe('true');
29+
const ariaHidden = Array.from(children).map(child => child.getAttribute('aria-hidden'));
30+
expect(ariaHidden).toEqual(['true', 'true', 'true']);
3131
});
3232

3333
describe('with animation', () => {

src/material-experimental/mdc-progress-bar/progress-bar.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export type ProgressBarMode = 'determinate' | 'indeterminate' | 'buffer' | 'quer
5555
'tabindex': '-1',
5656
'[attr.aria-valuenow]': '(mode === "indeterminate" || mode === "query") ? null : value',
5757
'[attr.mode]': 'mode',
58-
'class': 'mat-mdc-progress-bar',
58+
'class': 'mat-mdc-progress-bar mdc-linear-progress',
5959
'[class._mat-animation-noopable]': '_isNoopAnimation',
6060
},
6161
inputs: ['color'],
@@ -83,22 +83,26 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
8383

8484
/** Adapter used by MDC to interact with the DOM. */
8585
private _adapter: MDCLinearProgressAdapter = {
86-
addClass: (className: string) => this._rootElement.classList.add(className),
87-
forceLayout: () => this._rootElement.offsetWidth,
88-
removeAttribute: (name: string) => this._rootElement.removeAttribute(name),
89-
setAttribute: (name: string, value: string) => this._rootElement.setAttribute(name, value),
90-
hasClass: (className: string) => this._rootElement.classList.contains(className),
91-
removeClass: (className: string) => this._rootElement.classList.remove(className),
86+
addClass: (className: string) => this._elementRef.nativeElement.classList.add(className),
87+
forceLayout: () => this._elementRef.nativeElement.offsetWidth,
88+
removeAttribute: (name: string) => this._elementRef.nativeElement.removeAttribute(name),
89+
setAttribute: (name: string, value: string) => {
90+
if (name !== 'aria-valuenow') {
91+
this._elementRef.nativeElement.setAttribute(name, value);
92+
}
93+
},
94+
hasClass: (className: string) => this._elementRef.nativeElement.classList.contains(className),
95+
removeClass: (className: string) => this._elementRef.nativeElement.classList.remove(className),
9296
setPrimaryBarStyle: (styleProperty: string, value: string) => {
9397
(this._primaryBar.style as any)[styleProperty] = value;
9498
},
9599
setBufferBarStyle: (styleProperty: string, value: string) => {
96100
(this._bufferBar.style as any)[styleProperty] = value;
97101
},
98102
setStyle: (styleProperty: string, value: string) => {
99-
(this._rootElement.style as any)[styleProperty] = value;
103+
(this._elementRef.nativeElement.style as any)[styleProperty] = value;
100104
},
101-
getWidth: () => this._rootElement.offsetWidth,
105+
getWidth: () => this._elementRef.nativeElement.offsetWidth,
102106
attachResizeObserver: (callback) => {
103107
const resizeObserverConstructor = (typeof window !== 'undefined') &&
104108
(window as unknown as WithMDCResizeObserver).ResizeObserver;
@@ -111,7 +115,7 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
111115
// on the constructed `observer`. This should not happen, but adding this check for this
112116
// edge case.
113117
if (typeof observer.observe === 'function') {
114-
observer.observe(this._rootElement);
118+
observer.observe(this._elementRef.nativeElement);
115119
return observer;
116120
}
117121

@@ -144,7 +148,6 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
144148
}
145149
private _bufferValue = 0;
146150

147-
private _rootElement: HTMLElement;
148151
private _primaryBar: HTMLElement;
149152
private _bufferBar: HTMLElement;
150153

@@ -181,7 +184,6 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
181184
ngAfterViewInit() {
182185
const element = this._elementRef.nativeElement;
183186

184-
this._rootElement = element.querySelector('.mdc-linear-progress') as HTMLElement;
185187
this._primaryBar = element.querySelector('.mdc-linear-progress__primary-bar') as HTMLElement;
186188
this._bufferBar = element.querySelector('.mdc-linear-progress__buffer-bar') as HTMLElement;
187189

@@ -221,9 +223,9 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements AfterVie
221223

222224
const reverse = direction === 'rtl' ? mode !== 'query' : mode === 'query';
223225
const progressDirection = reverse ? 'rtl' : 'ltr';
224-
const currentDirection = this._rootElement.getAttribute('dir');
226+
const currentDirection = this._elementRef.nativeElement.getAttribute('dir');
225227
if (currentDirection !== progressDirection) {
226-
this._rootElement.setAttribute('dir', progressDirection);
228+
this._elementRef.nativeElement.setAttribute('dir', progressDirection);
227229
foundation.restartAnimation();
228230
}
229231

0 commit comments

Comments
 (0)