Skip to content

Commit

Permalink
simplify carousel control
Browse files Browse the repository at this point in the history
  • Loading branch information
cschroeter committed Dec 27, 2024
1 parent bdc3d9c commit 3655209
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
12 changes: 8 additions & 4 deletions packages/react/src/components/carousel/carousel-control.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { mergeProps } from '@zag-js/react'
import { forwardRef } from 'react'
import { type HTMLProps, type PolymorphicProps, ark } from '../factory'
import { carouselAnatomy } from './carousel.anatomy'
import { useCarouselContext } from './use-carousel-context'

export interface CarouselControlBaseProps extends PolymorphicProps {}
export interface CarouselControlProps extends HTMLProps<'div'>, CarouselControlBaseProps {}

export const CarouselControl = forwardRef<HTMLDivElement, CarouselControlProps>((props, ref) => (
<ark.div {...carouselAnatomy.build().control.attrs} {...props} ref={ref} />
))
export const CarouselControl = forwardRef<HTMLDivElement, CarouselControlProps>((props, ref) => {
const carousel = useCarouselContext()
const mergedProps = mergeProps(carousel.getControlProps(), props)

return <ark.div {...mergedProps} {...props} ref={ref} />
})

CarouselControl.displayName = 'CarouselControl'
4 changes: 1 addition & 3 deletions packages/react/src/components/carousel/carousel.anatomy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { anatomy } from '@zag-js/carousel'

export const carouselAnatomy = anatomy.extendWith('control')
export { anatomy as carouselAnatomy } from '@zag-js/carousel'
5 changes: 3 additions & 2 deletions packages/solid/src/components/carousel/carousel-control.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { mergeProps } from '@zag-js/solid'
import { type HTMLProps, type PolymorphicProps, ark } from '../factory'
import { carouselAnatomy } from './carousel.anatomy'
import { useCarouselContext } from './use-carousel-context'

export interface CarouselControlBaseProps extends PolymorphicProps<'div'> {}
export interface CarouselControlProps extends HTMLProps<'div'>, CarouselControlBaseProps {}

export const CarouselControl = (props: CarouselControlProps) => {
const mergedProps = mergeProps(() => carouselAnatomy.build().control.attrs, props)
const api = useCarouselContext()
const mergedProps = mergeProps(() => api().getControlProps(), props)

return <ark.div {...mergedProps} />
}
4 changes: 1 addition & 3 deletions packages/solid/src/components/carousel/carousel.anatomy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { anatomy } from '@zag-js/carousel'

export const carouselAnatomy = anatomy.extendWith('control')
export { anatomy as carouselAnatomy } from '@zag-js/carousel'
5 changes: 3 additions & 2 deletions packages/vue/src/components/carousel/carousel-control.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import type { HTMLAttributes } from 'vue'
import type { PolymorphicProps } from '../factory'
import { useCarouselContext } from './use-carousel-context'
export interface CarouselControlBaseProps extends PolymorphicProps {}
export interface CarouselControlProps
Expand All @@ -14,15 +15,15 @@ export interface CarouselControlProps
<script setup lang="ts">
import { ark } from '../factory'
import { useForwardExpose } from '../../utils'
import { carouselAnatomy } from './carousel.anatomy'
defineProps<CarouselControlProps>()
const carousel = useCarouselContext()
useForwardExpose()
</script>

<template>
<ark.div v-bind="{ ...carouselAnatomy.build().control.attrs }" :as-child="asChild">
<ark.div v-bind="carousel.getControlProps()" :as-child="asChild">
<slot />
</ark.div>
</template>
4 changes: 1 addition & 3 deletions packages/vue/src/components/carousel/carousel.anatomy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { anatomy } from '@zag-js/carousel'

export const carouselAnatomy = anatomy.extendWith('control')
export { anatomy as carouselAnatomy } from '@zag-js/carousel'

0 comments on commit 3655209

Please sign in to comment.