Skip to content

Commit 99dd025

Browse files
committed
feat: update Carousel drag options to support DragConfig for mouse and touch dragging
1 parent 0cbf77b commit 99dd025

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

docs/config.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
| `itemsToScroll` | `number` | 1 | Number of slides to move when navigating. Useful for creating slide groups. |
1818
| `itemsToShow` | `number` \| 'auto' | 1 | Number of slides visible simultaneously. Use 'auto' for variable width slides. |
1919
| `modelValue` | `number` | 0 | Controls the active slide index. Can be used with v-model for two-way binding. |
20-
| `mouseDrag` | `boolean` \| `object` | true | Enables/disables mouse drag navigation. When passed as an object, can contain a `threshold` property to control the drag distance required to trigger a slide transition. E.g. `{ threshold: 0.5 }`|
20+
| `mouseDrag` | `boolean` \| `DragConfig` | true | Enables/disables mouse drag navigation. See [Drag Options](#drag-options) for configuration details. |
2121
| `mouseScroll` | `boolean` | false | Enables/disables mouse wheel scrolling for carousel navigation. |
2222
| `mouseScrollThreshold` | `number` | 10 | Controls the sensitivity threshold for mouse scrolling. Higher values require more scrolling. |
2323
| `pauseAutoplayOnHover` | `boolean` | false | When true, autoplay pauses while the mouse cursor is over the carousel. |
2424
| `preventExcessiveDragging` | `boolean` | false | Limits dragging behavior at carousel boundaries for better UX. <Badge text="0.13.0" /> |
2525
| `slideEffect` | 'slide', 'fade' | 'slide' | Determines the transition effect between slides. |
2626
| `snapAlign` | 'start', 'end', 'center-odd', 'center-even' | 'center' | Determines how slides are aligned within the viewport. |
27-
| `touchDrag` | `boolean` | true | Enables/disables touch navigation on touch-enabled devices. |
27+
| `touchDrag` | `boolean` \| `DragConfig` | true | Enables/disables touch navigation on touch-enabled devices. See [Drag Options](#drag-options) for configuration details. |
2828
| `transition` | `number` | 300 | Duration of the slide transition animation in milliseconds. |
2929
| `wrapAround` | `boolean` | false | When true, creates an infinite loop effect by connecting the last slide to the first. |
3030

@@ -34,6 +34,27 @@
3434
3535
> **Drag Prevention**: The `preventExcessiveDragging` option is automatically disabled when `wrapAround` is enabled, as boundary restrictions aren't needed in infinite loop mode.
3636
37+
## Drag Options
38+
39+
Both `mouseDrag` and `touchDrag` properties accept either a boolean value or a `DragConfig` object with the following properties:
40+
41+
| Property | Type | Default | Description |
42+
|-------------|----------|---------|--------------------------------------------------------------------------------------------|
43+
| `threshold` | `number` | 0.3 | Controls the drag distance required to trigger a slide transition, as a fraction of slide width. Higher values require more dragging to trigger a slide change. |
44+
45+
### Example
46+
47+
```vue
48+
<template>
49+
<Carousel
50+
:mouseDrag="{ threshold: 0.5 }"
51+
:touchDrag="{ threshold: 0.7 }"
52+
>
53+
<!-- Slides -->
54+
</Carousel>
55+
</template>
56+
```
57+
3758
## Slots
3859

3960
### Slides/Default

src/components/Carousel/Carousel.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,11 @@ export const Carousel = defineComponent({
416416
dragged: { x: deltaX, y: deltaY },
417417
effectiveSlideSize: effectiveSlideSize.value,
418418
threshold:
419-
typeof config.mouseDrag === 'object' ? (config.mouseDrag?.threshold ?? 0) : 0,
419+
typeof config.mouseDrag === 'object'
420+
? (config.mouseDrag?.threshold ?? 0.3)
421+
: typeof config.touchDrag === 'object'
422+
? (config.touchDrag?.threshold ?? 0.3)
423+
: 0.3,
420424
})
421425

422426
activeSlideIndex.value = config.wrapAround

src/components/Carousel/carouselProps.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
NormalizedDir,
1818
SlideEffect,
1919
SnapAlign,
20+
DragConfig,
2021
} from '@/shared'
2122

2223
export const carouselProps = {
@@ -105,7 +106,7 @@ export const carouselProps = {
105106
// toggle mouse dragging
106107
mouseDrag: {
107108
default: DEFAULT_CONFIG.mouseDrag,
108-
type: [Boolean, Object] as PropType<boolean | { threshold?: number }>,
109+
type: [Boolean, Object] as PropType<boolean | DragConfig>,
109110
},
110111
// toggle mouse wheel scrolling
111112
mouseScroll: {
@@ -150,7 +151,7 @@ export const carouselProps = {
150151
// toggle touch dragging
151152
touchDrag: {
152153
default: DEFAULT_CONFIG.touchDrag,
153-
type: Boolean,
154+
type: [Boolean, Object] as PropType<boolean | DragConfig>,
154155
},
155156
// sliding transition time in ms
156157
transition: {

src/shared/types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export type SlideEffect = (typeof SLIDE_EFFECTS)[number]
2828

2929
export type SnapAlign = (typeof SNAP_ALIGN_OPTIONS)[number]
3030

31+
export type DragConfig = {
32+
threshold?: number
33+
}
34+
3135
export type CarouselConfig = {
3236
autoplay?: number
3337
breakpointMode?: BreakpointMode
@@ -42,14 +46,14 @@ export type CarouselConfig = {
4246
itemsToScroll: number
4347
itemsToShow: number | 'auto'
4448
modelValue?: number
45-
mouseDrag?: boolean | { threshold?: number }
49+
mouseDrag?: boolean | DragConfig
4650
mouseScroll?: boolean
4751
mouseScrollThreshold?: number
4852
pauseAutoplayOnHover?: boolean
4953
preventExcessiveDragging: boolean
5054
slideEffect: SlideEffect
5155
snapAlign: SnapAlign
52-
touchDrag?: boolean
56+
touchDrag?: boolean | DragConfig
5357
transition?: number
5458
wrapAround?: boolean
5559
}

0 commit comments

Comments
 (0)