Skip to content

Commit 83b3462

Browse files
committed
feat: add preventExcessiveDragging option to limit boundary slide gestures
refs #307
1 parent 42358c9 commit 83b3462

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
lines changed

docs/config.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@
22

33
## Available Props
44

5-
| Prop | Default | Description |
6-
| ---------------------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
7-
| `enabled` | true | Controlled weather the carousel is enabled or disabled. <Badge text="0.8.0"/> |
8-
| `itemsToShow` | 1 | Count of items to showed per view (can be a fraction). Must be between 1 and the total number of slides. If set to a value less than 1, it defaults to 1. If set to a value greater than the total number of slides, it defaults to the total number of slides. |
9-
| `itemsToScroll` | 1 | Number of slides to be scrolled |
10-
| `wrapAround` | false | Enable infinite scrolling mode. |
11-
| `snapAlign` | 'center' | Controls the carousel position alignment, can be 'start', 'end', 'center-[odd\|even]' |
12-
| `transition` | 300 | Sliding transition time in ms. |
13-
| `autoplay` | 0 | Auto play time in ms. |
14-
| `breakpointMode` | 'viewport' | Determines how the carousel breakpoints are calculated. acceptable values: 'viewport', 'carousel' <Badge text="0.5.0"/> |
15-
| `breakpoints` | null | An object to pass all the breakpoints settings. |
16-
| `modelValue` | 0 | Index number of the initial slide. |
17-
| `mouseDrag` | true | Toggle mouse dragging |
18-
| `touchDrag` | true | Toggle pointer touch dragging |
19-
| `pauseAutoplayOnHover` | false | Toggle if auto play should pause on mouse hover |
20-
| `dir` | 'ltr' | Controls the carousel direction. Available values: 'ltr', 'rtl', 'ttb', 'btt' or use verbose 'left-to-right', 'right-to-left', 'top-to-bottom', 'bottom-to-top' <Badge text="0.7.0"/> |
21-
| `i18n` | [`{ ariaNextSlide: ...}`](#i18n) | Used to translate and/or change aria labels and additional texts used in the carousel. <Badge text="0.3.1"/> |
22-
| `gap` | 0 | Used to add gap between the slides. <Badge text="0.6.0"/> |
23-
| `height` | 'auto' | Carousel track height. <Badge text="0.7.0"/> |
24-
| `ignoreAnimations` | false | List of animation names to ignore for size calculations. Can be a boolean, string, or array of strings. <Badge text="0.10.0"/> |
5+
| Prop | Default | Description |
6+
|----------------------------|----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
7+
| `enabled` | true | Controlled weather the carousel is enabled or disabled. <Badge text="0.8.0"/> |
8+
| `itemsToShow` | 1 | Count of items to showed per view (can be a fraction). Must be between 1 and the total number of slides. If set to a value less than 1, it defaults to 1. If set to a value greater than the total number of slides, it defaults to the total number of slides. |
9+
| `itemsToScroll` | 1 | Number of slides to be scrolled |
10+
| `wrapAround` | false | Enable infinite scrolling mode. |
11+
| `snapAlign` | 'center' | Controls the carousel position alignment, can be 'start', 'end', 'center-[odd\|even]' |
12+
| `transition` | 300 | Sliding transition time in ms. |
13+
| `autoplay` | 0 | Auto play time in ms. |
14+
| `breakpointMode` | 'viewport' | Determines how the carousel breakpoints are calculated. acceptable values: 'viewport', 'carousel' <Badge text="0.5.0"/> |
15+
| `breakpoints` | null | An object to pass all the breakpoints settings. |
16+
| `modelValue` | 0 | Index number of the initial slide. |
17+
| `mouseDrag` | true | Toggle mouse dragging |
18+
| `touchDrag` | true | Toggle pointer touch dragging |
19+
| `pauseAutoplayOnHover` | false | Toggle if auto play should pause on mouse hover |
20+
| `dir` | 'ltr' | Controls the carousel direction. Available values: 'ltr', 'rtl', 'ttb', 'btt' or use verbose 'left-to-right', 'right-to-left', 'top-to-bottom', 'bottom-to-top' <Badge text="0.7.0"/> |
21+
| `i18n` | [`{ ariaNextSlide: ...}`](#i18n) | Used to translate and/or change aria labels and additional texts used in the carousel. <Badge text="0.3.1"/> |
22+
| `gap` | 0 | Used to add gap between the slides. <Badge text="0.6.0"/> |
23+
| `height` | 'auto' | Carousel track height. <Badge text="0.7.0"/> |
24+
| `ignoreAnimations` | false | List of animation names to ignore for size calculations. Can be a boolean, string, or array of strings. <Badge text="0.10.0"/> |
25+
| `preventExcessiveDragging` | false | Prevents unwanted dragging behavior when the carousel reaches its first or last slide. |
26+
2527

2628

2729
## Slots

src/components/Carousel/Carousel.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export const Carousel = defineComponent({
374374

375375
// Initialize start positions for the drag
376376
startPosition.x = 'touches' in event ? event.touches[0].clientX : event.clientX
377-
startPosition.y = 'touches' in event ? event.touches[0].clientY : event.clientY
377+
startPosition.y = 'touches' in event ? event.touches[0].clientY : event.clientYg
378378

379379
// Attach event listeners for dragging and drag end
380380

@@ -391,9 +391,21 @@ export const Carousel = defineComponent({
391391
const currentX = 'touches' in event ? event.touches[0].clientX : event.clientX
392392
const currentY = 'touches' in event ? event.touches[0].clientY : event.clientY
393393

394+
const tmpDraggedX = currentX - startPosition.x
395+
const tmpDraggedY = currentY - startPosition.y
396+
397+
if (!config.wrapAround && config.preventExcessiveDragging) {
398+
const isAtMinIndex = activeSlideIndex.value === minSlideIndex.value;
399+
const isAtMaxIndex = activeSlideIndex.value === maxSlideIndex.value;
400+
401+
if ((Math.abs(tmpDraggedX) > Math.abs(tmpDraggedY) ?
402+
(tmpDraggedX > 0 && isAtMinIndex) || (tmpDraggedX < 0 && isAtMaxIndex) :
403+
(tmpDraggedY > 0 && isAtMinIndex) || (tmpDraggedY < 0 && isAtMaxIndex))) return;
404+
}
405+
394406
// Calculate deltas for X and Y axes
395-
dragged.x = currentX - startPosition.x
396-
dragged.y = currentY - startPosition.y
407+
dragged.x = tmpDraggedX
408+
dragged.y = tmpDraggedY
397409

398410
const draggedSlides = getDraggedSlidesCount({
399411
isVertical: isVertical.value,

src/components/Carousel/carouselProps.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,8 @@ export const carouselProps = {
121121
return SLIDE_EFFECTS.includes(value)
122122
},
123123
},
124+
preventExcessiveDragging: {
125+
default: false,
126+
type: Boolean
127+
}
124128
}

src/shared/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ export const DEFAULT_CONFIG: CarouselConfig = {
6161
i18n: I18N_DEFAULT_CONFIG,
6262
ignoreAnimations: false,
6363
slideEffect: SLIDE_EFFECTS[0],
64+
preventExcessiveDragging: false
6465
}

src/shared/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface CarouselConfig {
4747
i18n: { [key in I18nKeys]?: string }
4848
ignoreAnimations: boolean | string[] | string
4949
slideEffect: SlideEffect
50+
preventExcessiveDragging: boolean
5051
}
5152

5253
export type VueClass = string | Record<string, boolean> | VueClass[]

0 commit comments

Comments
 (0)