Skip to content

Commit 82f29a0

Browse files
authored
Merge pull request #235 from zvonimirfras/master
fix(dialog): add singleton click listener for safari fix
2 parents f094fa7 + edad9c1 commit 82f29a0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/dialog/dialog.directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class DialogDirective implements OnInit, OnDestroy, OnChanges {
135135
*/
136136
ngOnInit() {
137137
// fix for safari hijacking clicks
138-
document.body.firstElementChild.addEventListener("click", () => null, true);
138+
this.dialogService.singletonClickListen();
139139

140140
// bind events for hovering or clicking the host
141141
if (this.trigger === "hover" || this.trigger === "mouseenter") {

src/dialog/dialog.service.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import { PlaceholderService } from "./../placeholder/placeholder.module";
1818
*/
1919
@Injectable()
2020
export class DialogService {
21+
/**
22+
* Used in `singletonClickListen`, don't count on its existence and values.
23+
*/
24+
private static listeningForBodyClicks = false;
25+
2126
/**
2227
* Reflects the open or closed state of the `Dialog`.
2328
* @memberof DialogService
@@ -157,4 +162,23 @@ export class DialogService {
157162
}
158163
}
159164
}
165+
166+
/**
167+
* Fix for safari hijacking clicks.
168+
*
169+
* Runs on `ngOnInit` of every dialog. Ensures we don't have multiple listeners
170+
* because having many of them could degrade performance in certain cases (and is
171+
* not necessary for our use case)
172+
*
173+
* This is an internally used function, can change at any point (even get removed)
174+
* and changes to it won't be considered a breaking change. Use at your own risk.
175+
*/
176+
singletonClickListen() {
177+
console.log("singleton");
178+
if (!DialogService.listeningForBodyClicks) {
179+
console.log("singleton click");
180+
document.body.firstElementChild.addEventListener("click", () => null, true);
181+
DialogService.listeningForBodyClicks = true;
182+
}
183+
}
160184
}

0 commit comments

Comments
 (0)