refactor: change ways to better pass the data to parent
This commit is contained in:
parent
9f860844a0
commit
a09acece3f
|
|
@ -1,16 +1,17 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useForwardPropsEmits } from 'radix-vue'
|
import { useForwardPropsEmits } from 'radix-vue'
|
||||||
import { useProvideCarousel } from './useCarousel'
|
import { useProvideCarousel } from './useCarousel'
|
||||||
import type { CarouselProps } from './interface'
|
import type { CarouselEmits, CarouselProps } from './interface'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
|
|
||||||
const props = withDefaults(defineProps<CarouselProps>(), {
|
const props = withDefaults(defineProps<CarouselProps>(), {
|
||||||
orientation: 'horizontal',
|
orientation: 'horizontal',
|
||||||
})
|
})
|
||||||
|
|
||||||
const forwarded = useForwardPropsEmits(props)
|
const emits = defineEmits<CarouselEmits>()
|
||||||
|
|
||||||
const { scrollNext, scrollPrev } = useProvideCarousel(props)
|
const forwarded = useForwardPropsEmits(props)
|
||||||
|
const carouselArgs = useProvideCarousel(props, emits)
|
||||||
|
|
||||||
function onKeyDown(event: KeyboardEvent) {
|
function onKeyDown(event: KeyboardEvent) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|
@ -19,10 +20,10 @@ function onKeyDown(event: KeyboardEvent) {
|
||||||
const nextKey = props.orientation === 'vertical' ? 'ArrowDown' : 'ArrowRight'
|
const nextKey = props.orientation === 'vertical' ? 'ArrowDown' : 'ArrowRight'
|
||||||
|
|
||||||
if (event.key === prevKey)
|
if (event.key === prevKey)
|
||||||
scrollPrev()
|
carouselArgs.scrollPrev()
|
||||||
|
|
||||||
else if (event.key === nextKey)
|
else if (event.key === nextKey)
|
||||||
scrollNext()
|
carouselArgs.scrollNext()
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
@ -34,6 +35,6 @@ function onKeyDown(event: KeyboardEvent) {
|
||||||
v-bind="forwarded"
|
v-bind="forwarded"
|
||||||
@keydown="onKeyDown"
|
@keydown="onKeyDown"
|
||||||
>
|
>
|
||||||
<slot />
|
<slot v-bind="carouselArgs" />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import {
|
import type {
|
||||||
type EmblaOptionsType as CarouselOptions,
|
EmblaCarouselType as CarouselApi,
|
||||||
type EmblaPluginType as CarouselPlugin,
|
EmblaOptionsType as CarouselOptions,
|
||||||
|
EmblaPluginType as CarouselPlugin,
|
||||||
} from 'embla-carousel-vue'
|
} from 'embla-carousel-vue'
|
||||||
|
|
||||||
export interface CarouselProps {
|
export interface CarouselProps {
|
||||||
|
|
@ -8,3 +9,7 @@ export interface CarouselProps {
|
||||||
plugins?: CarouselPlugin[]
|
plugins?: CarouselPlugin[]
|
||||||
orientation?: 'horizontal' | 'vertical'
|
orientation?: 'horizontal' | 'vertical'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CarouselEmits {
|
||||||
|
(e: 'init-api', payload: CarouselApi): void
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,12 @@ import emblaCarouselVue, {
|
||||||
type EmblaCarouselType as CarouselApi,
|
type EmblaCarouselType as CarouselApi,
|
||||||
} from 'embla-carousel-vue'
|
} from 'embla-carousel-vue'
|
||||||
import { onMounted, ref } from 'vue'
|
import { onMounted, ref } from 'vue'
|
||||||
import type { CarouselProps } from './interface'
|
import type { CarouselEmits, CarouselProps } from './interface'
|
||||||
|
|
||||||
const [useProvideCarousel, useInjectCarousel] = createInjectionState(
|
const [useProvideCarousel, useInjectCarousel] = createInjectionState(
|
||||||
({
|
({
|
||||||
opts, orientation, plugins,
|
opts, orientation, plugins,
|
||||||
}: CarouselProps) => {
|
}: CarouselProps, emits: CarouselEmits) => {
|
||||||
const [emblaNode, emblaApi] = emblaCarouselVue({
|
const [emblaNode, emblaApi] = emblaCarouselVue({
|
||||||
...opts,
|
...opts,
|
||||||
axis: orientation === 'horizontal' ? 'x' : 'y',
|
axis: orientation === 'horizontal' ? 'x' : 'y',
|
||||||
|
|
@ -36,6 +36,8 @@ const [useProvideCarousel, useInjectCarousel] = createInjectionState(
|
||||||
emblaApi.value?.on('init', onSelect)
|
emblaApi.value?.on('init', onSelect)
|
||||||
emblaApi.value?.on('reInit', onSelect)
|
emblaApi.value?.on('reInit', onSelect)
|
||||||
emblaApi.value?.on('select', onSelect)
|
emblaApi.value?.on('select', onSelect)
|
||||||
|
|
||||||
|
emits('init-api', emblaApi.value)
|
||||||
})
|
})
|
||||||
|
|
||||||
return { carouselRef: emblaNode, carouselApi: emblaApi, canScrollPrev, canScrollNext, scrollPrev, scrollNext, orientation }
|
return { carouselRef: emblaNode, carouselApi: emblaApi, canScrollPrev, canScrollNext, scrollPrev, scrollNext, orientation }
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,17 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useForwardPropsEmits } from 'radix-vue'
|
import { useForwardPropsEmits } from 'radix-vue'
|
||||||
import { useProvideCarousel } from './useCarousel'
|
import { useProvideCarousel } from './useCarousel'
|
||||||
import type { CarouselProps } from './interface'
|
import type { CarouselEmits, CarouselProps } from './interface'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
|
|
||||||
const props = withDefaults(defineProps<CarouselProps>(), {
|
const props = withDefaults(defineProps<CarouselProps>(), {
|
||||||
orientation: 'horizontal',
|
orientation: 'horizontal',
|
||||||
})
|
})
|
||||||
|
|
||||||
const forwarded = useForwardPropsEmits(props)
|
const emits = defineEmits<CarouselEmits>()
|
||||||
|
|
||||||
const { scrollNext, scrollPrev } = useProvideCarousel(props)
|
const forwarded = useForwardPropsEmits(props)
|
||||||
|
const carouselArgs = useProvideCarousel(props, emits)
|
||||||
|
|
||||||
function onKeyDown(event: KeyboardEvent) {
|
function onKeyDown(event: KeyboardEvent) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|
@ -19,10 +20,10 @@ function onKeyDown(event: KeyboardEvent) {
|
||||||
const nextKey = props.orientation === 'vertical' ? 'ArrowDown' : 'ArrowRight'
|
const nextKey = props.orientation === 'vertical' ? 'ArrowDown' : 'ArrowRight'
|
||||||
|
|
||||||
if (event.key === prevKey)
|
if (event.key === prevKey)
|
||||||
scrollPrev()
|
carouselArgs.scrollPrev()
|
||||||
|
|
||||||
else if (event.key === nextKey)
|
else if (event.key === nextKey)
|
||||||
scrollNext()
|
carouselArgs.scrollNext()
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
@ -34,6 +35,6 @@ function onKeyDown(event: KeyboardEvent) {
|
||||||
v-bind="forwarded"
|
v-bind="forwarded"
|
||||||
@keydown="onKeyDown"
|
@keydown="onKeyDown"
|
||||||
>
|
>
|
||||||
<slot />
|
<slot v-bind="carouselArgs" />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import {
|
import type {
|
||||||
type EmblaOptionsType as CarouselOptions,
|
EmblaCarouselType as CarouselApi,
|
||||||
type EmblaPluginType as CarouselPlugin,
|
EmblaOptionsType as CarouselOptions,
|
||||||
|
EmblaPluginType as CarouselPlugin,
|
||||||
} from 'embla-carousel-vue'
|
} from 'embla-carousel-vue'
|
||||||
|
|
||||||
export interface CarouselProps {
|
export interface CarouselProps {
|
||||||
|
|
@ -8,3 +9,7 @@ export interface CarouselProps {
|
||||||
plugins?: CarouselPlugin[]
|
plugins?: CarouselPlugin[]
|
||||||
orientation?: 'horizontal' | 'vertical'
|
orientation?: 'horizontal' | 'vertical'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CarouselEmits {
|
||||||
|
(e: 'init-api', payload: CarouselApi): void
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,12 @@ import emblaCarouselVue, {
|
||||||
type EmblaCarouselType as CarouselApi,
|
type EmblaCarouselType as CarouselApi,
|
||||||
} from 'embla-carousel-vue'
|
} from 'embla-carousel-vue'
|
||||||
import { onMounted, ref } from 'vue'
|
import { onMounted, ref } from 'vue'
|
||||||
import type { CarouselProps } from './interface'
|
import type { CarouselEmits, CarouselProps } from './interface'
|
||||||
|
|
||||||
const [useProvideCarousel, useInjectCarousel] = createInjectionState(
|
const [useProvideCarousel, useInjectCarousel] = createInjectionState(
|
||||||
({
|
({
|
||||||
opts, orientation, plugins,
|
opts, orientation, plugins,
|
||||||
}: CarouselProps) => {
|
}: CarouselProps, emits: CarouselEmits) => {
|
||||||
const [emblaNode, emblaApi] = emblaCarouselVue({
|
const [emblaNode, emblaApi] = emblaCarouselVue({
|
||||||
...opts,
|
...opts,
|
||||||
axis: orientation === 'horizontal' ? 'x' : 'y',
|
axis: orientation === 'horizontal' ? 'x' : 'y',
|
||||||
|
|
@ -36,6 +36,8 @@ const [useProvideCarousel, useInjectCarousel] = createInjectionState(
|
||||||
emblaApi.value?.on('init', onSelect)
|
emblaApi.value?.on('init', onSelect)
|
||||||
emblaApi.value?.on('reInit', onSelect)
|
emblaApi.value?.on('reInit', onSelect)
|
||||||
emblaApi.value?.on('select', onSelect)
|
emblaApi.value?.on('select', onSelect)
|
||||||
|
|
||||||
|
emits('init-api', emblaApi.value)
|
||||||
})
|
})
|
||||||
|
|
||||||
return { carouselRef: emblaNode, carouselApi: emblaApi, canScrollPrev, canScrollNext, scrollPrev, scrollNext, orientation }
|
return { carouselRef: emblaNode, carouselApi: emblaApi, canScrollPrev, canScrollNext, scrollPrev, scrollNext, orientation }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user