feat: expose api for the parent component

This commit is contained in:
wasimTQ 2023-12-30 15:35:28 +05:30
parent 9f31e86f31
commit f3d4992b8f
3 changed files with 26 additions and 0 deletions

View File

@ -175,10 +175,32 @@ You can pass options to the carousel using the `opts` prop. See the [Embla Carou
## API
### Method 1
Use the `@init-api` emit method on `<Carousel />` component to set the instance of the API.
<ComponentPreview name="CarouselApi" />
### Method 2
You can access it through setting a template ref on the `<Carousel />` component.
```vue showLineNumbers {2,5,9}
<script setup>
const carouselContainerRef = ref<InstanceType<typeof Carousel> | null>(null)
function accessApi() {
carouselContainerRef.value?.carouselApi.on('select', () => {})
}
</script>
<template>
<Carousel ref="carouselContainerRef">
...
</Carousel>
</template>
```
## Events
You can listen to events using the API. To get the API instance use the `@init-api` emit method on the `<Carousel />` component

View File

@ -11,6 +11,8 @@ const emits = defineEmits<CarouselEmits>()
const carouselArgs = useProvideCarousel(props, emits)
defineExpose(carouselArgs)
function onKeyDown(event: KeyboardEvent) {
const prevKey = props.orientation === 'vertical' ? 'ArrowUp' : 'ArrowLeft'
const nextKey = props.orientation === 'vertical' ? 'ArrowDown' : 'ArrowRight'

View File

@ -11,6 +11,8 @@ const emits = defineEmits<CarouselEmits>()
const carouselArgs = useProvideCarousel(props, emits)
defineExpose(carouselArgs)
function onKeyDown(event: KeyboardEvent) {
const prevKey = props.orientation === 'vertical' ? 'ArrowUp' : 'ArrowLeft'
const nextKey = props.orientation === 'vertical' ? 'ArrowDown' : 'ArrowRight'