From 963217bfa4be4ca2e0ddd320ea5637e9f5ad2664 Mon Sep 17 00:00:00 2001 From: wasimTQ Date: Fri, 29 Dec 2023 16:25:43 +0530 Subject: [PATCH] feat: add new-york styling for carousels --- apps/www/__registry__/index.ts | 14 +++++ .../default/ui/carousel/CarouselNext.vue | 4 +- .../default/ui/carousel/CarouselPrevious.vue | 4 +- .../new-york/example/CarouselDemo.vue | 24 +++++++++ .../new-york/example/CarouselOrientation.vue | 30 +++++++++++ .../new-york/ui/carousel/Carousel.vue | 39 ++++++++++++++ .../new-york/ui/carousel/CarouselContent.vue | 21 ++++++++ .../new-york/ui/carousel/CarouselItem.vue | 20 +++++++ .../new-york/ui/carousel/CarouselNext.vue | 25 +++++++++ .../new-york/ui/carousel/CarouselPrevious.vue | 25 +++++++++ .../registry/new-york/ui/carousel/index.ts | 10 ++++ .../new-york/ui/carousel/interface.ts | 10 ++++ .../new-york/ui/carousel/useCarousel.ts | 54 +++++++++++++++++++ 13 files changed, 276 insertions(+), 4 deletions(-) create mode 100644 apps/www/src/lib/registry/new-york/example/CarouselDemo.vue create mode 100644 apps/www/src/lib/registry/new-york/example/CarouselOrientation.vue create mode 100644 apps/www/src/lib/registry/new-york/ui/carousel/Carousel.vue create mode 100644 apps/www/src/lib/registry/new-york/ui/carousel/CarouselContent.vue create mode 100644 apps/www/src/lib/registry/new-york/ui/carousel/CarouselItem.vue create mode 100644 apps/www/src/lib/registry/new-york/ui/carousel/CarouselNext.vue create mode 100644 apps/www/src/lib/registry/new-york/ui/carousel/CarouselPrevious.vue create mode 100644 apps/www/src/lib/registry/new-york/ui/carousel/index.ts create mode 100644 apps/www/src/lib/registry/new-york/ui/carousel/interface.ts create mode 100644 apps/www/src/lib/registry/new-york/ui/carousel/useCarousel.ts diff --git a/apps/www/__registry__/index.ts b/apps/www/__registry__/index.ts index 9dadf617..ae2bf373 100644 --- a/apps/www/__registry__/index.ts +++ b/apps/www/__registry__/index.ts @@ -991,6 +991,20 @@ export const Index = { component: () => import('../src/lib/registry/new-york/example/CardWithForm.vue').then(m => m.default), 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: { name: 'CheckboxDemo', type: 'components:example', diff --git a/apps/www/src/lib/registry/default/ui/carousel/CarouselNext.vue b/apps/www/src/lib/registry/default/ui/carousel/CarouselNext.vue index ec03059a..2c4c34b7 100644 --- a/apps/www/src/lib/registry/default/ui/carousel/CarouselNext.vue +++ b/apps/www/src/lib/registry/default/ui/carousel/CarouselNext.vue @@ -1,5 +1,5 @@ + + diff --git a/apps/www/src/lib/registry/new-york/example/CarouselOrientation.vue b/apps/www/src/lib/registry/new-york/example/CarouselOrientation.vue new file mode 100644 index 00000000..ab1cc66a --- /dev/null +++ b/apps/www/src/lib/registry/new-york/example/CarouselOrientation.vue @@ -0,0 +1,30 @@ + + + diff --git a/apps/www/src/lib/registry/new-york/ui/carousel/Carousel.vue b/apps/www/src/lib/registry/new-york/ui/carousel/Carousel.vue new file mode 100644 index 00000000..56be5281 --- /dev/null +++ b/apps/www/src/lib/registry/new-york/ui/carousel/Carousel.vue @@ -0,0 +1,39 @@ + + + diff --git a/apps/www/src/lib/registry/new-york/ui/carousel/CarouselContent.vue b/apps/www/src/lib/registry/new-york/ui/carousel/CarouselContent.vue new file mode 100644 index 00000000..7d141430 --- /dev/null +++ b/apps/www/src/lib/registry/new-york/ui/carousel/CarouselContent.vue @@ -0,0 +1,21 @@ + + + diff --git a/apps/www/src/lib/registry/new-york/ui/carousel/CarouselItem.vue b/apps/www/src/lib/registry/new-york/ui/carousel/CarouselItem.vue new file mode 100644 index 00000000..5ff44c57 --- /dev/null +++ b/apps/www/src/lib/registry/new-york/ui/carousel/CarouselItem.vue @@ -0,0 +1,20 @@ + + + diff --git a/apps/www/src/lib/registry/new-york/ui/carousel/CarouselNext.vue b/apps/www/src/lib/registry/new-york/ui/carousel/CarouselNext.vue new file mode 100644 index 00000000..37ca9f24 --- /dev/null +++ b/apps/www/src/lib/registry/new-york/ui/carousel/CarouselNext.vue @@ -0,0 +1,25 @@ + + + diff --git a/apps/www/src/lib/registry/new-york/ui/carousel/CarouselPrevious.vue b/apps/www/src/lib/registry/new-york/ui/carousel/CarouselPrevious.vue new file mode 100644 index 00000000..91c8389b --- /dev/null +++ b/apps/www/src/lib/registry/new-york/ui/carousel/CarouselPrevious.vue @@ -0,0 +1,25 @@ + + + diff --git a/apps/www/src/lib/registry/new-york/ui/carousel/index.ts b/apps/www/src/lib/registry/new-york/ui/carousel/index.ts new file mode 100644 index 00000000..339a2e6e --- /dev/null +++ b/apps/www/src/lib/registry/new-york/ui/carousel/index.ts @@ -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' diff --git a/apps/www/src/lib/registry/new-york/ui/carousel/interface.ts b/apps/www/src/lib/registry/new-york/ui/carousel/interface.ts new file mode 100644 index 00000000..328d90a5 --- /dev/null +++ b/apps/www/src/lib/registry/new-york/ui/carousel/interface.ts @@ -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' +} diff --git a/apps/www/src/lib/registry/new-york/ui/carousel/useCarousel.ts b/apps/www/src/lib/registry/new-york/ui/carousel/useCarousel.ts new file mode 100644 index 00000000..84a2e1e9 --- /dev/null +++ b/apps/www/src/lib/registry/new-york/ui/carousel/useCarousel.ts @@ -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 ') + + return carouselState +} + +export { useCarousel, useProvideCarousel }