|
| 1 | +<template> |
| 2 | + <div class="flex min-h-0 flex-col gap-2"> |
| 3 | + <div class="flex min-h-0 flex-1 flex-col overflow-auto overscroll-y-contain"> |
| 4 | + <div |
| 5 | + ref="container" |
| 6 | + class="scrollbar-hide flex shrink-0 grow snap-x snap-mandatory overflow-x-auto overscroll-x-contain scroll-smooth" |
| 7 | + > |
| 8 | + <component v-for="(card, index) in providedCards" :key="index" :is="card" ref="cards" /> |
| 9 | + </div> |
| 10 | + </div> |
| 11 | + <div class="flex flex-col gap-2"> |
| 12 | + <h3 class="text-center text-sm font-thin"> |
| 13 | + {{ cards?.[activeIndex].title }} |
| 14 | + </h3> |
| 15 | + <div class="flex flex-none justify-center gap-2" v-if="providedCards.length > 1"> |
| 16 | + <button |
| 17 | + v-for="(c, index) in providedCards" |
| 18 | + :key="c.props?.id" |
| 19 | + @click="scrollToItem(index)" |
| 20 | + :class="[ |
| 21 | + 'size-2 rounded-full transition-all duration-700', |
| 22 | + activeIndex === index ? 'scale-125 bg-primary' : 'bg-base-content/50 hover:bg-base-content', |
| 23 | + ]" |
| 24 | + :aria-label="c.props?.title" |
| 25 | + :title="c.props?.title" |
| 26 | + /> |
| 27 | + </div> |
| 28 | + </div> |
| 29 | + </div> |
| 30 | +</template> |
| 31 | + |
| 32 | +<script lang="ts" setup> |
| 33 | +import CarouselItem from "./CarouselItem.vue"; |
| 34 | +const container = useTemplateRef<HTMLDivElement>("container"); |
| 35 | +const activeIndex = ref(0); |
| 36 | +const activeId = defineModel<string>(); |
| 37 | +const slots = defineSlots<{ default(): VNode[] }>(); |
| 38 | +const providedCards = computed(() => slots.default().filter(({ type }) => type === CarouselItem)); |
| 39 | +const cards = useTemplateRef<InstanceType<typeof CarouselItem>[]>("cards"); |
| 40 | +
|
| 41 | +const scrollToItem = (index: number) => { |
| 42 | + cards.value?.[index].$el.scrollIntoView({ |
| 43 | + behavior: "smooth", |
| 44 | + inline: "start", |
| 45 | + }); |
| 46 | +}; |
| 47 | +
|
| 48 | +const { pause, resume } = watchPausable(activeId, (v) => { |
| 49 | + if (activeId.value) { |
| 50 | + const index = cards.value?.map((c) => c.id).indexOf(activeId.value) ?? -1; |
| 51 | + if (index !== -1) { |
| 52 | + scrollToItem(index); |
| 53 | + } |
| 54 | + } |
| 55 | +}); |
| 56 | +
|
| 57 | +useIntersectionObserver( |
| 58 | + cards as Ref<InstanceType<typeof CarouselItem>[]>, |
| 59 | + (entries) => { |
| 60 | + entries.forEach(({ isIntersecting, target }) => { |
| 61 | + if (isIntersecting) { |
| 62 | + const index = cards.value?.map((c) => c.$el).indexOf(target as HTMLDivElement) ?? -1; |
| 63 | + if (index !== -1) { |
| 64 | + pause(); |
| 65 | + activeIndex.value = index; |
| 66 | + activeId.value = cards.value?.[index].id; |
| 67 | + nextTick(() => resume()); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + }, |
| 72 | + { |
| 73 | + root: container, |
| 74 | + threshold: 0.5, |
| 75 | + }, |
| 76 | +); |
| 77 | +</script> |
| 78 | + |
| 79 | +<style scoped> |
| 80 | +.scrollbar-hide { |
| 81 | + scrollbar-width: none; |
| 82 | + -ms-overflow-style: none; |
| 83 | +} |
| 84 | +.scrollbar-hide::-webkit-scrollbar { |
| 85 | + display: none; |
| 86 | +} |
| 87 | +</style> |
0 commit comments