feat: create new carousel component with embala-carousel
This commit is contained in:
parent
989c0e2024
commit
f5369b5958
|
|
@ -27,6 +27,7 @@
|
|||
"clsx": "^2.0.0",
|
||||
"codesandbox": "^2.2.3",
|
||||
"date-fns": "^2.30.0",
|
||||
"embla-carousel-vue": "8.0.0-rc17",
|
||||
"lucide-vue-next": "^0.276.0",
|
||||
"radix-vue": "^1.2.5",
|
||||
"tailwindcss-animate": "^1.0.7",
|
||||
|
|
|
|||
39
apps/www/src/lib/registry/default/ui/carousel/Carousel.vue
Normal file
39
apps/www/src/lib/registry/default/ui/carousel/Carousel.vue
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<script setup lang="ts">
|
||||
import { useForwardPropsEmits } from 'radix-vue'
|
||||
import { useProvideCarousel } from './useCarousel'
|
||||
import type { CarouselProps } from './interface'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const props = withDefaults(defineProps<CarouselProps>(), {
|
||||
orientation: 'horizontal',
|
||||
})
|
||||
|
||||
const forwarded = useForwardPropsEmits(props)
|
||||
|
||||
const { scrollNext, scrollPrev } = useProvideCarousel(props)
|
||||
|
||||
function onKeyDown(event: KeyboardEvent) {
|
||||
event.preventDefault()
|
||||
|
||||
const prevKey = props.orientation === 'vertical' ? 'ArrowUp' : 'ArrowLeft'
|
||||
const nextKey = props.orientation === 'vertical' ? 'ArrowDown' : 'ArrowRight'
|
||||
|
||||
if (event.key === prevKey)
|
||||
scrollPrev()
|
||||
|
||||
else if (event.key === nextKey)
|
||||
scrollNext()
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
:class="cn('relative', $attrs.class ?? '')"
|
||||
role="region"
|
||||
aria-roledescription="carousel"
|
||||
v-bind="forwarded"
|
||||
@keydown="onKeyDown"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
<script setup lang="ts">
|
||||
import { useCarousel } from './useCarousel'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const { carouselRef, orientation } = useCarousel()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div ref="carouselRef" class="overflow-hidden">
|
||||
<div
|
||||
:class="
|
||||
cn(
|
||||
'flex',
|
||||
orientation === 'horizontal' ? '-ml-4' : '-mt-4 flex-col',
|
||||
$attrs.class ?? '',
|
||||
)"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<script setup lang="ts">
|
||||
import { useCarousel } from './useCarousel'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const { orientation } = useCarousel()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
role="group"
|
||||
aria-roledescription="slide"
|
||||
:class="cn(
|
||||
'min-w-0 shrink-0 grow-0 basis-full',
|
||||
orientation === 'horizontal' ? 'pl-4' : 'pt-4',
|
||||
$attrs.class ?? '',
|
||||
)"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<script setup lang="ts">
|
||||
import { ChevronRightIcon } from '@radix-icons/vue'
|
||||
import { useCarousel } from './useCarousel'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { Button } from '@/lib/registry/default/ui/button'
|
||||
|
||||
const { orientation, canScrollNext, scrollNext } = useCarousel()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Button
|
||||
:disabled="!canScrollNext"
|
||||
:class="cn(
|
||||
'absolute h-10 w-10 rounded-full p-0',
|
||||
orientation === 'horizontal'
|
||||
? '-right-12 top-1/2 -translate-y-1/2'
|
||||
: '-bottom-12 left-1/2 -translate-x-1/2 rotate-90',
|
||||
$attrs.class ?? '',
|
||||
)"
|
||||
variant="outline"
|
||||
@click="scrollNext"
|
||||
>
|
||||
<ChevronRightIcon class="h-4 w-4 text-white stroke-white" />
|
||||
</Button>
|
||||
</template>
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<script setup lang="ts">
|
||||
import { ChevronLeftIcon } from '@radix-icons/vue'
|
||||
import { useCarousel } from './useCarousel'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { Button } from '@/lib/registry/default/ui/button'
|
||||
|
||||
const { orientation, canScrollPrev, scrollPrev } = useCarousel()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Button
|
||||
:disabled="!canScrollPrev"
|
||||
:class="cn(
|
||||
'absolute h-10 w-10 rounded-full p-0',
|
||||
orientation === 'horizontal'
|
||||
? '-left-12 top-1/2 -translate-y-1/2'
|
||||
: '-top-12 left-1/2 -translate-x-1/2 rotate-90',
|
||||
$attrs.class ?? '',
|
||||
)"
|
||||
variant="outline"
|
||||
@click="scrollPrev"
|
||||
>
|
||||
<ChevronLeftIcon class="h-4 w-4 text-white" />
|
||||
</Button>
|
||||
</template>
|
||||
10
apps/www/src/lib/registry/default/ui/carousel/index.ts
Normal file
10
apps/www/src/lib/registry/default/ui/carousel/index.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
export { default as Carousel } from './Carousel.vue'
|
||||
export { default as CarouselContent } from './CarouselContent.vue'
|
||||
export { default as CarouselItem } from './CarouselItem.vue'
|
||||
export { default as CarouselPrevious } from './CarouselPrevious.vue'
|
||||
export { default as CarouselNext } from './CarouselNext.vue'
|
||||
export { useCarousel } from './useCarousel'
|
||||
|
||||
export {
|
||||
type EmblaCarouselType as CarouselApi,
|
||||
} from 'embla-carousel-vue'
|
||||
10
apps/www/src/lib/registry/default/ui/carousel/interface.ts
Normal file
10
apps/www/src/lib/registry/default/ui/carousel/interface.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import {
|
||||
type EmblaOptionsType as CarouselOptions,
|
||||
type EmblaPluginType as CarouselPlugin,
|
||||
} from 'embla-carousel-vue'
|
||||
|
||||
export interface CarouselProps {
|
||||
opts?: CarouselOptions
|
||||
plugins?: CarouselPlugin[]
|
||||
orientation?: 'horizontal' | 'vertical'
|
||||
}
|
||||
54
apps/www/src/lib/registry/default/ui/carousel/useCarousel.ts
Normal file
54
apps/www/src/lib/registry/default/ui/carousel/useCarousel.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import { createInjectionState } from '@vueuse/core'
|
||||
import emblaCarouselVue, {
|
||||
type EmblaCarouselType as CarouselApi,
|
||||
} from 'embla-carousel-vue'
|
||||
import { onMounted, ref } from 'vue'
|
||||
import type { CarouselProps } from './interface'
|
||||
|
||||
const [useProvideCarousel, useInjectCarousel] = createInjectionState(
|
||||
({
|
||||
opts, orientation, plugins,
|
||||
}: CarouselProps) => {
|
||||
const [emblaNode, emblaApi] = emblaCarouselVue({
|
||||
...opts,
|
||||
axis: orientation === 'horizontal' ? 'x' : 'y',
|
||||
}, plugins)
|
||||
|
||||
function scrollPrev() {
|
||||
emblaApi.value?.scrollPrev()
|
||||
}
|
||||
function scrollNext() {
|
||||
emblaApi.value?.scrollNext()
|
||||
}
|
||||
|
||||
const canScrollNext = ref(true)
|
||||
const canScrollPrev = ref(true)
|
||||
|
||||
function onSelect(api: CarouselApi) {
|
||||
canScrollNext.value = api.canScrollNext()
|
||||
canScrollPrev.value = api.canScrollPrev()
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (!emblaApi.value)
|
||||
return
|
||||
|
||||
emblaApi.value?.on('init', onSelect)
|
||||
emblaApi.value?.on('reInit', onSelect)
|
||||
emblaApi.value?.on('select', onSelect)
|
||||
})
|
||||
|
||||
return { carouselRef: emblaNode, carouselApi: emblaApi, canScrollPrev, canScrollNext, scrollPrev, scrollNext, orientation }
|
||||
},
|
||||
)
|
||||
|
||||
function useCarousel() {
|
||||
const carouselState = useInjectCarousel()
|
||||
|
||||
if (!carouselState)
|
||||
throw new Error('useCarousel must be used within a <Carousel />')
|
||||
|
||||
return carouselState
|
||||
}
|
||||
|
||||
export { useCarousel, useProvideCarousel }
|
||||
|
|
@ -83,6 +83,9 @@ importers:
|
|||
date-fns:
|
||||
specifier: ^2.30.0
|
||||
version: 2.30.0
|
||||
embla-carousel-vue:
|
||||
specifier: 8.0.0-rc17
|
||||
version: 8.0.0-rc17(vue@3.3.7)
|
||||
lucide-vue-next:
|
||||
specifier: ^0.276.0
|
||||
version: 0.276.0(vue@3.3.7)
|
||||
|
|
@ -6895,6 +6898,28 @@ packages:
|
|||
resolution: {integrity: sha512-L6uRgvZTH+4OF5NE/MBbzQx/WYpru1xCBE9respNj6qznEewGUIfhzmm7horWWxbNO2M0WckQypGctR8lH79xQ==}
|
||||
dev: false
|
||||
|
||||
/embla-carousel-reactive-utils@8.0.0-rc17(embla-carousel@8.0.0-rc17):
|
||||
resolution: {integrity: sha512-eluEOK/u5HdjYaTLC4bUG3iTCnyX7RsYix3il0aH4ZECOKa5fS+pVK2vrM17Mgw6C5Hyjcr3r3lfJtGerVzVsQ==}
|
||||
peerDependencies:
|
||||
embla-carousel: 8.0.0-rc17
|
||||
dependencies:
|
||||
embla-carousel: 8.0.0-rc17
|
||||
dev: false
|
||||
|
||||
/embla-carousel-vue@8.0.0-rc17(vue@3.3.7):
|
||||
resolution: {integrity: sha512-+LHBImxj5Z8OhQHuBxbLhBNZ9cyS1UvXpCbPwlYvUllZ5XbzN08eFNiG7aBnOzxx2WzX2mc6tFzToN+HbO3QPg==}
|
||||
peerDependencies:
|
||||
vue: ^3.2.37
|
||||
dependencies:
|
||||
embla-carousel: 8.0.0-rc17
|
||||
embla-carousel-reactive-utils: 8.0.0-rc17(embla-carousel@8.0.0-rc17)
|
||||
vue: 3.3.7(typescript@5.2.2)
|
||||
dev: false
|
||||
|
||||
/embla-carousel@8.0.0-rc17:
|
||||
resolution: {integrity: sha512-evF49b88VOitvqFtlvhvKVSu96Y8A+QSFdhok87Bfm8R7OYuk95FT+o8+M1GQLi/EhGDUlT193HTVAR0Wt2neQ==}
|
||||
dev: false
|
||||
|
||||
/emoji-regex@10.2.1:
|
||||
resolution: {integrity: sha512-97g6QgOk8zlDRdgq1WxwgTMgEWGVAQvB5Fdpgc1MkNy56la5SKP9GsMXKDOdqwn90/41a8yPwIGk1Y6WVbeMQA==}
|
||||
dev: false
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user