Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better support for typescript and add CarouselExposed type #429

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/components/Carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {

import { DEFAULT_CONFIG } from '@/partials/defaults'
import { carouselProps } from '@/partials/props'
import { CarouselConfig, CarouselNav } from '@/types'
import { CarouselConfig, CarouselExposed, CarouselNav } from '@/types'
import {
debounce,
throttle,
Expand All @@ -36,6 +36,15 @@ import SlideComponent from './Slide'
export default defineComponent({
name: 'Carousel',
props: carouselProps,
emits: [
'init',
'drag',
'slide-start',
'loop',
'update:modelValue',
'slide-end',
'before-init',
],
setup(props: CarouselConfig, { slots, emit, expose }: SetupContext) {
const root: Ref<Element | null> = ref(null)
const viewport: Ref<Element | null> = ref(null)
Expand Down Expand Up @@ -412,15 +421,13 @@ export default defineComponent({
config,
slidesCount,
slideSize,
next,
prev,
slideTo,
currentSlide: currentSlideIndex,
maxSlide: maxSlideIndex,
minSlide: minSlideIndex,
middleSlide: middleSlideIndex,
}
expose({

expose<CarouselExposed>({
updateBreakpointsConfig,
updateSlidesData,
updateSlideSize,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Navigation = (props: any, { slots, attrs }: any) => {
const minSlide = inject('minSlide', ref(1))
const normalizeDir = inject('normalizeDir', ref('ltr'))
const currentSlide = inject('currentSlide', ref(1))
const nav: CarouselNav = inject('nav', {})
const nav: CarouselNav = inject('nav', {} as CarouselNav)

const { wrapAround, i18n } = config
const getPrevIcon = (): string => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Pagination = () => {
const maxSlide = inject('maxSlide', ref(1))
const minSlide = inject('minSlide', ref(1))
const currentSlide = inject('currentSlide', ref(1))
const nav: CarouselNav = inject('nav', {})
const nav: CarouselNav = inject('nav', {} as CarouselNav)

const isActive = (slide: number): boolean =>
mapNumberToRange({
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import Navigation from './components/Navigation'
import Pagination from './components/Pagination'
import Slide from './components/Slide'

export * from '@/types'
export type * from '@/types'
export { Carousel, Slide, Navigation, Pagination, Icon }
27 changes: 24 additions & 3 deletions src/types/carousel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Ref } from 'vue'

import {
BREAKPOINT_MODE_OPTIONS,
DIR_OPTIONS,
Expand Down Expand Up @@ -35,9 +37,28 @@ export interface CarouselConfig {
}

export interface CarouselNav {
[key: string]: any
slideTo: (index: number) => void
next: () => void
prev: () => void
}

export interface CarouselData {
config: CarouselConfig
slidesCount: Ref<number>
slideSize: Ref<number>
currentSlide: Ref<number>
maxSlide: Ref<number>
minSlide: Ref<number>
middleSlide: Ref<number>
}

export interface ElementStyleObject {
[key: string]: any
export interface CarouselMethods extends CarouselNav {
updateBreakpointsConfig: () => void
updateSlidesData: () => void
updateSlideSize: () => void
restartCarousel: () => void
}
export interface CarouselExposed extends CarouselMethods {
nav: CarouselNav
data: CarouselData
}