feat: add new-york styling for carousels
This commit is contained in:
parent
34ca4e34d8
commit
963217bfa4
|
|
@ -991,6 +991,20 @@ export const Index = {
|
||||||
component: () => import('../src/lib/registry/new-york/example/CardWithForm.vue').then(m => m.default),
|
component: () => import('../src/lib/registry/new-york/example/CardWithForm.vue').then(m => m.default),
|
||||||
files: ['../src/lib/registry/new-york/example/CardWithForm.vue'],
|
files: ['../src/lib/registry/new-york/example/CardWithForm.vue'],
|
||||||
},
|
},
|
||||||
|
CarouselDemo: {
|
||||||
|
name: 'CarouselDemo',
|
||||||
|
type: 'components:example',
|
||||||
|
registryDependencies: ['carousel', 'button'],
|
||||||
|
component: () => import('../src/lib/registry/new-york/example/CarouselDemo.vue').then(m => m.default),
|
||||||
|
files: ['../src/lib/registry/new-york/example/CarouselDemo.vue'],
|
||||||
|
},
|
||||||
|
CarouselOrientation: {
|
||||||
|
name: 'CarouselOrientation',
|
||||||
|
type: 'components:example',
|
||||||
|
registryDependencies: ['carousel', 'button'],
|
||||||
|
component: () => import('../src/lib/registry/new-york/example/CarouselOrientation.vue').then(m => m.default),
|
||||||
|
files: ['../src/lib/registry/new-york/example/CarouselOrientation.vue'],
|
||||||
|
},
|
||||||
CheckboxDemo: {
|
CheckboxDemo: {
|
||||||
name: 'CheckboxDemo',
|
name: 'CheckboxDemo',
|
||||||
type: 'components:example',
|
type: 'components:example',
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ChevronRightIcon } from '@radix-icons/vue'
|
import { ChevronRight } from 'lucide-vue-next'
|
||||||
import { useCarousel } from './useCarousel'
|
import { useCarousel } from './useCarousel'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { Button } from '@/lib/registry/default/ui/button'
|
import { Button } from '@/lib/registry/default/ui/button'
|
||||||
|
|
@ -20,6 +20,6 @@ const { orientation, canScrollNext, scrollNext } = useCarousel()
|
||||||
variant="outline"
|
variant="outline"
|
||||||
@click="scrollNext"
|
@click="scrollNext"
|
||||||
>
|
>
|
||||||
<ChevronRightIcon class="h-4 w-4 text-white stroke-white" />
|
<ChevronRight class="h-4 w-4 text-white stroke-white" />
|
||||||
</Button>
|
</Button>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ChevronLeftIcon } from '@radix-icons/vue'
|
import { ChevronLeft } from 'lucide-vue-next'
|
||||||
import { useCarousel } from './useCarousel'
|
import { useCarousel } from './useCarousel'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import { Button } from '@/lib/registry/default/ui/button'
|
import { Button } from '@/lib/registry/default/ui/button'
|
||||||
|
|
@ -20,6 +20,6 @@ const { orientation, canScrollPrev, scrollPrev } = useCarousel()
|
||||||
variant="outline"
|
variant="outline"
|
||||||
@click="scrollPrev"
|
@click="scrollPrev"
|
||||||
>
|
>
|
||||||
<ChevronLeftIcon class="h-4 w-4 text-white" />
|
<ChevronLeft class="h-4 w-4 text-white" />
|
||||||
</Button>
|
</Button>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
||||||
24
apps/www/src/lib/registry/new-york/example/CarouselDemo.vue
Normal file
24
apps/www/src/lib/registry/new-york/example/CarouselDemo.vue
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious } from '@/lib/registry/new-york/ui/carousel'
|
||||||
|
import { Card, CardContent } from '@/lib/registry/new-york/ui/card'
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div class="flex items-center space-x-2">
|
||||||
|
<Carousel class="w-full max-w-xs">
|
||||||
|
<CarouselContent>
|
||||||
|
<CarouselItem v-for="(_, index) in 5" :key="index">
|
||||||
|
<div class="p-1">
|
||||||
|
<Card>
|
||||||
|
<CardContent class="flex aspect-square items-center justify-center p-6">
|
||||||
|
<span class="text-4xl font-semibold">{{ index + 1 }}</span>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
</CarouselItem>
|
||||||
|
</CarouselContent>
|
||||||
|
<CarouselPrevious />
|
||||||
|
<CarouselNext />
|
||||||
|
</Carousel>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious } from '@/lib/registry/new-york/ui/carousel'
|
||||||
|
import { Card, CardContent } from '@/lib/registry/new-york/ui/card'
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div class="w-1/2">
|
||||||
|
<Carousel
|
||||||
|
orientation="vertical"
|
||||||
|
class="w-full max-w-xs"
|
||||||
|
:opts="{
|
||||||
|
align: 'start',
|
||||||
|
}"
|
||||||
|
>
|
||||||
|
<CarouselContent class="-mt-1 h-[200px]">
|
||||||
|
<CarouselItem v-for="(_, index) in 5" :key="index" class="p-1 md:basis-1/2">
|
||||||
|
<div class="p-1">
|
||||||
|
<Card>
|
||||||
|
<CardContent class="flex items-center justify-center p-6">
|
||||||
|
<span class="text-3xl font-semibold">{{ index + 1 }}</span>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
</CarouselItem>
|
||||||
|
</CarouselContent>
|
||||||
|
<CarouselPrevious />
|
||||||
|
<CarouselNext />
|
||||||
|
</Carousel>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
39
apps/www/src/lib/registry/new-york/ui/carousel/Carousel.vue
Normal file
39
apps/www/src/lib/registry/new-york/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/new-york/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/new-york/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/new-york/ui/carousel/index.ts
Normal file
10
apps/www/src/lib/registry/new-york/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/new-york/ui/carousel/interface.ts
Normal file
10
apps/www/src/lib/registry/new-york/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'
|
||||||
|
}
|
||||||
|
|
@ -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 }
|
||||||
Loading…
Reference in New Issue
Block a user