@paroicms/tiny-modal 0.7.1 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/tiny-modal.css +14 -4
- package/dist/tiny-modal.d.ts +2 -0
- package/dist/tiny-modal.js +57 -2
- package/package.json +1 -1
package/dist/tiny-modal.css
CHANGED
|
@@ -2,18 +2,22 @@
|
|
|
2
2
|
.TinyModal {
|
|
3
3
|
background: transparent;
|
|
4
4
|
border: none;
|
|
5
|
+
display: flex;
|
|
6
|
+
flex-direction: column;
|
|
5
7
|
margin: auto;
|
|
8
|
+
max-height: 100dvh;
|
|
9
|
+
max-width: 100dvw;
|
|
6
10
|
padding: 0;
|
|
7
11
|
position: fixed;
|
|
8
|
-
overflow: visible;
|
|
9
|
-
max-width: 100vw;
|
|
10
|
-
max-height: 100vh;
|
|
11
12
|
}
|
|
12
13
|
.TinyModal::backdrop {
|
|
13
14
|
background-color: rgba(34, 34, 34, 0.7333333333);
|
|
14
15
|
}
|
|
15
16
|
.TinyModal-body {
|
|
16
|
-
|
|
17
|
+
flex: 1;
|
|
18
|
+
max-width: 100%;
|
|
19
|
+
min-height: 0;
|
|
20
|
+
overflow: auto;
|
|
17
21
|
}
|
|
18
22
|
.TinyModal-closeButton {
|
|
19
23
|
align-items: center;
|
|
@@ -40,6 +44,12 @@
|
|
|
40
44
|
right: -18px;
|
|
41
45
|
}
|
|
42
46
|
}
|
|
47
|
+
.TinyModal.fullHeight .TinyModal-closeButton {
|
|
48
|
+
top: 5px;
|
|
49
|
+
}
|
|
50
|
+
.TinyModal.fullWidth .TinyModal-closeButton {
|
|
51
|
+
right: 5px;
|
|
52
|
+
}
|
|
43
53
|
|
|
44
54
|
.TinyModalHidden {
|
|
45
55
|
display: none !important;
|
package/dist/tiny-modal.d.ts
CHANGED
|
@@ -8,9 +8,11 @@ export default function initializeTinyModal(options: {
|
|
|
8
8
|
dialogContent: HTMLElement;
|
|
9
9
|
openButton?: string | HTMLElement;
|
|
10
10
|
modalClass?: string;
|
|
11
|
+
loadCss?: boolean;
|
|
11
12
|
}): TinyModal;
|
|
12
13
|
export default function initializeTinyModal(options: {
|
|
13
14
|
dialogContent: string;
|
|
14
15
|
openButton?: string | HTMLElement;
|
|
15
16
|
modalClass?: string;
|
|
17
|
+
loadCss?: boolean;
|
|
16
18
|
}): TinyModal | undefined;
|
package/dist/tiny-modal.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
let cssLoaded = false;
|
|
2
|
+
export default function initializeTinyModal({ openButton, dialogContent, modalClass, loadCss, }) {
|
|
3
|
+
if (loadCss) {
|
|
4
|
+
loadCssFromModuleUrl();
|
|
5
|
+
}
|
|
2
6
|
const openBtnEl = openButton
|
|
3
7
|
? typeof openButton === "string"
|
|
4
8
|
? document.querySelector(openButton)
|
|
@@ -23,7 +27,6 @@ export default function initializeTinyModal({ openButton, dialogContent, modalCl
|
|
|
23
27
|
bodyEl.appendChild(contentEl instanceof HTMLTemplateElement ? contentEl.content.cloneNode(true) : contentEl);
|
|
24
28
|
dialogEl.appendChild(bodyEl);
|
|
25
29
|
document.body.appendChild(dialogEl);
|
|
26
|
-
// change the map to use the specific event union
|
|
27
30
|
const eventListeners = new Map();
|
|
28
31
|
function on(eventName, listener) {
|
|
29
32
|
if (!eventListeners.has(eventName)) {
|
|
@@ -38,6 +41,31 @@ export default function initializeTinyModal({ openButton, dialogContent, modalCl
|
|
|
38
41
|
});
|
|
39
42
|
}
|
|
40
43
|
}
|
|
44
|
+
function updateCloseButtonPosition() {
|
|
45
|
+
const dialogRect = dialogEl.getBoundingClientRect();
|
|
46
|
+
const viewportHeight = window.innerHeight;
|
|
47
|
+
const viewportWidth = window.innerWidth;
|
|
48
|
+
// Check if dialog is full height (with small tolerance for rounding)
|
|
49
|
+
if (Math.abs(dialogRect.height - viewportHeight) < 2) {
|
|
50
|
+
dialogEl.classList.add("fullHeight");
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
dialogEl.classList.remove("fullHeight");
|
|
54
|
+
}
|
|
55
|
+
// Check if dialog is full width (with small tolerance for rounding)
|
|
56
|
+
if (Math.abs(dialogRect.width - viewportWidth) < 2) {
|
|
57
|
+
dialogEl.classList.add("fullWidth");
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
dialogEl.classList.remove("fullWidth");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
64
|
+
if (dialogEl.open) {
|
|
65
|
+
updateCloseButtonPosition();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
resizeObserver.observe(dialogEl);
|
|
41
69
|
function open() {
|
|
42
70
|
dialogEl.showModal();
|
|
43
71
|
document.body.style.overflow = "hidden";
|
|
@@ -56,3 +84,30 @@ export default function initializeTinyModal({ openButton, dialogContent, modalCl
|
|
|
56
84
|
});
|
|
57
85
|
return { open, close, on };
|
|
58
86
|
}
|
|
87
|
+
function loadCssFromModuleUrl() {
|
|
88
|
+
if (cssLoaded)
|
|
89
|
+
return;
|
|
90
|
+
const moduleUrl = import.meta.url;
|
|
91
|
+
if (!moduleUrl)
|
|
92
|
+
throw new Error("import.meta.url not available");
|
|
93
|
+
// Derive CSS URL from the module URL
|
|
94
|
+
// e.g., https://esm.sh/@paroicms/tiny-modal@0.7.1 -> https://esm.sh/@paroicms/tiny-modal@0.7.1/dist/tiny-modal.css
|
|
95
|
+
// or https://esm.sh/@paroicms/tiny-modal@0.7.1/dist/tiny-modal.js -> https://esm.sh/@paroicms/tiny-modal@0.7.1/dist/tiny-modal.css
|
|
96
|
+
const cssUrl = moduleUrl.replace(/\/[^/]+\.m?js$/, () => {
|
|
97
|
+
if (moduleUrl.includes("/dist/")) {
|
|
98
|
+
return "/tiny-modal.css";
|
|
99
|
+
}
|
|
100
|
+
return "/dist/tiny-modal.css";
|
|
101
|
+
});
|
|
102
|
+
// Check if CSS is already loaded
|
|
103
|
+
if (document.querySelector(`link[href="${cssUrl}"]`)) {
|
|
104
|
+
cssLoaded = true;
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// Inject CSS link
|
|
108
|
+
const link = document.createElement("link");
|
|
109
|
+
link.rel = "stylesheet";
|
|
110
|
+
link.href = cssUrl;
|
|
111
|
+
document.head.appendChild(link);
|
|
112
|
+
cssLoaded = true;
|
|
113
|
+
}
|