Skip to content

Commit b22fc71

Browse files
committed
feat: Use modal dialog
1 parent 5ce60dd commit b22fc71

2 files changed

Lines changed: 17 additions & 39 deletions

File tree

assets/js/dialog.js

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import {
2-
openOverlay,
3-
closeOverlay,
4-
addOverlayClickListener,
5-
} from "./overlay.js";
6-
71
function getCloseButton(dialog) {
82
return dialog.querySelector(".o-dialog__header > .a-button");
93
}
@@ -12,31 +6,23 @@ function openDialog(dialogId) {
126
const dialog = document.getElementById(dialogId);
137
if (!dialog) return;
148

15-
dialog.show();
16-
openOverlay("dialog");
17-
18-
const closeButton = getCloseButton(dialog);
19-
if (closeButton) {
20-
closeButton.addEventListener("click", () => closeDialog(dialog));
21-
}
9+
dialog.showModal();
2210
}
2311

24-
function closeDialog(dialog) {
25-
const closeButton = getCloseButton(dialog);
26-
if (closeButton) {
27-
closeButton.replaceWith(closeButton.cloneNode(true));
28-
}
29-
dialog.close();
30-
closeOverlay();
31-
}
12+
function initDialogs() {
13+
document.querySelectorAll("dialog").forEach((dialog) => {
14+
dialog.addEventListener("click", (e) => {
15+
if (e.target === dialog) {
16+
dialog.close();
17+
}
18+
});
3219

33-
function closeAllDialogs() {
34-
document.querySelectorAll("dialog[open]").forEach((dialog) => {
35-
closeDialog(dialog);
20+
const closeButton = getCloseButton(dialog);
21+
if (closeButton) {
22+
closeButton.addEventListener("click", () => dialog.close());
23+
}
3624
});
37-
}
3825

39-
function initDialogs() {
4026
document.querySelectorAll("[data-dialog-trigger]").forEach((trigger) => {
4127
const handler = (e) => {
4228
if (e.type === "click" || (e.type === "keydown" && e.key === "Enter")) {
@@ -49,16 +35,6 @@ function initDialogs() {
4935
trigger.addEventListener("click", handler);
5036
trigger.addEventListener("keydown", handler);
5137
});
52-
53-
addOverlayClickListener(() => {
54-
closeAllDialogs();
55-
});
56-
57-
document.addEventListener("keydown", (e) => {
58-
if (e.key === "Escape") {
59-
closeAllDialogs();
60-
}
61-
});
6238
}
6339

6440
if (document.readyState === "loading") {

assets/sass/dialog.scss

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222

2323
&__wrapper {
2424
width: fit-content;
25-
position: fixed;
26-
top: 7rem;
2725
border: none;
2826
padding: 0;
2927
background-color: var(--bg-default);
3028
color: var(--color-body);
3129
border-radius: var(--border-radius-m);
32-
z-index: 12;
3330
border: var(--border);
3431
overflow: hidden;
32+
33+
&::backdrop {
34+
background-color: rgba(0, 0, 0, 0.6);
35+
backdrop-filter: blur(4px);
36+
}
3537
}
3638

3739
&__header {

0 commit comments

Comments
 (0)