-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosable.ts
More file actions
67 lines (59 loc) · 2.01 KB
/
Copy pathclosable.ts
File metadata and controls
67 lines (59 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { LitElement } from "lit";
import { property } from "lit/decorators.js";
import type { PropertyValues } from "lit";
import type WaDialog from "@awesome.me/webawesome/dist/components/dialog/dialog";
type Constructor<T> = new (...args: any[]) => T;
export declare class ClosableInterface {
open: boolean;
dialog: WaDialog;
canClose(): boolean;
}
// Mixin to add open property and open/close events to a LitElement wrapping a wa-dialog
export const Closable = <T extends Constructor<LitElement>>(superClass: T) => {
class ClosableElement extends superClass {
@property({ type: Boolean, reflect: true }) open = false;
get dialog(): WaDialog {
return this.firstElementChild as WaDialog;
}
canClose(): boolean {
return true;
}
protected updated(changedProperties: PropertyValues): void {
if (changedProperties.has("open")) {
if (this.dialog.open !== this.open) {
this.dialog.open = this.open;
}
}
}
protected firstUpdated(): void {
this.dialog.addEventListener("wa-hide", (originalEvent) => {
if (this.dialog !== originalEvent.target) {
return;
}
if (originalEvent.defaultPrevented) {
return;
}
// If open is already false, this was triggered programmatically - allow it
// Otherwise check canClose() for user-initiated close (Escape key, overlay click)
if (this.open && !this.canClose()) {
originalEvent.preventDefault();
originalEvent.stopPropagation();
return;
}
this.open = false;
this.dispatchEvent(new CustomEvent("close"));
});
this.dialog.addEventListener("wa-show", (originalEvent) => {
if (this.dialog !== originalEvent.target) {
return;
}
if (originalEvent.defaultPrevented) {
return;
}
this.open = true;
this.dispatchEvent(new CustomEvent("open"));
});
}
}
return ClosableElement as Constructor<ClosableInterface> & T;
};