Skip to content

Commit

Permalink
refactor: replace bounding rectangle handling with ElRect type for im…
Browse files Browse the repository at this point in the history
…proved clarity and consistency
  • Loading branch information
ismail9k committed Jan 3, 2025
1 parent 711a63f commit 37f556b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/components/Carousel/Carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
CarouselData,
CarouselExposed,
CarouselNav,
ElRect,
InjectedCarousel,
} from './Carousel.types'
import { carouselProps } from './carouselProps'
Expand Down Expand Up @@ -110,6 +111,8 @@ export const Carousel = defineComponent({
const isVertical = computed(() => ['ttb', 'btt'].includes(normalizedDir.value))
const isAuto = computed(() => config.itemsToShow === 'auto')

const dimension = computed(() => (isVertical.value ? 'height' : 'width'))

function updateBreakpointsConfig(): void {
if (!mounted.value) {
return
Expand Down Expand Up @@ -156,20 +159,20 @@ export const Carousel = defineComponent({
/**
* Setup functions
*/
const slidesBoundingRect = ref<Array<{ width: number; height: number }>>([])
const slidesRect = ref<Array<ElRect>>([])
function updateSlidesRectSize({
widthMultiplier,
heightMultiplier,
}: ScaleMultipliers): void {
slidesBoundingRect.value = slides.map((slide) => {
slidesRect.value = slides.map((slide) => {
const rect = slide.exposed?.getBoundingRect()
return {
width: rect.width * widthMultiplier,
height: rect.height * heightMultiplier,
}
})
}
const viewportBoundingRect: Ref<{ width: number; height: number }> = ref({
const viewportRect: Ref<ElRect> = ref({
width: 0,
height: 0,
})
Expand All @@ -178,7 +181,7 @@ export const Carousel = defineComponent({
heightMultiplier,
}: ScaleMultipliers): void {
const rect = viewport.value?.getBoundingClientRect() || { width: 0, height: 0 }
viewportBoundingRect.value = {
viewportRect.value = {
width: rect.width * widthMultiplier,
height: rect.height * heightMultiplier,
}
Expand All @@ -187,20 +190,19 @@ export const Carousel = defineComponent({
function updateSlideSize(): void {
if (!viewport.value) return

const dimension = isVertical.value ? 'height' : 'width'
const scaleMultipliers = getScaleMultipliers(transformElements)

updateViewportRectSize(scaleMultipliers)
updateSlidesRectSize(scaleMultipliers)

if (isAuto.value) {
slideSize.value = Math.max(
...slidesBoundingRect.value.map((slide) => slide[dimension])
...slidesRect.value.map((slide) => slide[dimension.value])
)

Check warning on line 201 in src/components/Carousel/Carousel.ts

View workflow job for this annotation

GitHub Actions / coverage-report

These lines are not covered by a test
} else {
const itemsToShow = Number(config.itemsToShow)
const totalGap = (itemsToShow - 1) * config.gap
slideSize.value = (viewportBoundingRect.value[dimension] - totalGap) / itemsToShow
slideSize.value = (viewportRect.value[dimension.value] - totalGap) / itemsToShow
}
}

Expand Down Expand Up @@ -677,13 +679,11 @@ export const Carousel = defineComponent({
})

const clonedSlidesOffset = computed(() => {
const dimension = isVertical.value ? 'height' : 'width'

if (isAuto.value) {
return (
slidesBoundingRect.value
slidesRect.value
.slice(-1 * clonedSlidesCount.value.before)
.reduce((acc, slide) => acc + slide[dimension] + config.gap, 0) * -1
.reduce((acc, slide) => acc + slide[dimension.value] + config.gap, 0) * -1

Check warning on line 686 in src/components/Carousel/Carousel.ts

View workflow job for this annotation

GitHub Actions / coverage-report

These lines are not covered by a test
)
}

Check warning on line 688 in src/components/Carousel/Carousel.ts

View workflow job for this annotation

GitHub Actions / coverage-report

This line is not covered by a test

Expand All @@ -692,37 +692,36 @@ export const Carousel = defineComponent({

const scrolledOffset = computed(() => {
let scrolledOffset = 0
const dimension = isVertical.value ? 'height' : 'width'

if (isAuto.value) {
const slideIndex =
((currentSlideIndex.value % slides.length) + slides.length) % slides.length
const snapAlignOffset = getSnapAlignOffset({
slideSize: slidesBoundingRect.value[slideIndex]?.[dimension],
viewportSize: viewportBoundingRect.value[dimension],
slideSize: slidesRect.value[slideIndex]?.[dimension.value],
viewportSize: viewportRect.value[dimension.value],
align: config.snapAlign,
})

Check warning on line 703 in src/components/Carousel/Carousel.ts

View workflow job for this annotation

GitHub Actions / coverage-report

These lines are not covered by a test

if (currentSlideIndex.value < 0) {
scrolledOffset =
slidesBoundingRect.value
slidesRect.value
.slice(currentSlideIndex.value)
.reduce((acc, slide) => acc + slide[dimension] + config.gap, 0) * -1
.reduce((acc, slide) => acc + slide[dimension.value] + config.gap, 0) * -1
} else {
scrolledOffset = slidesBoundingRect.value
scrolledOffset = slidesRect.value
.slice(0, currentSlideIndex.value)
.reduce((acc, slide) => acc + slide[dimension] + config.gap, 0)
.reduce((acc, slide) => acc + slide[dimension.value] + config.gap, 0)
}
scrolledOffset -= snapAlignOffset

Check warning on line 715 in src/components/Carousel/Carousel.ts

View workflow job for this annotation

GitHub Actions / coverage-report

These lines are not covered by a test

// remove whitespace
if (!config.wrapAround) {
const maxSlidingValue =
slidesBoundingRect.value.reduce(
(acc, slide) => acc + slide[dimension] + config.gap,
slidesRect.value.reduce(
(acc, slide) => acc + slide[dimension.value] + config.gap,
0
) -
viewportBoundingRect.value[dimension] -
viewportRect.value[dimension.value] -
config.gap

Check warning on line 725 in src/components/Carousel/Carousel.ts

View workflow job for this annotation

GitHub Actions / coverage-report

These lines are not covered by a test

scrolledOffset = getNumberInRange({
Expand Down
1 change: 1 addition & 0 deletions src/components/Carousel/Carousel.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ export interface CarouselExposed extends CarouselMethods {
nav: CarouselNav
data: Reactive<CarouselData>
}
export type ElRect = { width: number; height: number }

0 comments on commit 37f556b

Please sign in to comment.