11<template >
22 <section
3+ ref =" carouselWrapperRef"
34 class =" carousel-basic"
45 :class =" [elementClasses]"
5- ref =" carouselWrapperRef"
66 role =" region"
77 aria-label =" Image carousel"
88 >
1111
1212 <LayoutRow tag="div" variant="full-width" :style-class-passthrough =" [' mbe-20' ] " >
1313 <div
14+ ref =" carouselContainerRef"
1415 tabindex =" 0"
1516 class =" item-container"
1617 :class =" { 'allow-overflow': allowCarouselOverflow }"
17- ref =" carouselContainerRef"
1818 role =" group"
1919 aria-label =" Carousel items"
2020 >
2121 <div
2222 v-for =" (item, index) in carouselDataIds"
2323 :key =" index"
24- class =" item"
2524 ref =" carouselItems"
25+ class =" item"
2626 :aria-current =" currentIndex === index ? 'true' : 'false'"
2727 >
2828 <slot :name =" item" ></slot >
3939 </LayoutRow >
4040
4141 <LayoutRow tag="div" variant="full-width" :style-class-passthrough =" [' mbe-20' ] " >
42- <div tabindex =" 0" class =" controls-container" ref = " controlsContainerRef " >
42+ <div ref = " controlsContainerRef " tabindex =" 0" class =" controls-container" >
4343 <div class =" markers-container" >
4444 <ul class =" markers-list" >
4545 <li v-for =" index in itemCount" :key =" index" class =" markers-item" >
4646 <button
47- @click.prevent =" jumpToFrame(index - 1)"
4847 class =" btn-marker"
4948 :class =" [{ active: currentIndex === index - 1 }]"
5049 :aria-label =" `Jump to item ${Math.floor(index + 1)}`"
50+ @click.prevent =" jumpToFrame(index - 1)"
5151 ></button >
5252 </li >
5353 </ul >
5454 </div >
5555 <div class =" buttons-container" >
56- <button type =" button" @click.prevent = " actionPrevious() " class =" btn-action" aria-label =" Go to previous item" >
56+ <button type =" button" class =" btn-action" aria-label =" Go to previous item" @click.prevent = " actionPrevious() " >
5757 <Icon name="ic:outline-keyboard-arrow-left " class="arrows-icon" />
5858 </button >
59- <button type =" button" @click.prevent = " actionNext() " class =" btn-action" aria-label =" Go to next item" >
59+ <button type =" button" class =" btn-action" aria-label =" Go to next item" @click.prevent = " actionNext() " >
6060 <Icon name="ic:outline-keyboard-arrow-right " class="arrows-icon" />
6161 </button >
6262 </div >
6666</template >
6767
6868<script setup lang="ts">
69- import { useEventListener , useResizeObserver , useSwipe } from " @vueuse/core"
69+ import { useEventListener , useResizeObserver , useSwipe } from " @vueuse/core" ;
70+ import type { PropType } from " vue" ;
7071
7172const props = defineProps ({
7273 carouselDataIds: {
@@ -89,108 +90,108 @@ const props = defineProps({
8990 type: Boolean ,
9091 default: false ,
9192 },
92- })
93+ });
9394
94- const { elementClasses } = useStyleClassPassthrough (props .styleClassPassthrough )
95+ const { elementClasses } = useStyleClassPassthrough (props .styleClassPassthrough );
9596
96- const carouselWrapperRef = ref <HTMLDivElement | null >(null )
97- const carouselContainerRef = ref <HTMLDivElement | null >(null )
98- const carouselItemsRef = useTemplateRef <HTMLDivElement []>(" carouselItems" )
99- const controlsContainerRef = ref <HTMLDivElement | null >(null )
100- const carouselInitComplete = ref (false )
97+ const carouselWrapperRef = ref <HTMLDivElement | null >(null );
98+ const carouselContainerRef = ref <HTMLDivElement | null >(null );
99+ const carouselItemsRef = useTemplateRef <HTMLDivElement []>(" carouselItems" );
100+ const controlsContainerRef = ref <HTMLDivElement | null >(null );
101+ const carouselInitComplete = ref (false );
101102
102- const currentIndex = ref (0 )
103- const itemCount = ref (props .carouselDataIds .length )
104- const offset = ref (0 )
105- const transitionSpeedStr = props .transitionSpeed + " ms"
103+ const currentIndex = ref (0 );
104+ const itemCount = ref (props .carouselDataIds .length );
105+ const offset = ref (0 );
106+ const transitionSpeedStr = props .transitionSpeed + " ms" ;
106107const itemTransform = computed (() => {
107- return ` translateX(calc(${offset .value } * (${itemWidth .value } + var(--_carousel-item-track-gap)))) `
108- })
108+ return ` translateX(calc(${offset .value } * (${itemWidth .value } + var(--_carousel-item-track-gap)))) ` ;
109+ });
109110
110- const itemWidth = ref (" 0px" )
111+ const itemWidth = ref (" 0px" );
111112
112113const actionPrevious = () => {
113114 if (props .returnToStart && currentIndex .value === 0 ) {
114- offset .value = - itemCount .value
115- doAction ()
115+ offset .value = - itemCount .value ;
116+ doAction ();
116117 }
117118
118119 if (offset .value >= 0 ) {
119- return
120+ return ;
120121 }
121122
122- offset .value = Math .min (offset .value + 1 )
123- doAction ()
124- }
123+ offset .value = Math .min (offset .value + 1 );
124+ doAction ();
125+ };
125126
126127const actionNext = () => {
127128 if (props .returnToStart && offset .value <= - 1 * (itemCount .value - 1 )) {
128- offset .value = 0
129- doAction ()
130- return
129+ offset .value = 0 ;
130+ doAction ();
131+ return ;
131132 }
132133
133134 if (offset .value <= - 1 * (itemCount .value - 1 )) {
134- return
135+ return ;
135136 }
136137
137- offset .value = Math .min (offset .value - 1 )
138- doAction ()
139- }
138+ offset .value = Math .min (offset .value - 1 );
139+ doAction ();
140+ };
140141
141142const doAction = () => {
142- currentIndex .value = Math .abs (offset .value )
143- }
143+ currentIndex .value = Math .abs (offset .value );
144+ };
144145
145146const jumpToFrame = (index : number ) => {
146147 if (index >= 0 && index < itemCount .value ) {
147- offset .value = - index
148- doAction ()
148+ offset .value = - index ;
149+ doAction ();
149150 }
150- }
151+ };
151152
152153const initialSetup = () => {
153154 if (carouselItemsRef ?.value && carouselItemsRef .value .length > 0 && carouselItemsRef .value [0 ]) {
154- itemWidth .value = carouselItemsRef .value [0 ].offsetWidth + " px"
155+ itemWidth .value = carouselItemsRef .value [0 ].offsetWidth + " px" ;
155156 }
156157
157- carouselInitComplete .value = true
158- }
158+ carouselInitComplete .value = true ;
159+ };
159160
160161const { direction } = useSwipe (carouselContainerRef , {
161162 passive: false ,
162163 onSwipeEnd() {
163164 if (direction .value === " left" ) {
164- actionNext ()
165+ actionNext ();
165166 } else if (direction .value === " right" ) {
166- actionPrevious ()
167+ actionPrevious ();
167168 }
168169 },
169- })
170+ });
170171
171172useEventListener (carouselContainerRef , " keydown" , (event : KeyboardEvent ) => {
172173 if (event .key === " ArrowLeft" ) {
173- actionPrevious ()
174+ actionPrevious ();
174175 } else if (event .key === " ArrowRight" ) {
175- actionNext ()
176+ actionNext ();
176177 }
177- })
178+ });
178179
179180useEventListener (controlsContainerRef , " keydown" , (event : KeyboardEvent ) => {
180181 if (event .key === " ArrowLeft" ) {
181- actionPrevious ()
182+ actionPrevious ();
182183 } else if (event .key === " ArrowRight" ) {
183- actionNext ()
184+ actionNext ();
184185 }
185- })
186+ });
186187
187188useResizeObserver (carouselWrapperRef , async () => {
188- initialSetup ()
189- })
189+ initialSetup ();
190+ });
190191
191192onMounted (() => {
192- initialSetup ()
193- })
193+ initialSetup ();
194+ });
194195 </script >
195196
196197<style lang="css">
0 commit comments