Skip to content

Commit 087a67b

Browse files
committed
refactor: reorganize imports and enhance Carousel component types for better clarity and maintainability
1 parent fd46581 commit 087a67b

File tree

5 files changed

+195
-196
lines changed

5 files changed

+195
-196
lines changed

src/components/Carousel/Carousel.css

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
}
55

66
.carousel {
7+
height: var(--vc-carousel-height);
8+
overscroll-behavior: none;
79
position: relative;
810
touch-action: pan-y;
9-
overscroll-behavior: none;
1011
z-index: 1;
11-
height: var(--vc-carousel-height);
1212
}
1313

1414
.carousel.is-dragging {
@@ -17,78 +17,68 @@
1717

1818
.carousel__track {
1919
display: flex;
20+
gap: var(--vc-slide-gap);
21+
height: 100%;
2022
list-style: none;
21-
padding: 0 !important;
2223
margin: 0 !important;
23-
width: 100%;
24-
height: 100%;
24+
padding: 0 !important;
2525
position: relative;
2626
transition: transform ease-out;
2727
transition-duration: var(--vc-transition-duration);
28-
gap: var(--vc-slide-gap);
28+
width: 100%;
2929
}
3030

3131
.carousel__viewport {
32+
height: 100%;
3233
overflow: hidden;
3334
width: 100%;
34-
height: 100%;
3535
}
3636

3737
.carousel__sr-only {
38-
position: absolute;
39-
width: 1px;
38+
border: 0;
39+
clip: rect(0, 0, 0, 0);
4040
height: 1px;
41-
padding: 0;
4241
margin: -1px;
4342
overflow: hidden;
44-
clip: rect(0, 0, 0, 0);
45-
border: 0;
43+
padding: 0;
44+
position: absolute;
45+
width: 1px;
4646
}
4747

48-
.carousel.is-ttb {
49-
.carousel__track {
50-
flex-direction: column;
51-
}
48+
.carousel.is-ttb .carousel__track {
49+
flex-direction: column;
5250
}
5351

54-
.carousel.is-btt {
55-
.carousel__track {
56-
flex-direction: column-reverse;
57-
}
52+
.carousel.is-btt .carousel__track {
53+
flex-direction: column-reverse;
5854
}
5955

60-
.carousel.is-vertical {
61-
.carousel__slide--clone:first-child {
62-
margin-block-start: var(--vc-cloned-offset);
63-
}
56+
.carousel.is-vertical .carousel__slide--clone:first-child {
57+
margin-block-start: var(--vc-cloned-offset);
6458
}
6559

66-
.carousel:not(.is-vertical) {
67-
.carousel__slide--clone:first-child {
68-
margin-inline-start: var(--vc-cloned-offset);
69-
}
60+
.carousel:not(.is-vertical) .carousel__slide--clone:first-child {
61+
margin-inline-start: var(--vc-cloned-offset);
7062
}
7163

72-
.carousel.is-effect-fade {
73-
.carousel__track {
74-
transition: none;
75-
display: grid;
76-
grid-template-columns: 100%;
77-
grid-template-rows: 100%;
78-
}
64+
.carousel.is-effect-fade .carousel__track {
65+
display: grid;
66+
grid-template-columns: 100%;
67+
grid-template-rows: 100%;
68+
transition: none;
69+
}
7970

80-
.carousel__slide {
81-
opacity: 0;
82-
width: 100% !important;
83-
height: 100% !important;
84-
transition: opacity ease-in-out;
85-
transition-duration: var(--vc-transition-duration);
86-
grid-area: 1 / 1; /* Make all slides occupy the same grid cell */
87-
pointer-events: none; /* Prevent inactive slides from being clickable */
88-
}
71+
.carousel.is-effect-fade .carousel__slide {
72+
grid-area: 1 / 1;
73+
height: 100% !important;
74+
opacity: 0;
75+
pointer-events: none;
76+
transition: opacity ease-in-out;
77+
transition-duration: var(--vc-transition-duration);
78+
width: 100% !important;
79+
}
8980

90-
.carousel__slide--active {
91-
opacity: 1;
92-
pointer-events: auto; /* Re-enable pointer events for active slide */
93-
}
81+
.carousel.is-effect-fade .carousel__slide--active {
82+
opacity: 1;
83+
pointer-events: auto;
9484
}

src/components/Carousel/Carousel.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mount } from '@vue/test-utils'
2-
import { expect, it, describe, beforeEach } from 'vitest'
2+
import { beforeEach, describe, expect, it } from 'vitest'
33

44
import { Carousel } from './Carousel'
55

src/components/Carousel/Carousel.ts

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
import {
2+
computed,
3+
ComputedRef,
4+
Ref,
5+
SetupContext,
26
defineComponent,
3-
onMounted,
7+
h,
48
onBeforeUnmount,
5-
ref,
6-
reactive,
9+
onMounted,
710
provide,
8-
computed,
9-
h,
11+
reactive,
12+
ref,
13+
shallowReactive,
1014
watch,
11-
SetupContext,
12-
Ref,
13-
ComputedRef,
1415
watchEffect,
15-
shallowReactive,
1616
} from 'vue'
1717

1818
import { ARIA as ARIAComponent } from '@/components/ARIA'
1919
import {
2020
CarouselConfig,
21+
createSlideRegistry,
2122
DEFAULT_CONFIG,
2223
DIR_MAP,
24+
injectCarousel,
2325
NonNormalizedDir,
2426
NormalizedDir,
25-
injectCarousel,
26-
createSlideRegistry,
2727
} from '@/shared'
2828
import {
29-
except,
30-
throttle,
31-
getNumberInRange,
32-
mapNumberToRange,
29+
calculateAverage,
3330
createCloneSlides,
31+
except,
3432
getDraggedSlidesCount,
35-
getSnapAlignOffset,
33+
getNumberInRange,
3634
getScaleMultipliers,
35+
getSnapAlignOffset,
36+
mapNumberToRange,
3737
ScaleMultipliers,
38+
throttle,
3839
toCssValue,
39-
calculateAverage,
4040
} from '@/utils'
4141

4242
import {
@@ -52,15 +52,15 @@ export const Carousel = defineComponent({
5252
name: 'VueCarousel',
5353
props: carouselProps,
5454
emits: [
55-
'init',
55+
'before-init',
5656
'drag',
57-
'slide-start',
57+
'init',
5858
'loop',
59-
'update:modelValue',
6059
'slide-end',
61-
'before-init',
6260
'slide-registered',
61+
'slide-start',
6362
'slide-unregistered',
63+
'update:modelValue',
6464
],
6565
setup(props: CarouselConfig, { slots, emit, expose }: SetupContext) {
6666
const slideRegistry = createSlideRegistry(emit)
@@ -823,45 +823,45 @@ export const Carousel = defineComponent({
823823
const nav: CarouselNav = { slideTo, next, prev }
824824

825825
const provided: InjectedCarousel = reactive({
826+
activeSlide: activeSlideIndex,
826827
config,
827-
slidesCount,
828-
viewport,
829-
slides,
830828
currentSlide: currentSlideIndex,
831-
activeSlide: activeSlideIndex,
829+
isSliding,
830+
isVertical,
832831
maxSlide: maxSlideIndex,
833832
minSlide: minSlideIndex,
834-
visibleRange,
835-
slideSize,
836-
isVertical,
837-
normalizedDir,
838833
nav,
839-
isSliding,
834+
normalizedDir,
840835
slideRegistry,
836+
slideSize,
837+
slides,
838+
slidesCount,
839+
viewport,
840+
visibleRange,
841841
})
842842

843843
provide(injectCarousel, provided)
844844

845845
const data = reactive<CarouselData>({
846846
config,
847-
slidesCount,
848-
slideSize,
849847
currentSlide: currentSlideIndex,
850848
maxSlide: maxSlideIndex,
851-
minSlide: minSlideIndex,
852849
middleSlide: middleSlideIndex,
850+
minSlide: minSlideIndex,
851+
slideSize,
852+
slidesCount,
853853
})
854854

855855
expose<CarouselExposed>({
856-
updateBreakpointsConfig,
857-
updateSlidesData,
858-
updateSlideSize,
859-
restartCarousel,
860-
slideTo,
856+
data,
857+
nav,
861858
next,
862859
prev,
863-
nav,
864-
data,
860+
restartCarousel,
861+
slideTo,
862+
updateBreakpointsConfig,
863+
updateSlideSize,
864+
updateSlidesData,
865865
})
866866

867867
return () => {

src/components/Carousel/Carousel.types.ts

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,60 @@ import {
66
ShallowReactive,
77
} from 'vue'
88

9-
import { SlideRegistry, CarouselConfig, NormalizedDir } from '@/shared'
9+
import { CarouselConfig, NormalizedDir, SlideRegistry } from '@/shared'
1010

11-
export interface CarouselNav {
12-
slideTo: (index: number) => void
13-
next: (skipTransition?: boolean) => void
14-
prev: (skipTransition?: boolean) => void
11+
export type ElRect = {
12+
height: number
13+
width: number
1514
}
1615

17-
export type InjectedCarousel = Reactive<{
18-
config: CarouselConfig
19-
viewport: Ref<Element | null>
20-
slides: ShallowReactive<Array<ComponentInternalInstance>>
21-
slidesCount: ComputedRef<number>
22-
activeSlide: Ref<number>
23-
currentSlide: Ref<number>
24-
maxSlide: ComputedRef<number>
25-
minSlide: ComputedRef<number>
26-
visibleRange: ComputedRef<{ min: number; max: number }>
27-
slideSize: Ref<number>
28-
isVertical: ComputedRef<boolean>
29-
normalizedDir: ComputedRef<NormalizedDir>
30-
nav: CarouselNav
31-
isSliding: Ref<boolean>
32-
slideRegistry: SlideRegistry
33-
}>
16+
export type Range = {
17+
min: number
18+
max: number
19+
}
3420

35-
export interface CarouselData {
21+
export type CarouselData = {
3622
config: CarouselConfig
37-
slidesCount: Ref<number>
38-
slideSize: Ref<number>
3923
currentSlide: Ref<number>
4024
maxSlide: Ref<number>
41-
minSlide: Ref<number>
4225
middleSlide: Ref<number>
26+
minSlide: Ref<number>
27+
slideSize: Ref<number>
28+
slidesCount: Ref<number>
4329
}
4430

45-
export interface CarouselMethods extends CarouselNav {
31+
export type CarouselExposed = CarouselMethods & {
32+
data: Reactive<CarouselData>
33+
nav: CarouselNav
34+
}
35+
36+
export type CarouselMethods = CarouselNav & {
37+
restartCarousel: () => void
4638
updateBreakpointsConfig: () => void
47-
updateSlidesData: () => void
4839
updateSlideSize: () => void
49-
restartCarousel: () => void
40+
updateSlidesData: () => void
5041
}
51-
export interface CarouselExposed extends CarouselMethods {
52-
nav: CarouselNav
53-
data: Reactive<CarouselData>
42+
43+
export type CarouselNav = {
44+
next: (skipTransition?: boolean) => void
45+
prev: (skipTransition?: boolean) => void
46+
slideTo: (index: number) => void
5447
}
55-
export type ElRect = { width: number; height: number }
48+
49+
export type InjectedCarousel = Reactive<{
50+
activeSlide: Ref<number>
51+
config: CarouselConfig
52+
currentSlide: Ref<number>
53+
isSliding: Ref<boolean>
54+
isVertical: ComputedRef<boolean>
55+
maxSlide: ComputedRef<number>
56+
minSlide: ComputedRef<number>
57+
nav: CarouselNav
58+
normalizedDir: ComputedRef<NormalizedDir>
59+
slideRegistry: SlideRegistry
60+
slideSize: Ref<number>
61+
slides: ShallowReactive<Array<ComponentInternalInstance>>
62+
slidesCount: ComputedRef<number>
63+
viewport: Ref<Element | null>
64+
visibleRange: ComputedRef<Range>
65+
}>

0 commit comments

Comments
 (0)