-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
emblaCarouselSvelte.ts
79 lines (68 loc) · 1.91 KB
/
emblaCarouselSvelte.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { ActionReturn } from 'svelte/action'
import {
areOptionsEqual,
arePluginsEqual,
canUseDOM
} from 'embla-carousel-reactive-utils'
import EmblaCarousel, {
EmblaCarouselType,
EmblaOptionsType,
EmblaPluginType
} from 'embla-carousel'
type EmblaCarouselParameterType = {
options: EmblaOptionsType
plugins: EmblaPluginType[]
}
type EmblaCarouselAttributesType = {
'on:emblaInit'?: (evt: CustomEvent<EmblaCarouselType>) => void
onemblaInit?: (evt: CustomEvent<EmblaCarouselType>) => void
}
export type EmblaCarouselSvelteType = ActionReturn<
EmblaCarouselParameterType,
EmblaCarouselAttributesType
>
function emblaCarouselSvelte(
emblaNode: HTMLElement,
emblaConfig: EmblaCarouselParameterType = { options: {}, plugins: [] }
): EmblaCarouselSvelteType {
let storedEmblaConfig = emblaConfig
let emblaApi: EmblaCarouselType
if (canUseDOM()) {
EmblaCarousel.globalOptions = emblaCarouselSvelte.globalOptions
emblaApi = EmblaCarousel(
emblaNode,
storedEmblaConfig.options,
storedEmblaConfig.plugins
)
emblaApi.on('init', () =>
emblaNode.dispatchEvent(
new CustomEvent('emblaInit', { detail: emblaApi })
)
)
}
return {
destroy: () => {
if (emblaApi) emblaApi.destroy()
},
update: (newEmblaConfig) => {
const optionsChanged = !areOptionsEqual(
storedEmblaConfig.options,
newEmblaConfig.options
)
const pluginsChanged = !arePluginsEqual(
storedEmblaConfig.plugins,
newEmblaConfig.plugins
)
if (!optionsChanged && !pluginsChanged) return
storedEmblaConfig = newEmblaConfig
if (emblaApi) {
emblaApi.reInit(storedEmblaConfig.options, storedEmblaConfig.plugins)
}
}
}
}
declare namespace emblaCarouselSvelte {
let globalOptions: EmblaOptionsType | undefined
}
emblaCarouselSvelte.globalOptions = undefined
export default emblaCarouselSvelte