Skip to content

fix(overlay-container): overlay container throws an error if its wasn't set initially #5194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/lib/core/overlay/overlay-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export class OverlayContainer {
get themeClass(): string { return this._themeClass; }
set themeClass(value: string) {
if (this._containerElement) {
this._containerElement.classList.remove(this._themeClass);

if (this._safeClassDefiner(this._themeClass)) {
this._containerElement.classList.remove(this._themeClass);
}

if (value) {
this._containerElement.classList.add(value);
Expand Down Expand Up @@ -61,6 +64,20 @@ export class OverlayContainer {
document.body.appendChild(container);
this._containerElement = container;
}

/***
* @description classList.contains throw error if get checked for empty string in classList
* @param className
* @return {boolean}
* @private
*/
private _safeClassDefiner(className: string): boolean {
try {
return this._containerElement.classList.contains(className);
} catch (e) {
return false;
}
}
}

export function OVERLAY_CONTAINER_PROVIDER_FACTORY(parentContainer: OverlayContainer) {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/core/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,14 @@ describe('OverlayContainer theming', () => {
expect(overlayContainerElement.classList).not.toContain('initial-theme');
expect(overlayContainerElement.classList).toContain('new-theme');
});

it('should not throw error if previously-set theme initialy was not set', () => {
overlayContainer.themeClass = 'new-theme';
expect(overlayContainerElement.classList).toContain('new-theme');

expect( function(){ overlayContainer.themeClass = ''; }).not.toThrow();
expect(overlayContainerElement.classList).not.toContain('new-theme');
});
});

/** Simple component for testing ComponentPortal. */
Expand Down