@inizioevoke/astro-core 1.3.2 → 1.3.4

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@inizioevoke/astro-core",
3
- "version": "1.3.2",
3
+ "version": "1.3.4",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "",
@@ -17,7 +17,7 @@
17
17
  "index.ts"
18
18
  ],
19
19
  "peerDependencies": {
20
- "astro": ">=6.0.0",
20
+ "astro": ">=5.0.0",
21
21
  "typescript": ">=5.0.0"
22
22
  },
23
23
  "devDependencies": {
@@ -5,8 +5,11 @@ interface ModalEventCallbackItem {
5
5
  once?: boolean;
6
6
  callback: ModalEventCallback;
7
7
  }
8
+ interface ModalEventCallbackArgs {
9
+ once?: boolean
10
+ }
8
11
 
9
- type ModalEventType = 'show' | 'hide';
12
+ type ModalEventType = 'show' | 'hide' | 'overlay';
10
13
  type ModalEventTypeCallbacks = Record<ModalEventType, Set<ModalEventCallbackItem>>;
11
14
  const callbacks = new Map<HTMLElement, ModalEventTypeCallbacks>();
12
15
 
@@ -50,7 +53,7 @@ function __showOverlay(animation: ModalAnimation) {
50
53
  overlay.classList.add('evo-modal-visible', `evo-modal-show-${animation}`);
51
54
  }
52
55
  }
53
- export function onShow(selector: string | HTMLElement, callback: ModalEventCallback, { once }: { once?: boolean } = {}) {
56
+ export function onShow(selector: string | HTMLElement, callback: ModalEventCallback, { once }: ModalEventCallbackArgs = {}) {
54
57
  onEvent('show', selector, { once, callback });
55
58
  }
56
59
 
@@ -105,7 +108,7 @@ function __hide(modal: HTMLElement, animation: ModalAnimation) {
105
108
  }, animation !== 'none' ? animationDuration : 0);
106
109
  });
107
110
  }
108
- export function onHide(selector: string | HTMLElement, callback: ModalEventCallback, { once }: { once?: boolean } = {}) {
111
+ export function onHide(selector: string | HTMLElement, callback: ModalEventCallback, { once }: ModalEventCallbackArgs = {}) {
109
112
  onEvent('hide', selector, { once, callback });
110
113
  }
111
114
 
@@ -120,6 +123,10 @@ export function appendOverlay(selector: string | HTMLElement) {
120
123
  }
121
124
  }
122
125
 
126
+ export function onOverlayClick(callback: ModalEventCallback, { once }: ModalEventCallbackArgs = {}) {
127
+ onEvent('overlay', overlay, { once, callback });
128
+ }
129
+
123
130
  function onEvent(type: ModalEventType, selector: string | HTMLElement, callbackItem: ModalEventCallbackItem) {
124
131
  let modal = typeof selector == 'string' ? document.querySelector<HTMLElement>(/^[\.#]/.test(selector) ? selector : `#${selector}`) : selector;
125
132
  if (modal) {
@@ -132,15 +139,15 @@ function onEvent(type: ModalEventType, selector: string | HTMLElement, callbackI
132
139
  }
133
140
  }
134
141
 
135
- function triggerEventCallbacks(modal: HTMLElement, eventType: 'show' | 'hide') {
136
- if (modal) {
137
- const modalCallbacks = callbacks.get(modal);
138
- if (modalCallbacks && modalCallbacks[eventType]) {
139
- const items = [...modalCallbacks[eventType]];
142
+ function triggerEventCallbacks(element: HTMLElement, eventType: ModalEventType) {
143
+ if (element) {
144
+ const eventCallbacks = callbacks.get(element);
145
+ if (eventCallbacks && eventCallbacks[eventType]) {
146
+ const items = [...eventCallbacks[eventType]];
140
147
  const keep: ModalEventCallbackItem[] = [];
141
148
  for (const item of items) {
142
149
  try {
143
- item.callback(modal);
150
+ item.callback(element);
144
151
  } catch (err) {
145
152
  console.log(err);
146
153
  } finally {
@@ -149,7 +156,7 @@ function triggerEventCallbacks(modal: HTMLElement, eventType: 'show' | 'hide') {
149
156
  }
150
157
  }
151
158
  }
152
- callbacks.set(modal, {...modalCallbacks, [eventType]: new Set(keep)});
159
+ callbacks.set(element, {...eventCallbacks, [eventType]: new Set(keep)});
153
160
  }
154
161
  }
155
162
  }
@@ -163,6 +170,7 @@ const overlay: HTMLElement = document.querySelector<HTMLElement>('.evo-modal-ove
163
170
  })();
164
171
  overlay.addEventListener('click', () => {
165
172
  hide();
173
+ triggerEventCallbacks(overlay, 'overlay');
166
174
  });
167
175
 
168
176
  const animationDuration = (() => {
@@ -1 +1,14 @@
1
- <div class="evo-modal-overlay"></div>
1
+ ---
2
+ import type { HTMLAttributes } from "astro/types";
3
+
4
+ interface Props extends HTMLAttributes<'div'> {
5
+ class?: string;
6
+ }
7
+
8
+ const {
9
+ class: cssClass,
10
+ ...rest
11
+ } = Astro.props;
12
+
13
+ ---
14
+ <div class:list={['evo-modal-overlay', cssClass]} {...rest}><slot /></div>
@@ -4,7 +4,8 @@ export interface IScrollContainer {
4
4
  resize(args: { height?: number }): void;
5
5
  refresh(): void;
6
6
  }
7
- export class ScrollContainer implements IScrollContainer {
7
+
8
+ class ScrollContainer implements IScrollContainer {
8
9
  #container: HTMLElement;
9
10
  #content: HTMLElement;
10
11
  #track: HTMLElement;
@@ -17,7 +18,7 @@ export class ScrollContainer implements IScrollContainer {
17
18
  #mousemoveHandler: any = undefined;
18
19
 
19
20
  constructor(container: HTMLElement) {
20
- const _this = this;
21
+ // const _this = this;
21
22
  this.#container = container;
22
23
  this.#content = container.querySelector('.evo-scroll-content') as HTMLElement;
23
24
  this.#track = container.querySelector('.evo-scroll-track') as HTMLElement;
@@ -25,19 +26,9 @@ export class ScrollContainer implements IScrollContainer {
25
26
  this.#isDragging = false;
26
27
  this.#startY = 0;
27
28
  this.#startScrollTop = 0;
28
- // this.#enabled = enabled ?? true;
29
-
30
- this.#thumb.addEventListener('mousedown', (e) => {
31
- _this.#thumbMouseDown(e);
32
- }, { passive: true });
33
-
34
- // this.#thumb.addEventListener('mouseup', (e) => {
35
- // _this.#thumbMouseUp(e);
36
- // }, { passive: true });
37
29
 
38
- this.#content.addEventListener('scroll', () => {
39
- _this.#scrollContent();
40
- }, { passive: true });
30
+ this.#thumb.addEventListener('mousedown', this.#thumbMouseDown.bind(this), { passive: true });
31
+ this.#content.addEventListener('scroll', this.#scrollContent.bind(this), { passive: true });
41
32
  }
42
33
 
43
34
  scroll({ left, top, behavior = 'auto' }: { left?: number | string, top?: number | string, behavior?: 'auto' | 'instant' | 'smooth' } = {}) {
@@ -113,6 +104,7 @@ export class ScrollContainer implements IScrollContainer {
113
104
  }
114
105
 
115
106
  #thumbMouseMove(e: MouseEvent) {
107
+ // console.log(performance.now());
116
108
  if (this.#isDragging) {
117
109
  const deltaY = e.clientY - this.#startY;
118
110
  const scrollRatio =
@@ -141,30 +133,30 @@ export class ScrollContainer implements IScrollContainer {
141
133
  }
142
134
  }
143
135
 
144
- export function resize(scrollContainer: string | HTMLElement | ScrollContainer, { height }: { height?: number }) {
136
+ export function resize(selector: string | HTMLElement | ScrollContainer, { height }: { height?: number }) {
145
137
  if (typeof height == 'number') {
146
- const sc = getScrollContainer(scrollContainer);
138
+ const sc = getScrollContainer(selector);
147
139
  if (sc) {
148
140
  sc.resize({ height })
149
141
  }
150
142
  }
151
143
  }
152
144
 
153
- export function refresh(scrollContainer?: string | HTMLElement | ScrollContainer) {
154
- if (scrollContainer) {
155
- const sc = getScrollContainer(scrollContainer);
145
+ export function refresh(selector?: string | HTMLElement | ScrollContainer) {
146
+ if (selector) {
147
+ const sc = getScrollContainer(selector);
156
148
  if (sc) {
157
149
  sc.refresh()
158
150
  }
159
151
  } else {
160
- [...scrollContainers.values()].forEach((scrollContainer) => {
161
- scrollContainer.refresh();
152
+ [...scrollContainers.values()].forEach((selector) => {
153
+ selector.refresh();
162
154
  });
163
155
  }
164
156
  }
165
157
 
166
- export function getScroll(scrollContainer: string | HTMLElement | ScrollContainer): { left: number, top: number } | undefined {
167
- const sc = getScrollContainer(scrollContainer);
158
+ export function getScroll(selector: string | HTMLElement | ScrollContainer): { left: number, top: number } | undefined {
159
+ const sc = getScrollContainer(selector);
168
160
  if (sc) {
169
161
  return sc.getScroll();
170
162
  } else {
@@ -172,8 +164,8 @@ export function getScroll(scrollContainer: string | HTMLElement | ScrollContaine
172
164
  }
173
165
  }
174
166
 
175
- export function scroll(scrollContainer: string | HTMLElement | ScrollContainer, { left, top, behavior = 'auto' }: { left?: number, top?: number, behavior?: 'auto' | 'instant' | 'smooth' } = {}) {
176
- const sc = getScrollContainer(scrollContainer);
167
+ export function scroll(selector: string | HTMLElement | ScrollContainer, { left, top, behavior = 'auto' }: { left?: number, top?: number, behavior?: 'auto' | 'instant' | 'smooth' } = {}) {
168
+ const sc = getScrollContainer(selector);
177
169
  if (sc) {
178
170
  const current = sc.getScroll();
179
171
  sc.scroll({ left: left ?? current.left, top: top ?? current.top, behavior });
@@ -188,6 +180,21 @@ export function getScrollContainer(selector: string | HTMLElement | ScrollContai
188
180
  return key ? scrollContainers.get(key) : undefined;
189
181
  }
190
182
 
183
+ export function createScrollContainer(selector: string | HTMLElement): IScrollContainer | undefined {
184
+ const container = typeof selector == 'string' ? document.querySelector<HTMLElement>(selector) : selector;
185
+ if (container) {
186
+ if (scrollContainers.has(container)) {
187
+ return scrollContainers.get(container);
188
+ } else {
189
+ const scrollContainer = new ScrollContainer(container);
190
+ initNewScrollContainer(container, scrollContainer);
191
+ return scrollContainer;
192
+ }
193
+ } else {
194
+ return undefined;
195
+ }
196
+ }
197
+
191
198
  function getPaddingY(content: HTMLElement): number {
192
199
  const style = getComputedStyle(content);
193
200
  return parseFloat(style.paddingTop) + parseFloat(style.paddingBottom);
@@ -221,16 +228,20 @@ const mutationObserver = new MutationObserver((mutations) => {
221
228
  });
222
229
  });
223
230
 
224
- document.querySelectorAll<HTMLElement>('.evo-scroll-container').forEach((container) => {
225
- const scrollContainer = new ScrollContainer(container);
231
+ function initNewScrollContainer(container: HTMLElement, scrollContainer: ScrollContainer) {
226
232
  scrollContainers.set(container, scrollContainer);
227
233
 
228
234
  intersectionObserver.observe(container);
229
235
  mutationObserver.observe(container, { attributes: true, childList: true, subtree: true });
236
+ }
237
+
238
+ document.querySelectorAll<HTMLElement>('.evo-scroll-container').forEach((container) => {
239
+ const scrollContainer = new ScrollContainer(container);
240
+ initNewScrollContainer(container, scrollContainer);
230
241
  });
231
242
 
232
243
  window.addEventListener('resize', () => {
233
244
  [...scrollContainers.values()].forEach((scrollContainer) => {
234
245
  scrollContainer.refresh();
235
246
  });
236
- });
247
+ }, { passive: true });