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

fix(carousel): dynamically recalculate slots in render function #430

Merged
merged 1 commit into from
Nov 27, 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
18 changes: 9 additions & 9 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, CarouselExposed, CarouselNav } from '@/types'
import { CarouselConfig, CarouselData, CarouselExposed, CarouselNav } from '@/types'
import {
debounce,
throttle,
Expand Down Expand Up @@ -417,15 +417,15 @@ export default defineComponent({
// Init carousel
emit('before-init')

const data = {
const data = reactive<CarouselData>({
config,
slidesCount,
slideSize,
currentSlide: currentSlideIndex,
maxSlide: maxSlideIndex,
minSlide: minSlideIndex,
middleSlide: middleSlideIndex,
}
})

expose<CarouselExposed>({
updateBreakpointsConfig,
Expand Down Expand Up @@ -468,11 +468,10 @@ export default defineComponent({
return `translate${translateAxis}(${dragOffset - totalOffset}px)`
})

const slotSlides = slots.default || slots.slides
const slotAddons = slots.addons
const slotsProps = reactive(data)

return () => {
const slotSlides = slots.default || slots.slides
const slotAddons = slots.addons

if (!config.enabled) {
return h(
'section',
Expand All @@ -484,8 +483,9 @@ export default defineComponent({
)
}

const slidesElements = getSlidesVNodes(slotSlides?.(slotsProps))
const addonsElements = slotAddons?.(slotsProps) || []
const slidesElements = getSlidesVNodes(slotSlides?.(data))
const addonsElements = slotAddons?.(data) || []

slidesElements.forEach((el: typeof SlideComponent, index: number) => {
if (el.props) {
el.props.index = index
Expand Down
4 changes: 2 additions & 2 deletions src/types/carousel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ref } from 'vue'
import { Reactive, Ref } from 'vue'

import {
BREAKPOINT_MODE_OPTIONS,
Expand Down Expand Up @@ -60,5 +60,5 @@ export interface CarouselMethods extends CarouselNav {
}
export interface CarouselExposed extends CarouselMethods {
nav: CarouselNav
data: CarouselData
data: Reactive<CarouselData>
}