@inizioevoke/astro-core 3.0.0 → 3.0.1
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/package.json
CHANGED
|
@@ -1,21 +1,32 @@
|
|
|
1
1
|
---
|
|
2
2
|
import type { HTMLAttributes } from 'astro/types';
|
|
3
|
+
import type { EvoModalElementAttributes } from './Modal.ts';
|
|
3
4
|
import './Modal.css';
|
|
4
5
|
|
|
5
6
|
export interface Props extends HTMLAttributes<'div'> {
|
|
6
7
|
id: string;
|
|
7
8
|
closeButton?: 'default' | 'minimal' | 'none' | 'false' | false;
|
|
8
9
|
closeButtonAtts?: Record<string, string>;
|
|
10
|
+
overlayId?: string;
|
|
11
|
+
overlayCssClass?: string;
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
const {
|
|
12
15
|
id,
|
|
13
16
|
closeButton = 'default',
|
|
14
17
|
closeButtonAtts,
|
|
18
|
+
overlayId,
|
|
19
|
+
overlayCssClass,
|
|
15
20
|
...rest
|
|
16
21
|
} = Astro.props;
|
|
17
22
|
|
|
18
|
-
const attrs = {...rest};
|
|
23
|
+
const attrs: EvoModalElementAttributes = {...rest};
|
|
24
|
+
if (overlayId) {
|
|
25
|
+
attrs['overlay-id'] = overlayId;
|
|
26
|
+
}
|
|
27
|
+
if (overlayCssClass) {
|
|
28
|
+
attrs['overlay-append-class'] = overlayCssClass;
|
|
29
|
+
}
|
|
19
30
|
const cssClass = attrs['class'] ?? '';
|
|
20
31
|
|
|
21
32
|
['class'].forEach((a) => {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { HTMLAttributes } from "astro/types";
|
|
1
2
|
|
|
2
3
|
const TAG_NAME = 'evo-modal';
|
|
3
4
|
const CSS_VISIBLE = 'evo-modal-visible';
|
|
@@ -21,6 +22,10 @@ declare global {
|
|
|
21
22
|
}
|
|
22
23
|
type ModalEventType = 'evomodal-visible' | 'evomodal-hidden';
|
|
23
24
|
|
|
25
|
+
export interface EvoModalElementAttributes extends HTMLAttributes<'div'> {
|
|
26
|
+
'overlay-id'?: string;
|
|
27
|
+
'overlay-append-class'?: string;
|
|
28
|
+
}
|
|
24
29
|
export interface IEvoModalElement {
|
|
25
30
|
showModal(opts?: { animation?: ModalAnimation }): Promise<void>;
|
|
26
31
|
hideModal(opts?: { animation?: ModalAnimation }): Promise<void>;
|
|
@@ -49,7 +54,11 @@ export class EvoModalElement extends HTMLElement implements IEvoModalElement {
|
|
|
49
54
|
});
|
|
50
55
|
this.#animation = animation;
|
|
51
56
|
this.classList.add(CSS_VISIBLE, `${CSS_PREFIX_SHOW}-${animation}`);
|
|
52
|
-
showOverlay(
|
|
57
|
+
showOverlay({
|
|
58
|
+
animation: this.#animation,
|
|
59
|
+
overlayId: this.getAttribute('overlay-id'),
|
|
60
|
+
overlayAppendCssClass: this.getAttribute('overlay-append-class')
|
|
61
|
+
});
|
|
53
62
|
|
|
54
63
|
setTimeout(() => {
|
|
55
64
|
this.dispatchEvent(createModalEvent('evomodal-visible', this));
|
|
@@ -62,10 +71,13 @@ export class EvoModalElement extends HTMLElement implements IEvoModalElement {
|
|
|
62
71
|
}
|
|
63
72
|
|
|
64
73
|
hideModal({ animation }: { animation?: ModalAnimation } = {}): Promise<void> {
|
|
65
|
-
return this.#hideModal({
|
|
74
|
+
return this.#hideModal({
|
|
75
|
+
animation,
|
|
76
|
+
overlayId: this.getAttribute('overlay-id')
|
|
77
|
+
});
|
|
66
78
|
}
|
|
67
79
|
|
|
68
|
-
#hideModal({ animation, overlay = true, replacedBy }: { animation?: ModalAnimation, overlay?: boolean, replacedBy?: EvoModalElement } = {}): Promise<void> {
|
|
80
|
+
#hideModal({ overlayId, animation, overlay = true, replacedBy }: { overlayId?: string | null, animation?: ModalAnimation, overlay?: boolean, replacedBy?: EvoModalElement } = {}): Promise<void> {
|
|
69
81
|
return new Promise<void>((resolve) => {
|
|
70
82
|
if (this.classList.contains(CSS_VISIBLE)) {
|
|
71
83
|
if (!animation) {
|
|
@@ -77,7 +89,7 @@ export class EvoModalElement extends HTMLElement implements IEvoModalElement {
|
|
|
77
89
|
this.classList.add(`${CSS_PREFIX_HIDE}-${animation}`);
|
|
78
90
|
this.classList.remove(`${CSS_PREFIX_SHOW}-${animation}`);
|
|
79
91
|
if (overlay) {
|
|
80
|
-
hideOverlay({ modal: this, animation: this.#animation, duration });
|
|
92
|
+
hideOverlay({ overlayId, modal: this, animation: this.#animation, duration });
|
|
81
93
|
}
|
|
82
94
|
|
|
83
95
|
setTimeout(() => {
|
|
@@ -140,17 +152,21 @@ export async function hideModals(animation?: ModalAnimation) {
|
|
|
140
152
|
await Promise.all(modals.map((m) => { return m.hideModal({ animation })}));
|
|
141
153
|
}
|
|
142
154
|
|
|
143
|
-
export function showOverlay(animation
|
|
144
|
-
const overlay = getOverlay();
|
|
155
|
+
export function showOverlay({ overlayId, overlayAppendCssClass, animation = 'fade' }: { overlayId?: string | null, overlayAppendCssClass?: string | null, animation?: ModalAnimation}) {
|
|
156
|
+
const overlay = getOverlay(overlayId);
|
|
145
157
|
if (overlay && !overlay.classList.contains(CSS_VISIBLE)) {
|
|
146
158
|
overlay.classList.add(CSS_VISIBLE);
|
|
159
|
+
if (overlayAppendCssClass) {
|
|
160
|
+
overlay.classList.add(overlayAppendCssClass);
|
|
161
|
+
}
|
|
147
162
|
requestAnimationFrame(() => {
|
|
148
163
|
overlay.classList.add(`${CSS_PREFIX_SHOW}-${animation}`);
|
|
149
164
|
});
|
|
150
165
|
}
|
|
151
166
|
}
|
|
152
|
-
|
|
153
|
-
|
|
167
|
+
|
|
168
|
+
export function hideOverlay({ overlayId, modal, animation = 'fade', duration }: { overlayId?: string | null, modal?: EvoModalElement, animation?: ModalAnimation, duration?: number } = {}) {
|
|
169
|
+
const overlay = getOverlay(overlayId);
|
|
154
170
|
if (overlay && overlay.classList.contains(CSS_VISIBLE)) {
|
|
155
171
|
const cssShow = [...overlay.classList]
|
|
156
172
|
.filter((c) => {
|
|
@@ -166,7 +182,13 @@ export function hideOverlay({ modal, animation = 'fade', duration }: { modal?: E
|
|
|
166
182
|
overlay.classList.add(cssHide[0]);
|
|
167
183
|
overlay.classList.remove(cssShow[0]);
|
|
168
184
|
setTimeout(() => {
|
|
169
|
-
overlay.
|
|
185
|
+
const cssClasses = overlay.getAttribute('data-class')?.split(' ') ?? ['evo-modal-overlay'];
|
|
186
|
+
const currentClasses = [...overlay.classList];
|
|
187
|
+
for (const c of currentClasses) {
|
|
188
|
+
if (!cssClasses.includes(c)) {
|
|
189
|
+
overlay.classList.remove(c);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
170
192
|
}, duration ?? getAnimationDuration(modal));
|
|
171
193
|
}
|
|
172
194
|
|
|
@@ -190,8 +212,16 @@ export function bindTriggers(container: Document | HTMLElement = document) {
|
|
|
190
212
|
});
|
|
191
213
|
}
|
|
192
214
|
|
|
193
|
-
function getOverlay() {
|
|
194
|
-
|
|
215
|
+
function getOverlay(overlayId?: string | null) {
|
|
216
|
+
let overlay: HTMLElement | null = null;
|
|
217
|
+
if (overlayId) {
|
|
218
|
+
overlay = document.querySelector<HTMLElement>(`#${overlayId}`);
|
|
219
|
+
}
|
|
220
|
+
if (!overlay) {
|
|
221
|
+
overlay = document.querySelector<HTMLElement>(`.${CSS_OVERLAY}`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return overlay;
|
|
195
225
|
}
|
|
196
226
|
|
|
197
227
|
function getAnimationDuration(element: HTMLElement = document.body) {
|
|
@@ -218,7 +248,9 @@ document.addEventListener('DOMContentLoaded', (e) => {
|
|
|
218
248
|
overlay.classList.add(CSS_OVERLAY);
|
|
219
249
|
document.body.append(overlay);
|
|
220
250
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
251
|
+
document.querySelectorAll<HTMLElement>(`.${CSS_OVERLAY}`).forEach((overlay) => {
|
|
252
|
+
overlay.addEventListener('click', () => {
|
|
253
|
+
hideModals();
|
|
254
|
+
}, { passive: true });
|
|
255
|
+
});
|
|
224
256
|
}, { passive: true });
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
---
|
|
2
2
|
import type { HTMLAttributes } from "astro/types";
|
|
3
|
+
import type { string } from "astro:schema";
|
|
3
4
|
|
|
4
5
|
export interface Props extends HTMLAttributes<'div'> {
|
|
6
|
+
id?: string;
|
|
5
7
|
class?: string;
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
const {
|
|
11
|
+
id,
|
|
9
12
|
class: cssClass,
|
|
10
13
|
...rest
|
|
11
14
|
} = Astro.props;
|
|
12
15
|
|
|
16
|
+
const cssClassList: string[] = ['evo-modal-overlay', cssClass ?? ''];
|
|
17
|
+
|
|
13
18
|
---
|
|
14
|
-
<div class:list={
|
|
19
|
+
<div id={id} class:list={cssClassList} data-class={cssClassList.filter(c => c.trim() !== '').join(' ')} {...rest}><slot /></div>
|