Skip to content

Commit

Permalink
Refactor carousel slide cloning logic into a separate utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
ismail9k committed Dec 9, 2024
1 parent 29886b8 commit 700b2dd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
36 changes: 6 additions & 30 deletions src/components/Carousel/Carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
computed,
h,
watch,
cloneVNode,
VNode,
SetupContext,
Ref,
Expand All @@ -21,7 +20,6 @@ import {
} from 'vue'

import { ARIA as ARIAComponent } from '@/components/ARIA'
import { Slide } from '@/components/Slide'
import { injectCarousel } from '@/injectSymbols'
import {
CarouselConfig,
Expand All @@ -38,6 +36,7 @@ import {
mapNumberToRange,
getScrolledIndex,
getTransformValues,
createCloneSlides,
} from '@/utils'

import {
Expand Down Expand Up @@ -678,35 +677,12 @@ export const Carousel = defineComponent({
const addonsElements = slotAddons?.(data) || []

if (config.wrapAround) {
// Ensure scoped css tracks properly
pushScopeId(output.length > 0 ? output[0].scopeId : null)
// Ensure scoped CSS tracks properly
const scopeId = output.length > 0 ? output[0].scopeId : null
pushScopeId(scopeId)
const toShow = clonedSlidesCount.value
const slidesBefore = []
for (let i = -toShow; i < 0; i++) {
const props = {
index: i,
isClone: true,
key: `clone-before-${i}`,
}
slidesBefore.push(
slides.length > 0
? cloneVNode(slides[(i + slides.length) % slides.length].vnode, props)
: h(Slide, props)
)
}
const slidesAfter = []
for (let i = 0; i < toShow; i++) {
const props = {
index: slides.length > 0 ? i + slides.length : i + 99999,
isClone: true,
key: `clone-after-${i}`,
}
slidesAfter.push(
slides.length > 0
? cloneVNode(slides[i % slides.length].vnode, props)
: h(Slide, props)
)
}
const slidesBefore = createCloneSlides({ slides, position: 'before', toShow })
const slidesAfter = createCloneSlides({ slides, position: 'after', toShow })
popScopeId()
output = [...slidesBefore, ...output, ...slidesAfter]
}
Expand Down
32 changes: 32 additions & 0 deletions src/utils/createCloneSlides.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { cloneVNode, ComponentInternalInstance, h } from 'vue'

import { Slide } from '@/components/Slide'

type CreateCloneSlidesArgs = {
slides: Array<ComponentInternalInstance>
position: 'before' | 'after'
toShow: number
}

export function createCloneSlides({ slides, position, toShow }: CreateCloneSlidesArgs) {
const clones = []
const isBefore = position === 'before'
const start = isBefore ? -toShow : 0
const end = isBefore ? 0 : toShow

for (let i = start; i < end; i++) {
const index = isBefore ? i : slides.length > 0 ? i + slides.length : i + 99999
const props = {
index,
isClone: true,
key: `clone-${position}-${i}`,
}
clones.push(
slides.length > 0
? cloneVNode(slides[(i + slides.length) % slides.length].vnode, props)
: h(Slide, props)
)
}

return clones
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './mapNumberToRange'
export * from './i18nFormatter'
export * from './throttle'
export * from './getTransformValues'
export * from './createCloneSlides'

0 comments on commit 700b2dd

Please sign in to comment.