Skip to content

Commit

Permalink
Merge pull request #429 from ismail9k/392-typescript-support
Browse files Browse the repository at this point in the history
Better support for typescript and add CarouselExposed type
  • Loading branch information
ismail9k authored Nov 26, 2024
2 parents 6d009d2 + 1f7dd06 commit 8b94607
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
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
}

0 comments on commit 8b94607

Please sign in to comment.