vevet 5.0.3 → 5.0.5

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.
Files changed (54) hide show
  1. package/lib/cdn/vevet.iife.min.js +9 -9
  2. package/lib/cjs/components/Snap/Slide/index.js +1 -0
  3. package/lib/cjs/components/Snap/Slide/index.js.map +1 -1
  4. package/lib/cjs/components/Snap/Swipe/index.js +3 -3
  5. package/lib/cjs/components/Snap/Swipe/index.js.map +1 -1
  6. package/lib/cjs/components/Snap/Track/index.js +23 -0
  7. package/lib/cjs/components/Snap/Track/index.js.map +1 -1
  8. package/lib/cjs/components/Snap/Wheel/index.js +1 -1
  9. package/lib/cjs/components/Snap/Wheel/index.js.map +1 -1
  10. package/lib/cjs/components/Snap/index.js +63 -29
  11. package/lib/cjs/components/Snap/index.js.map +1 -1
  12. package/lib/cjs/utils/common/closest.js +15 -0
  13. package/lib/cjs/utils/common/closest.js.map +1 -0
  14. package/lib/cjs/utils/common/index.js +2 -1
  15. package/lib/cjs/utils/common/index.js.map +1 -1
  16. package/lib/cjs/version.js +1 -1
  17. package/lib/esm/components/Snap/Slide/index.js +1 -0
  18. package/lib/esm/components/Snap/Slide/index.js.map +1 -1
  19. package/lib/esm/components/Snap/Swipe/index.js +3 -3
  20. package/lib/esm/components/Snap/Swipe/index.js.map +1 -1
  21. package/lib/esm/components/Snap/Track/index.js +15 -0
  22. package/lib/esm/components/Snap/Track/index.js.map +1 -1
  23. package/lib/esm/components/Snap/Wheel/index.js +1 -1
  24. package/lib/esm/components/Snap/Wheel/index.js.map +1 -1
  25. package/lib/esm/components/Snap/index.js +52 -22
  26. package/lib/esm/components/Snap/index.js.map +1 -1
  27. package/lib/esm/utils/common/closest.js +10 -0
  28. package/lib/esm/utils/common/closest.js.map +1 -0
  29. package/lib/esm/utils/common/index.js +2 -1
  30. package/lib/esm/utils/common/index.js.map +1 -1
  31. package/lib/esm/version.js +1 -1
  32. package/lib/types/components/Snap/Slide/index.d.ts.map +1 -1
  33. package/lib/types/components/Snap/Swipe/index.d.ts.map +1 -1
  34. package/lib/types/components/Snap/Track/index.d.ts +4 -0
  35. package/lib/types/components/Snap/Track/index.d.ts.map +1 -1
  36. package/lib/types/components/Snap/index.d.ts +8 -4
  37. package/lib/types/components/Snap/index.d.ts.map +1 -1
  38. package/lib/types/components/Snap/types.d.ts +13 -1
  39. package/lib/types/components/Snap/types.d.ts.map +1 -1
  40. package/lib/types/utils/common/closest.d.ts +8 -0
  41. package/lib/types/utils/common/closest.d.ts.map +1 -0
  42. package/lib/types/utils/common/index.d.ts +2 -1
  43. package/lib/types/utils/common/index.d.ts.map +1 -1
  44. package/lib/types/version.d.ts +1 -1
  45. package/package.json +1 -1
  46. package/src/components/Snap/Slide/index.ts +1 -0
  47. package/src/components/Snap/Swipe/index.ts +4 -3
  48. package/src/components/Snap/Track/index.ts +20 -0
  49. package/src/components/Snap/Wheel/index.ts +1 -1
  50. package/src/components/Snap/index.ts +80 -23
  51. package/src/components/Snap/types.ts +15 -1
  52. package/src/utils/common/closest.ts +11 -0
  53. package/src/utils/common/index.ts +2 -1
  54. package/src/version.ts +1 -1
@@ -4,7 +4,9 @@ import {
4
4
  ISnapCallbacksMap,
5
5
  ISnapMagnet,
6
6
  ISnapMutableProps,
7
+ ISnapNexPrevArg,
7
8
  ISnapStaticProps,
9
+ ISnapToSlideArg,
8
10
  } from './types';
9
11
  import { TRequiredProps } from '@/internal/requiredProps';
10
12
  import { Raf } from '../Raf';
@@ -17,6 +19,7 @@ import {
17
19
  damp,
18
20
  lerp,
19
21
  toPixels,
22
+ closest,
20
23
  } from '@/utils';
21
24
  import { SnapSlide } from './Slide';
22
25
  import { SnapWheel } from './Wheel';
@@ -49,6 +52,7 @@ export class Snap<
49
52
  public _getStatic(): TRequiredProps<StaticProps> {
50
53
  return {
51
54
  ...super._getStatic(),
55
+ eventsEmitter: null,
52
56
  activeIndex: 0,
53
57
  } as TRequiredProps<StaticProps>;
54
58
  }
@@ -122,6 +126,9 @@ export class Snap<
122
126
  /** Active slide index */
123
127
  protected _activeIndex: number;
124
128
 
129
+ /** Target slide index */
130
+ protected _targetIndex?: number;
131
+
125
132
  constructor(props?: StaticProps & MutableProps) {
126
133
  super(props);
127
134
 
@@ -227,6 +234,11 @@ export class Snap<
227
234
  return this.props.container;
228
235
  }
229
236
 
237
+ /** Get events emitter */
238
+ get eventsEmitter() {
239
+ return this.props.eventsEmitter ?? this.container;
240
+ }
241
+
230
242
  /** Container size depending on direction (width or height) */
231
243
  get domSize() {
232
244
  return this._domSize;
@@ -297,7 +309,7 @@ export class Snap<
297
309
  const slide = slides.find(({ index }) => index === this.activeIndex);
298
310
  if (props.stickOnResize && slide) {
299
311
  this.track.clampTarget();
300
- this.track.set(slide.staticCoord);
312
+ this.track.set(slide.magnets[0]);
301
313
  }
302
314
 
303
315
  // Emit callbacks
@@ -337,18 +349,24 @@ export class Snap<
337
349
  return;
338
350
  }
339
351
 
340
- const { track, props } = this;
352
+ const { _swipe: swipe, track, props } = this;
341
353
 
342
354
  // Update values
343
355
  this._updateSlidesCoords();
344
356
  this._updateSlidesProgress();
345
357
 
346
358
  // Get magnet after slide coordinates are updated
347
- const { magnet, _swipe: swipe } = this;
359
+ const { magnet } = this;
348
360
 
349
361
  // Active index change
350
- if (magnet && magnet.slide.index !== this._activeIndex) {
362
+ if (
363
+ magnet &&
364
+ magnet.slide.index !== this._activeIndex &&
365
+ (typeof this._targetIndex === 'undefined' ||
366
+ magnet.slide.index === this._targetIndex)
367
+ ) {
351
368
  this._activeIndex = magnet.slide.index;
369
+ this._targetIndex = undefined;
352
370
  this.callbacks.emit('activeSlide', this.activeSlide);
353
371
  }
354
372
 
@@ -452,10 +470,8 @@ export class Snap<
452
470
  return undefined;
453
471
  }
454
472
 
455
- const magnet = magnets.reduce((prev, curr) =>
456
- Math.abs(curr.magnet - current) < Math.abs(prev.magnet - current)
457
- ? curr
458
- : prev,
473
+ const magnet = magnets.reduce((p, c) =>
474
+ Math.abs(c.magnet - current) < Math.abs(p.magnet - current) ? c : p,
459
475
  );
460
476
 
461
477
  return {
@@ -518,12 +534,20 @@ export class Snap<
518
534
  this._timeline = undefined;
519
535
  });
520
536
 
537
+ tm.on('destroy', () => {
538
+ this._targetIndex = undefined;
539
+ });
540
+
521
541
  tm.play();
522
542
  }
523
543
 
524
544
  /** Go to a slide by index */
525
- public toSlide(targetIndex: number, duration = this.props.duration) {
526
- const { isEmpty, activeIndex, slides, track } = this;
545
+ public toSlide(
546
+ targetIndex: number,
547
+ { direction = null, duration = this.props.duration }: ISnapToSlideArg = {},
548
+ ) {
549
+ const { isEmpty, activeIndex, slides, track, props } = this;
550
+ const { current, max, loopCount } = track;
527
551
 
528
552
  if (isEmpty) {
529
553
  return;
@@ -531,42 +555,75 @@ export class Snap<
531
555
 
532
556
  const index = loop(targetIndex, 0, this.slides.length);
533
557
 
558
+ // Stick if the same slide
559
+
534
560
  if (index === activeIndex) {
535
561
  this.stick();
536
562
 
537
563
  return;
538
564
  }
539
565
 
566
+ this._targetIndex = index;
567
+
568
+ // Get nearest magnet
569
+
540
570
  const slideMagnets = slides[index].magnets;
541
- const current = track.loopedCurrent;
571
+ const targetMagnet = slideMagnets[0] + loopCount * max;
542
572
 
543
- const magnet = slideMagnets.reduce((prev, curr) =>
544
- Math.abs(curr - current) < Math.abs(prev - current) ? curr : prev,
545
- );
573
+ // Use static magnet when not looping
574
+
575
+ if (!props.loop) {
576
+ this.toCoord(targetMagnet, duration);
577
+
578
+ return;
579
+ }
580
+
581
+ // Or calculate closest magnet
546
582
 
547
- this.toCoord(this.track.current + (magnet - track.loopedCurrent), duration);
583
+ const targetMagnetMin = targetMagnet - max;
584
+ const targetMagnetMax = targetMagnet + max;
585
+ const allMagnets = [targetMagnetMin, targetMagnet, targetMagnetMax];
586
+
587
+ if (typeof direction === 'string') {
588
+ const magnets = allMagnets.filter((magnet) =>
589
+ direction === 'next' ? magnet >= current : magnet <= current,
590
+ );
591
+ const magnet = closest(current, magnets);
592
+
593
+ this.toCoord(magnet, duration);
594
+ } else {
595
+ const magnet = closest(current, allMagnets);
596
+
597
+ this.toCoord(magnet, duration);
598
+ }
548
599
  }
549
600
 
550
601
  /** Go to next slide */
551
- public next(duration = this.props.duration) {
602
+ public next({
603
+ duration = this.props.duration,
604
+ skip = 1,
605
+ }: ISnapNexPrevArg = {}) {
552
606
  const { props, slides, activeIndex } = this;
553
607
 
554
608
  const index = props.loop
555
- ? loop(activeIndex + 1, 0, slides.length)
556
- : Math.min(activeIndex + 1, slides.length - 1);
609
+ ? loop(activeIndex + skip, 0, slides.length)
610
+ : Math.min(activeIndex + skip, slides.length - 1);
557
611
 
558
- this.toSlide(index, duration);
612
+ this.toSlide(index, { duration, direction: 'next' });
559
613
  }
560
614
 
561
615
  /** Go to previous slide */
562
- public prev(duration = this.props.duration) {
616
+ public prev({
617
+ duration = this.props.duration,
618
+ skip = 1,
619
+ }: ISnapNexPrevArg = {}) {
563
620
  const { props, slides, activeIndex } = this;
564
621
 
565
622
  const index = props.loop
566
- ? loop(activeIndex - 1, 0, slides.length)
567
- : Math.max(activeIndex - 1, 0);
623
+ ? loop(activeIndex - skip, 0, slides.length)
624
+ : Math.max(activeIndex - skip, 0);
568
625
 
569
- this.toSlide(index, duration);
626
+ this.toSlide(index, { duration, direction: 'prev' });
570
627
  }
571
628
 
572
629
  /**
@@ -13,8 +13,12 @@ import { ISnapSlideProps } from './Slide/types';
13
13
 
14
14
  /** Static properties for the Snap component */
15
15
  export interface ISnapStaticProps extends IModuleStaticProps {
16
- /** HTML container element where listeners are attached */
16
+ /** HTML container */
17
17
  container: HTMLElement;
18
+ /** HTML Element used to emit events
19
+ * @default null
20
+ */
21
+ eventsEmitter?: HTMLElement | null;
18
22
  /**
19
23
  * Default active index
20
24
  * @default 0
@@ -171,3 +175,13 @@ export interface ISnapMagnet {
171
175
  /** Difference with current coordinate */
172
176
  diff: number;
173
177
  }
178
+
179
+ export interface ISnapNexPrevArg {
180
+ duration?: TSnapDuration;
181
+ skip?: number;
182
+ }
183
+
184
+ export interface ISnapToSlideArg {
185
+ direction?: 'next' | 'prev' | null;
186
+ duration?: TSnapDuration;
187
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Get closest value in array to target value.
3
+ *
4
+ * @example
5
+ * closest(5, [3, 6, 9, 12, 15]); // => 6
6
+ */
7
+ export function closest(target: number, values: number[]) {
8
+ return values.reduce((prev, curr) =>
9
+ Math.abs(curr - target) < Math.abs(prev - target) ? curr : prev,
10
+ );
11
+ }
@@ -1,2 +1,3 @@
1
- export * from './uid';
1
+ export * from './closest';
2
2
  export * from './toPixels';
3
+ export * from './uid';
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- const version = '5.0.3';
1
+ const version = '5.0.5';
2
2
  export default version;