Skip to content

Commit 3c23fc5

Browse files
committed
feat(overlay): replace OverlayContainer themeClass w/ addClass/removeClass methods
BREAKING CHANGE: Now that the Overlay is part of the cdk rather than Angular Material directly, the `themeClass` property has been replaced by more general `addClass` and `removeClass` methods.
1 parent f2cae6e commit 3c23fc5

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

src/cdk/overlay/overlay-container.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,29 @@ export class OverlayContainer implements OnDestroy {
1919

2020
private _themeClass: string;
2121

22+
ngOnDestroy() {
23+
if (this._containerElement && this._containerElement.parentNode) {
24+
this._containerElement.parentNode.removeChild(this._containerElement);
25+
}
26+
}
27+
2228
/**
23-
* Base theme to be applied to all overlay-based components.
29+
* Adds a CSS class to the overlay container.
30+
* @param cssClass The CSS class to add.
2431
*/
25-
get themeClass(): string { return this._themeClass; }
26-
set themeClass(value: string) {
27-
if (this._containerElement) {
28-
if (this._themeClass) {
29-
this._containerElement.classList.remove(this._themeClass);
30-
}
31-
32-
if (value) {
33-
this._containerElement.classList.add(value);
34-
}
32+
addClass(cssClass: string) {
33+
if (cssClass && cssClass.trim()) {
34+
this.getContainerElement().classList.add(cssClass);
3535
}
36-
37-
this._themeClass = value;
3836
}
3937

40-
ngOnDestroy() {
41-
if (this._containerElement && this._containerElement.parentNode) {
42-
this._containerElement.parentNode.removeChild(this._containerElement);
38+
/**
39+
* Removed a CSS class from the overlay container.
40+
* @param cssClass The CSS class to remove.
41+
*/
42+
removeClass(cssClass: string) {
43+
if (cssClass && cssClass.trim()) {
44+
this.getContainerElement().classList.remove(cssClass);
4345
}
4446
}
4547

src/cdk/overlay/overlay.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
### Overlay
22

3-
A service used to manage overlays.
3+
A service used to manage overlays.

src/lib/core/overlay/overlay-container.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ describe('OverlayContainer', () => {
4343
expect(document.querySelector('.cdk-overlay-container'))
4444
.toBeNull('Expected the overlay container *not* to be in the DOM after destruction');
4545
});
46+
47+
it('should add and remove css classes from the container element', () => {
48+
const containerElement = document.querySelector('.cdk-overlay-container')!;
49+
50+
overlayContainer.addClass('commander-shepard');
51+
52+
expect(containerElement.classList.contains('commander-shepard'))
53+
.toBe(true, 'Expected the overlay container to have class "commander-shepard"');
54+
55+
overlayContainer.removeClass('commander-shepard');
56+
57+
expect(containerElement.classList.contains('commander-shepard'))
58+
.toBe(false, 'Expected the overlay container not to have class "commander-shepard"');
59+
});
4660
});
4761

4862
/** Test-bed component that contains a TempatePortal and an ElementRef. */

0 commit comments

Comments
 (0)