feat(Carousel): add select event (#3678)

Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
Polly
2025-04-22 15:44:47 +01:00
committed by GitHub
parent e25aa78050
commit 22edfd708a
2 changed files with 14 additions and 0 deletions

View File

@@ -252,6 +252,10 @@ class: 'p-8 px-16'
:component-slots :component-slots
### Emits
:component-emits
### Expose ### Expose
You can access the typed component instance using [`useTemplateRef`](https://vuejs.org/api/composition-api-helpers.html#usetemplateref). You can access the typed component instance using [`useTemplateRef`](https://vuejs.org/api/composition-api-helpers.html#usetemplateref).

View File

@@ -99,6 +99,13 @@ export type CarouselSlots<T extends CarouselItem = CarouselItem> = {
default(props: { item: T, index: number }): any default(props: { item: T, index: number }): any
} }
export interface CarouselEmits {
/**
* Emitted when the selected slide changes
* @param selectedIndex The index of the selected slide
*/
select: [selectedIndex: number]
}
</script> </script>
<script setup lang="ts" generic="T extends CarouselItem"> <script setup lang="ts" generic="T extends CarouselItem">
@@ -141,6 +148,7 @@ const props = withDefaults(defineProps<CarouselProps<T>>(), {
wheelGestures: false wheelGestures: false
}) })
defineSlots<CarouselSlots<T>>() defineSlots<CarouselSlots<T>>()
const emits = defineEmits<CarouselEmits>()
const { dir, t } = useLocale() const { dir, t } = useLocale()
const appConfig = useAppConfig() as Carousel['AppConfig'] const appConfig = useAppConfig() as Carousel['AppConfig']
@@ -242,6 +250,8 @@ function onSelect(api: EmblaCarouselType) {
canScrollNext.value = api?.canScrollNext() || false canScrollNext.value = api?.canScrollNext() || false
canScrollPrev.value = api?.canScrollPrev() || false canScrollPrev.value = api?.canScrollPrev() || false
selectedIndex.value = api?.selectedScrollSnap() || 0 selectedIndex.value = api?.selectedScrollSnap() || 0
emits('select', selectedIndex.value)
} }
onMounted(() => { onMounted(() => {