Skip to content

Commit

Permalink
refactor(cdk/overlay): use classList.toggle
Browse files Browse the repository at this point in the history
With Angular dropping IE11 support in version 13, we can now use `classList.toggle` with the second `force` param. Related to #7374
  • Loading branch information
jelbourn authored and andrewseguin committed Sep 14, 2021
1 parent d897a0c commit f4a54d6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
12 changes: 4 additions & 8 deletions src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,11 @@ export class OverlayRef implements PortalOutlet, OverlayReference {

/** Toggles a single CSS class or an array of classes on an element. */
private _toggleClasses(element: HTMLElement, cssClasses: string | string[], isAdd: boolean) {
const classList = element.classList;
const classes = coerceArray(cssClasses || []).filter(c => !!c);

coerceArray(cssClasses).forEach(cssClass => {
// We can't do a spread here, because IE doesn't support setting multiple classes.
// Also trying to add an empty string to a DOMTokenList will throw.
if (cssClass) {
isAdd ? classList.add(cssClass) : classList.remove(cssClass);
}
});
if (classes.length) {
isAdd ? element.classList.add(...classes) : element.classList.remove(...classes);
}
}

/** Detaches the overlay content next time the zone stabilizes. */
Expand Down
9 changes: 9 additions & 0 deletions src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,17 @@ describe('Overlay', () => {
const overlayRef = overlay.create();
overlayRef.attach(componentPortal);

// Empty string
expect(() => overlayRef.addPanelClass('')).not.toThrow();
expect(() => overlayRef.removePanelClass('')).not.toThrow();

// Empty array
expect(() => overlayRef.addPanelClass([])).not.toThrow();
expect(() => overlayRef.removePanelClass([])).not.toThrow();

// Array containing only the empty string
expect(() => overlayRef.addPanelClass([''])).not.toThrow();
expect(() => overlayRef.removePanelClass([''])).not.toThrow();
});

describe('positioning', () => {
Expand Down
6 changes: 1 addition & 5 deletions src/dev-app/connected-overlay/connected-overlay-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ export class ConnectedOverlayDemo {
const box = document.querySelector<HTMLElement>('.cdk-overlay-connected-position-bounding-box');

if (box) {
if (showBoundingBox) {
box.classList.add('demo-bounding-box-visible');
} else {
box.classList.remove('demo-bounding-box-visible');
}
box.classList.toggle('demo-bounding-box-visible', showBoundingBox);
}
}
}

0 comments on commit f4a54d6

Please sign in to comment.