@inizioevoke/astro-core 1.5.2 → 1.6.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.
@@ -9,6 +9,7 @@
9
9
  --evo-scroll-container-thumb-color: #666666;
10
10
  --evo-scroll-container-thumb-border-width: 0px;
11
11
  --evo-scroll-container-thumb-border-color: var(--evo-scroll-container-track-color);
12
+ --evo-scroll-container-content-padding-right: calc(1rem + var(--evo-scroll-container-track-width));
12
13
  }
13
14
 
14
15
  evo-scroll-container,
@@ -27,7 +28,7 @@
27
28
  .evo-scroll-content {
28
29
  height: 100%;
29
30
  overflow-y: scroll;
30
- padding-right: calc(1rem + var(--evo-scroll-container-track-width));
31
+ padding-right: var(--evo-scroll-container-content-padding-right);
31
32
  -ms-overflow-style: none;
32
33
  scrollbar-width: none;
33
34
  &::-webkit-scrollbar {
@@ -30,7 +30,6 @@ export class EvoScrollContainerElement extends HTMLElement {
30
30
  return 'evo-scroll-container';
31
31
  }
32
32
 
33
- // #container: HTMLElement & { evoScrollContainer: ScrollContainer };
34
33
  #content!: HTMLElement;
35
34
  #track!: HTMLElement;
36
35
  #thumb!: HTMLElement;
@@ -41,15 +40,14 @@ export class EvoScrollContainerElement extends HTMLElement {
41
40
  #scrollbarSettings: ScrollbarSettings = {};
42
41
  #mouseupHandler: any = undefined;
43
42
  #mousemoveHandler: any = undefined;
43
+ #boundResizeHandler: () => void;
44
44
 
45
45
  constructor() {
46
46
  super();
47
+ this.#boundResizeHandler = this.refresh.bind(this);
47
48
  }
48
49
 
49
50
  connectedCallback() {
50
- // const _this = this;
51
- // this.#container = container as HTMLElement & { evoScrollContainer: ScrollContainer };
52
- // this.#container.evoScrollContainer = this;
53
51
  this.#content = this.querySelector('.evo-scroll-content') as HTMLElement;
54
52
  this.#track = this.querySelector('.evo-scroll-track') as HTMLElement;
55
53
  this.#thumb = this.querySelector('.evo-scroll-thumb') as HTMLElement;
@@ -63,41 +61,13 @@ export class EvoScrollContainerElement extends HTMLElement {
63
61
  intersectionObserver.observe(this);
64
62
  mutationObserver.observe(this, { attributes: true, childList: true, subtree: true });
65
63
 
66
- window.addEventListener('resize', this.refresh.bind(this), { passive: true });
64
+ window.addEventListener('resize', this.#boundResizeHandler, { passive: true });
67
65
  }
68
66
 
69
- // scroll({ left, top, behavior = 'auto' }: { left?: number | string, top?: number | string, behavior?: 'auto' | 'instant' | 'smooth' } = {}) {
70
- // const current = this.getScroll();
71
- // if (typeof left == 'string') {
72
- // if (/^[\+\-]=?\d+/.test(left)) {
73
- // left = current.left + parseInt(left.replace(/=/,''), 10);
74
- // } else {
75
- // left = parseInt(left, 10);
76
- // }
77
- // if (isNaN(left)) {
78
- // left = undefined;
79
- // }
80
- // }
81
- // if (typeof top == 'string') {
82
- // if (/^[\+\-]=?\d+/.test(top)) {
83
- // top = current.top + parseInt(top.replace(/=/,''), 10);
84
- // } else {
85
- // top = parseInt(top, 10);
86
- // }
87
- // if (isNaN(top)) {
88
- // top = undefined;
89
- // }
90
- // }
91
- // left = left ?? current.left;
92
- // top = top ?? current.top;
93
- // if (left < 0) {
94
- // left = 0;
95
- // }
96
- // if (top < 0) {
97
- // top = 0;
98
- // }
99
- // this.#content.scroll({ left: left, top: top, behavior });
100
- // }
67
+ disconnectedCallback() {
68
+ intersectionObserver.unobserve(this);
69
+ window.removeEventListener('resize', this.#boundResizeHandler);
70
+ }
101
71
 
102
72
  scroll(opts?: ScrollToOptions): void;
103
73
  scroll(x: number, y: number): void;
@@ -115,17 +85,6 @@ export class EvoScrollContainerElement extends HTMLElement {
115
85
  scrollBy(x: number, y: number): void;
116
86
  scrollBy(...args: any[]) {
117
87
  this.#content.scrollBy(...args);
118
- // let opts: ScrollToOptions = {};
119
- // if (args.length === 1) {
120
- // opts = args[0] as ScrollToOptions;
121
- // } else if (args.length == 2) {
122
- // opts.left = args[0];
123
- // opts.top = args[1];
124
- // }
125
- // const current = this.getScroll();
126
- // opts.left = current.left + (opts.left ?? 0);
127
- // opts.top = current.top + (opts.top ?? 0);
128
- // this.scroll(opts);
129
88
  }
130
89
 
131
90
  get scrollLeft() {
@@ -147,8 +106,6 @@ export class EvoScrollContainerElement extends HTMLElement {
147
106
  return this.#content.scrollHeight;
148
107
  }
149
108
 
150
-
151
-
152
109
  getScroll() {
153
110
  return {
154
111
  left: this.#content.scrollLeft,
@@ -159,7 +116,7 @@ export class EvoScrollContainerElement extends HTMLElement {
159
116
  resize({ height }: { height?: number }) {
160
117
  if (typeof height == 'number') {
161
118
  this.style.height = `${height}px`;
162
- refresh();
119
+ this.refresh();
163
120
  }
164
121
  }
165
122
 
@@ -177,6 +134,13 @@ export class EvoScrollContainerElement extends HTMLElement {
177
134
  #updateThumbSize() {
178
135
  const scrollHeight = this.#content.scrollHeight - getPaddingY(this.#content);
179
136
  const containerHeight = this.clientHeight - getPaddingY(this);
137
+
138
+ if (scrollHeight <= 0) {
139
+ this.#thumb.style.height = '20px';
140
+ // this.#track.style.display = 'none';
141
+ return;
142
+ }
143
+
180
144
  const visibleRatio = containerHeight / scrollHeight;
181
145
  const thumbHeight = visibleRatio * containerHeight;
182
146
  if (this.#scrollbarSettings.thumbHeight != undefined) {
@@ -205,12 +169,17 @@ export class EvoScrollContainerElement extends HTMLElement {
205
169
  }
206
170
 
207
171
  #thumbMouseMove(e: MouseEvent) {
208
- // console.log(performance.now());
209
172
  if (this.#isDragging) {
210
173
  const deltaY = e.clientY - this.#startY;
174
+ const thumbTrackHeight = this.clientHeight - this.#thumb.clientHeight;
175
+
176
+ if (thumbTrackHeight <= 0) {
177
+ return;
178
+ }
179
+
211
180
  const scrollRatio =
212
- (this.#content.scrollHeight - this.clientHeight) /
213
- (this.clientHeight - this.#thumb.clientHeight);
181
+ (this.#content.scrollHeight - this.clientHeight) / thumbTrackHeight;
182
+
214
183
  this.#content.scrollTop = this.#startScrollTop + deltaY * scrollRatio;
215
184
  } else {
216
185
  this.#mouseDetach();
@@ -228,9 +197,15 @@ export class EvoScrollContainerElement extends HTMLElement {
228
197
  #scrollContent() {
229
198
  const scrollHeight = this.#content.scrollHeight - getPaddingY(this.#content);
230
199
  const containerHeight = this.clientHeight - getPaddingY(this);
231
- const scrollRatio = this.#content.scrollTop / (scrollHeight - containerHeight);
232
- const top = scrollRatio * (this.#track.clientHeight - this.#thumb.clientHeight);
233
- this.#thumb.style.top = `${top}px`;
200
+ const scrollableHeight = scrollHeight - containerHeight;
201
+
202
+ if (scrollableHeight <= 0) {
203
+ this.#thumb.style.top = '0px';
204
+ } else {
205
+ const scrollRatio = this.#content.scrollTop / scrollableHeight;
206
+ const top = scrollRatio * (this.#track.clientHeight - this.#thumb.clientHeight);
207
+ this.#thumb.style.top = `${top}px`;
208
+ }
234
209
  }
235
210
  }
236
211
  if (!customElements.get(EvoScrollContainerElement.htmlTagName)) {
@@ -284,25 +259,8 @@ export function getScrollContainer(selector: string | HTMLElement): IScrollConta
284
259
  }
285
260
  const scrollContainer = typeof selector == 'string' ? document.querySelector<HTMLElement>(selector) : selector;
286
261
  return scrollContainer instanceof EvoScrollContainerElement ? scrollContainer as IScrollContainer : undefined;
287
- // const key = typeof selector == 'string' ? document.querySelector<HTMLElement>(selector) : selector;
288
- // return key ? scrollContainers.get(key) : undefined;
289
262
  }
290
263
 
291
- // export function createScrollContainer(selector: string | HTMLElement): IScrollContainer | undefined {
292
- // const container = typeof selector == 'string' ? document.querySelector<HTMLElement>(selector) : selector;
293
- // if (container) {
294
- // if (scrollContainers.has(container)) {
295
- // return scrollContainers.get(container);
296
- // } else {
297
- // const scrollContainer = new ScrollContainer(container);
298
- // initNewScrollContainer(container, scrollContainer);
299
- // return scrollContainer;
300
- // }
301
- // } else {
302
- // return undefined;
303
- // }
304
- // }
305
-
306
264
  interface ScrollbarSettings {
307
265
  trackVisible?: boolean;
308
266
  thumbHeight?: number;
@@ -312,26 +270,3 @@ function getPaddingY(content: HTMLElement): number {
312
270
  const style = getComputedStyle(content);
313
271
  return parseFloat(style.paddingTop) + parseFloat(style.paddingBottom);
314
272
  }
315
-
316
- // const scrollContainers = new Map<HTMLElement, ScrollContainer>();
317
-
318
-
319
-
320
- // function initNewScrollContainer(container: HTMLElement, scrollContainer: ScrollContainer) {
321
- // scrollContainers.set(container, scrollContainer);
322
-
323
- // intersectionObserver.observe(container);
324
- // mutationObserver.observe(container, { attributes: true, childList: true, subtree: true });
325
- // }
326
-
327
- // document.querySelectorAll<HTMLElement>('.evo-scroll-container').forEach((container) => {
328
- // const scrollContainer = new ScrollContainer(container);
329
- // initNewScrollContainer(container, scrollContainer);
330
- // });
331
-
332
- // window.addEventListener('resize', () => {
333
- // // [...scrollContainers.values()].forEach((scrollContainer) => {
334
- // [...document.querySelectorAll<ScrollContainer>('scroll-container')].forEach((scrollContainer) => {
335
- // scrollContainer.refresh();
336
- // });
337
- // }, { passive: true });
@@ -0,0 +1,3 @@
1
+ export { default as Modal } from './Modal/v2/Modal.astro';
2
+ export { default as ModalOverlay } from './Modal/v2/ModalOverlay.astro';
3
+ export { default as ModalTrigger } from './Modal/v2/ModalTrigger.astro';
@@ -0,0 +1,109 @@
1
+ export type SwipeDirection = 'left' | 'right' | 'up' | 'down';
2
+ export type SwipeThreshold = '1/4' | '1/3' | '1/2' | number;
3
+
4
+ export interface OnSwipeHandlerArgs {
5
+ direction: SwipeDirection;
6
+ threshold: number;
7
+ distance: {
8
+ x: number;
9
+ y: number;
10
+ },
11
+ duration: number;
12
+ }
13
+ export type OnSwipeHandler = (args: OnSwipeHandlerArgs) => void;
14
+
15
+ export interface SwipeController {
16
+ enable: () => void;
17
+ disable: () => void;
18
+ dispose: () => void;
19
+ }
20
+
21
+ export default function onSwipe(direction: SwipeDirection, swipeThreshold: SwipeThreshold, handler: OnSwipeHandler): SwipeController {
22
+ const threshold: number = typeof swipeThreshold == 'number' ? swipeThreshold : (() => {
23
+ const ratios: Record<string, number> = { '1/4': 0.25, '1/3': 0.333, '1/2': 0.5 };
24
+ switch (direction) {
25
+ case 'up':
26
+ case 'down':
27
+ return Math.round(screen.height * (ratios[swipeThreshold] ?? ratios['1/3']));
28
+ default:
29
+ return Math.round(screen.width * (ratios[swipeThreshold] ?? ratios['1/3']));
30
+ }
31
+ })();
32
+
33
+ let listening = false;
34
+ let x: number = NaN;
35
+ let y: number = NaN;
36
+ let start: number = NaN;
37
+
38
+ const handleSwipe = (deltaX: number, deltaY: number, duration: number) => {
39
+ let flag = false;
40
+ switch (direction) {
41
+ case 'up':
42
+ flag = Math.abs(deltaY) >= threshold && Math.abs(deltaX) < threshold && deltaY > 0;
43
+ break;
44
+ case 'down':
45
+ flag = Math.abs(deltaY) >= threshold && Math.abs(deltaX) < threshold && deltaY < 0;
46
+ break;
47
+ case 'left':
48
+ flag = Math.abs(deltaX) >= threshold && Math.abs(deltaY) < threshold && deltaX > 0;
49
+ break;
50
+ case 'right':
51
+ flag = Math.abs(deltaX) >= threshold && Math.abs(deltaY) < threshold && deltaX < 0;
52
+ break;
53
+ }
54
+ if (flag) {
55
+ try {
56
+ handler({ direction, threshold, distance: { x: deltaX, y: deltaY }, duration});
57
+ } catch (err) {
58
+ console.error(err);
59
+ }
60
+ }
61
+ }
62
+
63
+ const touchstart = (e: TouchEvent) => {
64
+ if (!listening) return;
65
+ if (e.touches.length === 1 && isNaN(x)) {
66
+ start = performance.now();
67
+ x = e.touches[0].clientX;
68
+ y = e.touches[0].clientY;
69
+ } else {
70
+ start = NaN;
71
+ x = NaN;
72
+ y = NaN;
73
+ }
74
+ };
75
+
76
+ const touchend = (e: TouchEvent) => {
77
+ if (!listening) return;
78
+ if (e.changedTouches.length === 1 && !isNaN(x) && !isNaN(y)) {
79
+ const deltaX = x - e.changedTouches[0].clientX;
80
+ const deltaY = y - e.changedTouches[0].clientY;
81
+ handleSwipe(deltaX, deltaY, performance.now() - start);
82
+ x = NaN;
83
+ y = NaN;
84
+ start = NaN;
85
+ }
86
+ };
87
+
88
+ const enable = () => {
89
+ if (!listening) {
90
+ listening = true;
91
+ window.addEventListener('touchstart', touchstart, { passive: true });
92
+ window.addEventListener('touchend', touchend, { passive: true });
93
+ }
94
+ };
95
+
96
+ const disable = () => {
97
+ listening = false;
98
+ window.removeEventListener('touchstart', touchstart);
99
+ window.removeEventListener('touchend', touchend);
100
+ };
101
+
102
+ enable();
103
+
104
+ return {
105
+ enable,
106
+ disable,
107
+ dispose: disable
108
+ };
109
+ }
package/src/lib/v2.ts ADDED
@@ -0,0 +1 @@
1
+ export * as Modal from '../components/Modal/v2/Modal';