-
Notifications
You must be signed in to change notification settings - Fork 707
feat: add support for observing selected carousel slide index #3678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v3
Are you sure you want to change the base?
Conversation
I tried to achieve this functionality without this change by adding event listeners to the emblaApi prop (i used a reference to access an instance of the Carousel component in the setup script) but that didn't work. |
commit: |
Here's the code that I was using to try and achieve this functionality via the <style>
@import "tailwindcss";
@import "@nuxt/ui";
@import "assets/css/main.css";
</style>
<script setup lang="ts">
const runtimeConfig = useRuntimeConfig();
const carousel = useTemplateRef("carousel");
const props = defineProps<{
item: any,
selectedImageIndex: number
}>();
const emit = defineEmits<{
close: [],
selectedSlideIndexUpdated: [number]
}>();
// function selectedSlideIndexUpdated(slide: number) {
// emit('selectedSlideIndexUpdated', slide);
// }
onMounted(() => {
const emblaApi = carousel.value?.emblaApi;
function selectedSlideIndexUpdated() {
console.log("selected slide changed");
const slide = emblaApi?.selectedScrollSnap();
if (!slide) return;
console.log('selected slide changed');
emit('selectedSlideIndexUpdated', slide);
}
emblaApi?.on('init', selectedSlideIndexUpdated)
emblaApi?.on('reInit', selectedSlideIndexUpdated)
emblaApi?.on('select', selectedSlideIndexUpdated)
});
</script>
<template>
<UModal
:close="{ onClick: () => emit('close') }"
:title="item.name"
:description="item.description"
:ui="{ title: 'font-semibold text-xl', description: 'font-normal' }"
>
<template #body>
<!-- @select="selectedSlideIndexUpdated" -->
<UCarousel ref="carousel" :start-index="selectedImageIndex" v-slot="{ item: imageKey }" :items="item.imageNames" :ui="{ item: 'basis-full' }" class="rounded-lg overflow-hidden" arrows>
<img :src="`${runtimeConfig.public.instance.prefix ?? ''}/items/img/${item.identifier}/${imageKey}`" class="w-full" draggable="true">
</UCarousel>
</template>
</UModal>
</template> note that This issue is caused because the embla |
const emit = defineEmits<{ | ||
select: [selectedIndex: number] | ||
}>() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const emit = defineEmits<{ | |
select: [selectedIndex: number] | |
}>() | |
const emits = defineEmits<CarouselEmits>() |
@@ -247,6 +257,7 @@ function onSelect(api: EmblaCarouselType) { | |||
canScrollNext.value = api?.canScrollNext() || false | |||
canScrollPrev.value = api?.canScrollPrev() || false | |||
selectedIndex.value = api?.selectedScrollSnap() || 0 | |||
emit('select', selectedIndex.value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emit('select', selectedIndex.value) | |
emits('select', selectedIndex.value) |
π Linked issue
β Type of change
π Description
This change adds a new event emitter to the Carousel component that emits the index of the currently selected slide. This change is required to make carousels support reactivity: i.e, passing state from one carousel to another.
π Checklist