Skip to content

Commit f5dada0

Browse files
jelbournandrewseguin
authored andcommitted
refactor(material/bottom-sheet): use classList.toggle
With Angular dropping IE11 support in version 13, we can now use `classList.toggle` with the second `force` param. Related to #7374
1 parent 7f216d9 commit f5dada0

File tree

3 files changed

+26
-33
lines changed

3 files changed

+26
-33
lines changed

src/material-experimental/mdc-snack-bar/snack-bar-container.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ export class MatSnackBarContainer extends BasePortalOutlet
204204
}
205205

206206
private _setClass(cssClass: string, active: boolean) {
207-
const classList = this._elementRef.nativeElement.classList;
208-
active ? classList.add(cssClass) : classList.remove(cssClass);
207+
this._elementRef.nativeElement.classList.toggle(cssClass, active);
209208
}
210209

211210
/** Applies the user-configured CSS classes to the snack bar. */

src/material/bottom-sheet/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ng_module(
2323
"//src:dev_mode_types",
2424
"//src/cdk/a11y",
2525
"//src/cdk/bidi",
26+
"//src/cdk/coercion",
2627
"//src/cdk/keycodes",
2728
"//src/cdk/layout",
2829
"//src/cdk/overlay",

src/material/bottom-sheet/bottom-sheet-container.ts

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,37 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {AnimationEvent} from '@angular/animations';
10+
import {FocusTrap, FocusTrapFactory, InteractivityChecker} from '@angular/cdk/a11y';
11+
import {coerceArray} from '@angular/cdk/coercion';
12+
import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';
13+
import {_getFocusedElementPierceShadowDom} from '@angular/cdk/platform';
14+
import {
15+
BasePortalOutlet,
16+
CdkPortalOutlet,
17+
ComponentPortal,
18+
DomPortal,
19+
TemplatePortal,
20+
} from '@angular/cdk/portal';
21+
import {DOCUMENT} from '@angular/common';
922
import {
23+
ChangeDetectionStrategy,
24+
ChangeDetectorRef,
1025
Component,
1126
ComponentRef,
12-
EmbeddedViewRef,
13-
ViewChild,
14-
OnDestroy,
1527
ElementRef,
16-
ChangeDetectionStrategy,
17-
ViewEncapsulation,
18-
ChangeDetectorRef,
28+
EmbeddedViewRef,
1929
EventEmitter,
2030
Inject,
21-
Optional,
2231
NgZone,
32+
OnDestroy,
33+
Optional,
34+
ViewChild,
35+
ViewEncapsulation,
2336
} from '@angular/core';
24-
import {AnimationEvent} from '@angular/animations';
25-
import {
26-
BasePortalOutlet,
27-
ComponentPortal,
28-
TemplatePortal,
29-
CdkPortalOutlet,
30-
DomPortal,
31-
} from '@angular/cdk/portal';
32-
import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';
33-
import {MatBottomSheetConfig} from './bottom-sheet-config';
34-
import {matBottomSheetAnimations} from './bottom-sheet-animations';
3537
import {Subscription} from 'rxjs';
36-
import {DOCUMENT} from '@angular/common';
37-
import {FocusTrap, FocusTrapFactory, InteractivityChecker} from '@angular/cdk/a11y';
38-
import {_getFocusedElementPierceShadowDom} from '@angular/cdk/platform';
38+
import {matBottomSheetAnimations} from './bottom-sheet-animations';
39+
import {MatBottomSheetConfig} from './bottom-sheet-config';
3940

4041
// TODO(crisbeto): consolidate some logic between this, MatDialog and MatSnackBar
4142

@@ -178,8 +179,7 @@ export class MatBottomSheetContainer extends BasePortalOutlet implements OnDestr
178179
}
179180

180181
private _toggleClass(cssClass: string, add: boolean) {
181-
const classList = this._elementRef.nativeElement.classList;
182-
add ? classList.add(cssClass) : classList.remove(cssClass);
182+
this._elementRef.nativeElement.classList.toggle(cssClass, add);
183183
}
184184

185185
private _validatePortalAttached() {
@@ -190,14 +190,7 @@ export class MatBottomSheetContainer extends BasePortalOutlet implements OnDestr
190190

191191
private _setPanelClass() {
192192
const element: HTMLElement = this._elementRef.nativeElement;
193-
const panelClass = this.bottomSheetConfig.panelClass;
194-
195-
if (Array.isArray(panelClass)) {
196-
// Note that we can't use a spread here, because IE doesn't support multiple arguments.
197-
panelClass.forEach(cssClass => element.classList.add(cssClass));
198-
} else if (panelClass) {
199-
element.classList.add(panelClass);
200-
}
193+
element.classList.add(...coerceArray(this.bottomSheetConfig.panelClass || []));
201194
}
202195

203196
/**

0 commit comments

Comments
 (0)