-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathSplide.vue
145 lines (125 loc) · 3.21 KB
/
Splide.vue
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<template>
<component :is="tag" class="splide" ref="root">
<SplideTrack v-if="hasTrack">
<slot></slot>
</SplideTrack>
<slot v-else></slot>
</component>
</template>
<script lang="ts">
import { type ComponentConstructor, type Options, Splide } from '@splidejs/splide';
import { computed, defineComponent, onBeforeUnmount, onMounted, type PropType, provide, type Ref, ref, watch } from 'vue';
import { EVENTS } from '../../constants/events';
import { SPLIDE_INJECTION_KEY } from '../../constants/keys';
import { merge } from '../../utils';
import SplideTrack from '../SplideTrack/SplideTrack.vue';
/**
* The component for the Splide root element.
*
* @since 0.4.0
*/
export default defineComponent( {
name: 'Splide',
emits: EVENTS.map( event => `splide:${ event }` ),
components: { SplideTrack },
props: {
/**
* Changes the tag name.
*/
tag: {
default: 'div',
type : String,
},
/**
* Options for Splide instance.
*/
options: {
default: {},
type : Object as PropType<Options>,
},
/**
* Registers extension components.
*/
extensions: Object as PropType<Record<string, ComponentConstructor>>,
/**
* Registers a transition component.
*/
transition: Function as PropType<ComponentConstructor>,
/**
* Determines whether to render a track element or not.
*/
hasTrack: {
default: true,
type : Boolean,
},
},
setup( props, context ) {
const splide = ref<Splide>();
const root = ref<HTMLElement>();
onMounted( () => {
if ( root.value ) {
splide.value = new Splide( root.value, props.options );
bind( splide.value );
splide.value.mount( props.extensions, props.transition );
}
} );
onBeforeUnmount( () => {
splide.value?.destroy();
} );
watch( () => merge( {}, props.options ), options => {
if ( splide.value ) {
splide.value.options = options;
}
}, { deep: true } );
provide<Ref<Splide | undefined>>( SPLIDE_INJECTION_KEY, splide );
/**
* Returns the current index.
*/
const index = computed( () => splide.value?.index || 0 );
/**
* Returns the latest number of slides.
*/
const length = computed( () => splide.value?.length || 0 );
/**
* Goes to the slide specified by the control.
*
* @see `Splide#go()`
*
* @param control - A control pattern.
*/
function go( control: number | string ): void {
splide.value?.go( control );
}
/**
* Registers another splide instance to sync.
*
* @param target - A target to sync.
*/
function sync( target: Splide ): void {
splide.value?.sync( target );
}
/**
* Listens to splide events and propagates them as Vue events.
*
* @private
*
* @param splide - A splide instance.
*/
function bind( splide: Splide ): void {
EVENTS.forEach( event => {
splide.on( event, ( ...args: any[] ) => {
context.emit( `splide:${ event }`, splide, ...args );
} );
} );
}
return {
splide,
root,
index,
length,
go,
sync,
}
},
} );
</script>