Skip to content

Commit c90c373

Browse files
committed
fix(backdrop): modal backdrop not removed on zoneless - close #240 - thanks @anyplin
1 parent 9268edd commit c90c373

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

projects/coreui-angular/src/lib/backdrop/backdrop.service.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { inject, Injectable, RendererFactory2, DOCUMENT } from '@angular/core';
1+
import { DOCUMENT, inject, Injectable } from '@angular/core';
22

33
import { Subject } from 'rxjs';
44

@@ -9,10 +9,7 @@ export class BackdropService {
99
readonly #backdropClick = new Subject<boolean>();
1010
readonly backdropClick$ = this.#backdropClick.asObservable();
1111

12-
#document = inject(DOCUMENT);
13-
#rendererFactory = inject(RendererFactory2);
14-
#renderer = this.#rendererFactory.createRenderer(null, null);
15-
#unListen!: () => void;
12+
readonly #document = inject(DOCUMENT);
1613

1714
activeBackdrop: any;
1815

@@ -25,33 +22,40 @@ export class BackdropService {
2522

2623
scrollbarWidth = this.#scrollbarWidth;
2724

28-
setBackdrop(type: string = 'modal'): any {
29-
const backdropElement = this.#renderer.createElement('div');
30-
this.#renderer.addClass(backdropElement, `${type}-backdrop`);
31-
this.#renderer.addClass(backdropElement, 'fade');
32-
this.#renderer.appendChild(this.#document.body, backdropElement);
33-
this.#unListen = this.#renderer.listen(backdropElement, 'click', (e): void => {
25+
setBackdrop(type: string = 'modal'): HTMLDivElement {
26+
const backdropElement = this.#document.createElement('div');
27+
backdropElement.classList.add(`${type}-backdrop`);
28+
backdropElement.classList.add('fade');
29+
this.#document.body.appendChild(backdropElement);
30+
31+
const clickHandler = () => {
3432
this.onClickHandler();
35-
});
33+
};
34+
(backdropElement as any).__backdropClickHandler = clickHandler;
35+
backdropElement.addEventListener('click', clickHandler);
36+
3637
this.scrollbarWidth = this.#scrollbarWidth;
3738
setTimeout(() => {
38-
this.#renderer.addClass(backdropElement, 'show');
39+
backdropElement.classList.add('show');
3940
// this.hideScrollbar();
4041
});
4142
this.activeBackdrop = backdropElement;
4243
return backdropElement;
4344
}
4445

45-
clearBackdrop(backdropElement: any): any {
46+
clearBackdrop(backdropElement: HTMLElement): any {
4647
if (backdropElement) {
47-
this.#unListen();
48-
this.#renderer.removeClass(backdropElement, 'show');
48+
const storedHandler = (backdropElement as any).__backdropClickHandler;
49+
if (storedHandler) {
50+
backdropElement.removeEventListener('click', storedHandler);
51+
delete (backdropElement as any).__backdropClickHandler;
52+
}
53+
backdropElement.classList.remove('show');
4954
setTimeout(() => {
5055
if (this.activeBackdrop === backdropElement) {
5156
this.resetScrollbar();
5257
}
53-
this.#renderer.removeChild(this.#document.body, backdropElement);
54-
backdropElement = undefined;
58+
this.#document.body.removeChild(backdropElement);
5559
}, 300);
5660
}
5761
return undefined;
@@ -61,19 +65,23 @@ export class BackdropService {
6165
return [this.#document.documentElement.dir, this.#document.body.dir].includes('rtl');
6266
}
6367

68+
get #padding() {
69+
return `padding-${this.#isRTL ? 'left' : 'right'}`;
70+
}
71+
6472
#scrollBarVisible = true;
6573

6674
hideScrollbar(): void {
6775
if (this.#scrollBarVisible) {
68-
this.#renderer.setStyle(this.#document.body, 'overflow', 'hidden');
69-
this.#renderer.setStyle(this.#document.body, `padding-${this.#isRTL ? 'left' : 'right'}`, this.scrollbarWidth);
76+
this.#document.body.style.setProperty('overflow', 'hidden');
77+
this.#document.body.style.setProperty(this.#padding, this.scrollbarWidth);
7078
this.#scrollBarVisible = false;
7179
}
7280
}
7381

7482
resetScrollbar(): void {
75-
this.#renderer.removeStyle(this.#document.body, 'overflow');
76-
this.#renderer.removeStyle(this.#document.body, `padding-${this.#isRTL ? 'left' : 'right'}`);
83+
this.#document.body.style.removeProperty('overflow');
84+
this.#document.body.style.removeProperty(this.#padding);
7785
this.#scrollBarVisible = true;
7886
}
7987

0 commit comments

Comments
 (0)