-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup-handler.ts
179 lines (141 loc) · 4.42 KB
/
popup-handler.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import A11yDialog from "a11y-dialog";
import "iframe-resizer/js/iframeResizer";
import "iframe-resizer/js/iframeResizer.contentWindow";
const CONTAINER_ID = "__authsignal-popup-container";
const CONTENT_ID = "__authsignal-popup-content";
const OVERLAY_ID = "__authsignal-popup-overlay";
const STYLE_ID = "__authsignal-popup-style";
const IFRAME_ID = "__authsignal-popup-iframe";
const DEFAULT_WIDTH = "385px";
type PopupShowInput = {
url: string;
};
type EventType = "show" | "hide" | "destroy" | "create";
type EventHandler = (node: Element, event?: Event) => void;
type PopupHandlerOptions = {
width?: string;
};
class PopupHandler {
private popup: A11yDialog | null = null;
private isHeightAutoResized = true;
constructor({width}: PopupHandlerOptions) {
if (document.querySelector(`#${CONTAINER_ID}`)) {
throw new Error("Multiple instances of Authsignal popup is not supported.");
}
this.create({width});
}
create({width = DEFAULT_WIDTH}: PopupHandlerOptions) {
const isWidthValidCSSValue = CSS.supports("width", width);
let popupWidth = width;
if (!isWidthValidCSSValue) {
console.warn("Invalid CSS value for `popupOptions.width`. Using default value instead.");
popupWidth = DEFAULT_WIDTH;
}
// Create dialog container
const container = document.createElement("div");
container.setAttribute("id", CONTAINER_ID);
container.setAttribute("aria-hidden", "true");
// Create dialog overlay
const overlay = document.createElement("div");
overlay.setAttribute("id", OVERLAY_ID);
overlay.setAttribute("data-a11y-dialog-hide", "true");
// Create dialog content
const content = document.createElement("div");
content.setAttribute("id", CONTENT_ID);
document.body.appendChild(container);
// Create CSS for dialog
const style = document.createElement("style");
style.setAttribute("id", STYLE_ID);
style.textContent = `
#${CONTAINER_ID},
#${OVERLAY_ID} {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
#${CONTAINER_ID} {
z-index: 2147483647;
display: flex;
}
#${CONTAINER_ID}[aria-hidden='true'] {
display: none;
}
#${OVERLAY_ID} {
background-color: rgba(0, 0, 0, 0.18);
}
#${CONTENT_ID} {
margin: auto;
z-index: 2147483647;
position: relative;
background-color: transparent;
border-radius: 8px;
width: ${popupWidth};
}
#${CONTENT_ID} iframe {
width: 1px;
min-width: 100%;
border-radius: inherit;
max-height: 65vh;
}
`;
// Attach the created elements
document.head.insertAdjacentElement("beforeend", style);
container.appendChild(overlay);
container.appendChild(content);
this.popup = new A11yDialog(container);
// Make sure to remove any trace of the dialog on hide
this.popup.on("hide", () => {
this.destroy();
});
}
destroy() {
const dialogEl = document.querySelector(`#${CONTAINER_ID}`);
const styleEl = document.querySelector(`#${STYLE_ID}`);
if (dialogEl && styleEl) {
document.body.removeChild(dialogEl);
document.head.removeChild(styleEl);
}
}
show({url}: PopupShowInput) {
if (!this.popup) {
throw new Error("Popup is not initialized");
}
const iframe = document.createElement("iframe");
iframe.setAttribute("id", IFRAME_ID);
iframe.setAttribute("name", "authsignal");
iframe.setAttribute("title", "Authsignal multi-factor authentication");
iframe.setAttribute("src", url);
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("allow", "publickey-credentials-get *; clipboard-write");
const dialogContent = document.querySelector(`#${CONTENT_ID}`);
if (dialogContent) {
dialogContent.appendChild(iframe);
}
// @ts-expect-error can't get typescript import to behave nicely, this works though
iFrameResize(
{
checkOrigin: false,
scrolling: true,
onInit: () => {
this.popup?.show();
},
},
iframe
);
}
close() {
if (!this.popup) {
throw new Error("Popup is not initialized");
}
this.popup.hide();
}
on(event: EventType, handler: EventHandler) {
if (!this.popup) {
throw new Error("Popup is not initialized");
}
this.popup.on(event, handler);
}
}
export {PopupHandler};