From 3b67d54833462760406786d9ba8a18eea2a8bde0 Mon Sep 17 00:00:00 2001 From: Mike Newbon Date: Mon, 30 Jun 2025 14:55:07 +0200 Subject: [PATCH] fix(Carousel): resolve plugins with page transitions (#4380) Co-authored-by: Benjamin Canac --- src/runtime/components/Carousel.vue | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/runtime/components/Carousel.vue b/src/runtime/components/Carousel.vue index 4faa2517..ca8f7dbb 100644 --- a/src/runtime/components/Carousel.vue +++ b/src/runtime/components/Carousel.vue @@ -118,7 +118,7 @@ export interface CarouselEmits { import { computed, ref, watch, onMounted } from 'vue' import useEmblaCarousel from 'embla-carousel-vue' import { Primitive, useForwardProps } from 'reka-ui' -import { reactivePick, computedAsync } from '@vueuse/core' +import { reactivePick } from '@vueuse/core' import { useAppConfig } from '#imports' import { useLocale } from '../composables/useLocale' import { tv } from '../utils/tv' @@ -175,41 +175,45 @@ const options = computed(() => ({ direction: dir.value === 'rtl' ? 'rtl' : 'ltr' })) -const plugins = computedAsync(async () => { - const plugins = [] +const plugins = ref([]) + +async function loadPlugins() { + const emblaPlugins: EmblaPluginType[] = [] if (props.autoplay) { const AutoplayPlugin = await import('embla-carousel-autoplay').then(r => r.default) - plugins.push(AutoplayPlugin(typeof props.autoplay === 'boolean' ? {} : props.autoplay)) + emblaPlugins.push(AutoplayPlugin(typeof props.autoplay === 'boolean' ? {} : props.autoplay)) } if (props.autoScroll) { const AutoScrollPlugin = await import('embla-carousel-auto-scroll').then(r => r.default) - plugins.push(AutoScrollPlugin(typeof props.autoScroll === 'boolean' ? {} : props.autoScroll)) + emblaPlugins.push(AutoScrollPlugin(typeof props.autoScroll === 'boolean' ? {} : props.autoScroll)) } if (props.autoHeight) { const AutoHeightPlugin = await import('embla-carousel-auto-height').then(r => r.default) - plugins.push(AutoHeightPlugin(typeof props.autoHeight === 'boolean' ? {} : props.autoHeight)) + emblaPlugins.push(AutoHeightPlugin(typeof props.autoHeight === 'boolean' ? {} : props.autoHeight)) } if (props.classNames) { const ClassNamesPlugin = await import('embla-carousel-class-names').then(r => r.default) - plugins.push(ClassNamesPlugin(typeof props.classNames === 'boolean' ? {} : props.classNames)) + emblaPlugins.push(ClassNamesPlugin(typeof props.classNames === 'boolean' ? {} : props.classNames)) } if (props.fade) { const FadePlugin = await import('embla-carousel-fade').then(r => r.default) - plugins.push(FadePlugin(typeof props.fade === 'boolean' ? {} : props.fade)) + emblaPlugins.push(FadePlugin(typeof props.fade === 'boolean' ? {} : props.fade)) } if (props.wheelGestures) { const { WheelGesturesPlugin } = await import('embla-carousel-wheel-gestures') - plugins.push(WheelGesturesPlugin(typeof props.wheelGestures === 'boolean' ? {} : props.wheelGestures)) + emblaPlugins.push(WheelGesturesPlugin(typeof props.wheelGestures === 'boolean' ? {} : props.wheelGestures)) } - return plugins -}) + plugins.value = emblaPlugins +} + +watch(() => [props.autoplay, props.autoScroll, props.autoHeight, props.classNames, props.fade, props.wheelGestures], loadPlugins, { immediate: true }) const [emblaRef, emblaApi] = useEmblaCarousel(options.value, plugins.value)