Skip to content

Commit 4f025f2

Browse files
committed
extension: Implement modal class
1 parent c431111 commit 4f025f2

File tree

3 files changed

+85
-18
lines changed

3 files changed

+85
-18
lines changed

web/packages/extension/assets/options.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ <h2 class="option-title">Per-Site Options</h2>
125125
<h3>Site-Specific Settings</h3>
126126
<small style="opacity:0.8;">Enable the switch to override the global setting for the website.</small>
127127
</div>
128-
<button class="modal-close-btn">&times;</button>
128+
<button class="modal-close-btn" data-ruffle-modal-dismiss>&times;</button>
129129
</div>
130130

131131
<div class="modal-body">
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
export class Modal {
2+
public static readonly MODAL_SHOW = "ruffle.modal.show";
3+
public static readonly MODAL_HIDE = "ruffle.modal.hide";
4+
5+
private readonly modalElement: HTMLElement;
6+
private readonly closeButton: NodeListOf<HTMLElement>;
7+
8+
constructor(target: string | HTMLElement) {
9+
if (target instanceof HTMLElement) {
10+
this.modalElement = target;
11+
} else {
12+
const el = document.querySelector<HTMLElement>(target);
13+
14+
if (!el) {
15+
throw new Error(`Unknown modal element`);
16+
}
17+
18+
this.modalElement = el;
19+
}
20+
21+
this.closeButton = this.modalElement.querySelectorAll<HTMLElement>(
22+
"[data-ruffle-modal-dismiss]",
23+
);
24+
25+
for (const el of this.closeButton) {
26+
el.addEventListener("click", this.hide.bind(this));
27+
}
28+
}
29+
30+
public get element(): HTMLElement {
31+
return this.modalElement;
32+
}
33+
34+
public show(): void {
35+
document.body.classList.add("modal-open");
36+
37+
this.modalElement.style.display = "flex";
38+
39+
setTimeout(() => {
40+
this.modalElement.classList.add("show");
41+
42+
this.modalElement.dispatchEvent(
43+
new CustomEvent(Modal.MODAL_SHOW, {
44+
bubbles: true,
45+
detail: {
46+
modalElement: this.modalElement,
47+
},
48+
}),
49+
);
50+
}, 150);
51+
}
52+
53+
public hide(): void {
54+
document.body.classList.remove("modal-open");
55+
56+
this.modalElement.classList.remove("show");
57+
58+
setTimeout((): void => {
59+
this.modalElement.style.display = "";
60+
61+
this.modalElement.dispatchEvent(
62+
new CustomEvent(Modal.MODAL_HIDE, {
63+
bubbles: true,
64+
detail: {
65+
modalElement: this.modalElement,
66+
},
67+
}),
68+
);
69+
}, 150);
70+
}
71+
}

web/packages/extension/src/options.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as utils from "./utils";
22
import { bindOptions, resetOptions } from "./common";
33
import { buildInfo } from "ruffle-core";
4+
import { Modal } from "./modal";
45

56
type SettingForm = {
67
id: string;
@@ -646,28 +647,23 @@ window.addEventListener("DOMContentLoaded", async () => {
646647
});
647648
}
648649

649-
const modal = document.getElementById("site-settings-modal")!;
650-
const addNewBtn = document.getElementById("site-entry-new")!;
651-
const closeBtns = document.querySelectorAll(
652-
".modal-close-btn, #modal-cancel-btn",
653-
);
654-
655-
const openModal = () => {
656-
modal.style.display = "flex";
657-
document.body.classList.add("modal-open");
658-
};
650+
const modal = new Modal(document.getElementById("site-settings-modal")!);
659651

660-
const closeModal = () => {
661-
modal.style.display = "none";
662-
document.body.classList.remove("modal-open");
663-
};
652+
modal.element.addEventListener(Modal.MODAL_SHOW, () => {
653+
console.log("MODAL_SHOW");
654+
});
664655

665-
addNewBtn.addEventListener("click", openModal);
656+
modal.element.addEventListener(Modal.MODAL_HIDE, () => {
657+
console.log("MODAL_HIDE");
658+
});
666659

667-
closeBtns.forEach((btn) => btn.addEventListener("click", closeModal));
660+
// const addNewBtn = document.getElementById("site-entry-new")!;
661+
// const modalSaveBtn = document.getElementById("modal-save-btn")!;
668662

669663
document.querySelectorAll(".edit-site-btn").forEach((btn) => {
670-
btn.addEventListener("click", openModal);
664+
btn.addEventListener("click", () => {
665+
modal.show();
666+
});
671667
});
672668

673669
document.querySelectorAll(".settings-option").forEach((option) => {

0 commit comments

Comments
 (0)