astro-swiper 0.2.0 → 0.3.0

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": "astro-swiper",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Astro component for swiper, dedicated to slider / carousel / photo swiper / slide",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -4,7 +4,7 @@
4
4
 
5
5
  import 'swiper/css/bundle'
6
6
 
7
- import type { AstroSwiperType } from './index'
7
+ import type { AstroSwiperType } from '../index'
8
8
 
9
9
  type Props = AstroSwiperType
10
10
 
@@ -12,39 +12,73 @@ type Props = AstroSwiperType
12
12
  const {
13
13
  options={},
14
14
  class: className = '',
15
+ uniqueClass = 'astro-swiper-' + Math.random().toString(36).slice(2, 11),
16
+ linkToThumbUniqueClass = '',
15
17
  ...props
16
18
  } = Astro.props;
17
19
 
18
- const swiperId = 'astro-swiper-' + Math.random().toString(36).slice(2, 11)
19
-
20
20
  ---
21
21
 
22
22
  <!-- Slider main container -->
23
23
  <astro-swiper
24
- class={`swiper ${swiperId} ${className}`}
24
+ class={`swiper ${uniqueClass} ${className}`}
25
25
  {...props}
26
26
  data-options={JSON.stringify(options)}
27
- data-id={swiperId}>
27
+ data-linktothumbuniqueclass={linkToThumbUniqueClass}
28
+ data-uniqueclass={uniqueClass}
29
+ >
28
30
  <slot/>
29
31
  </astro-swiper>
30
32
 
31
33
 
32
34
  <script>
33
35
  import Swiper from 'swiper/bundle'
34
- import { register } from 'swiper/element/bundle';
35
-
36
+ import { useSwiper, useOptions, useDelaySwiper } from '../index'
36
37
 
37
38
  class AstroSwiper extends HTMLElement {
38
39
  constructor() {
39
40
  super();
40
- register();
41
-
42
41
 
43
42
  // Read the message from the data attribute.
44
- const options = JSON.parse(this.dataset.options || '')
45
- const id = this.dataset.id // to have more than 1 swiper in a single page
46
-
47
- const swiper = new Swiper(`.${id}`, options)
43
+ const options = JSON.parse(this.dataset.options || '{}')
44
+ const uniqueClass = this.dataset.uniqueclass || '' // to have more than 1 swiper in a single page
45
+ const linkToThumbUniqueClass = this.dataset.linktothumbuniqueclass || ''
46
+ // if (thumbOf !== '') {
47
+ // options.thumbs = { swiper: useSwiper[thumbOf], ...options.thumbs }
48
+ // console.log(options.thumbs)
49
+ // console.log('Thumb is set to ')
50
+ // console.log(useSwiper[thumbOf])
51
+ // }
52
+
53
+ if (linkToThumbUniqueClass === '') {
54
+ const swiper = new Swiper(`.${uniqueClass}`, options)
55
+ useSwiper[uniqueClass] = swiper
56
+ } else {
57
+ if (useSwiper[linkToThumbUniqueClass] !== undefined) {
58
+ options.thumbs = { swiper: useSwiper[linkToThumbUniqueClass], ...options.thumbs }
59
+ const swiper = new Swiper(`.${uniqueClass}`, options)
60
+ useSwiper[uniqueClass] = swiper
61
+ } else {
62
+ // cannot create the swiper as the thumbnail swiper not created yet
63
+ // will be done later
64
+ useDelaySwiper[uniqueClass] = linkToThumbUniqueClass
65
+ }
66
+ }
67
+
68
+ useOptions[uniqueClass] = options
69
+
70
+ // look if any delay swiper can be created
71
+ Object.keys(useDelaySwiper).forEach(delayedClass => {
72
+ const delayedThumbClass = useDelaySwiper[delayedClass]
73
+ if (useSwiper[delayedThumbClass] !== undefined) {
74
+ const delayedOptions = useOptions[delayedClass]
75
+ delayedOptions.thumbs = { swiper: useSwiper[delayedThumbClass], ...delayedOptions.thumbs }
76
+ const swiper = new Swiper(`.${delayedClass}`, delayedOptions)
77
+ useSwiper[delayedClass] = swiper
78
+ useOptions[delayedClass] = delayedOptions
79
+ delete useDelaySwiper[delayedClass]
80
+ }
81
+ })
48
82
  }
49
83
  }
50
84
 
package/src/index.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  // Copyright (c) Pascal Brand
2
2
  // MIT License
3
3
 
4
- import type { SwiperOptions } from 'swiper/types'
4
+ import type { Swiper, SwiperOptions } from 'swiper/types'
5
5
 
6
6
  import type { HTMLAttributes } from 'astro/types'
7
7
 
8
8
  export interface AstroSwiperType extends HTMLAttributes<"div"> {
9
9
  options?: SwiperOptions,
10
+ uniqueClass?: string,
11
+ linkToThumbUniqueClass?: string,
10
12
  }
11
13
 
12
14
  export { default as Swiper } from './components/Swiper.astro'
@@ -16,3 +18,7 @@ export { default as SwiperPagination } from './components/SwiperPagination.astro
16
18
  export { default as SwiperScrollbar } from './components/SwiperScrollbar.astro'
17
19
  export { default as SwiperSlide } from './components/SwiperSlide.astro'
18
20
  export { default as SwiperWrapper } from './components/SwiperWrapper.astro'
21
+
22
+ export const useSwiper: { [className: string] : Swiper; } = {};
23
+ export const useOptions: { [className: string] : SwiperOptions; } = {};
24
+ export const useDelaySwiper: { [className: string] : string; } = {};