Skip to content

Commit 1e05296

Browse files
committed
refactor: streamline Carousel and useDragging by consolidating isSliding handling and improving drag event logic
1 parent 2013db9 commit 1e05296

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/components/Carousel/Carousel.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,6 @@ export const Carousel = defineComponent({
401401
*/
402402
const isSliding = ref(false)
403403

404-
const { handleScroll } = useWheel({
405-
isVertical: isVertical.value,
406-
isReversed: isReversed.value,
407-
isSliding: isSliding.value,
408-
config,
409-
next,
410-
prev,
411-
})
412-
413404
const onDrag = ({
414405
delta,
415406
isTouch,
@@ -443,15 +434,24 @@ export const Carousel = defineComponent({
443434

444435
emit('drag', delta)
445436
}
446-
const onDragEnd = () => {
447-
slideTo(activeSlideIndex.value)
448-
}
437+
438+
const onDragEnd = () => slideTo(activeSlideIndex.value)
439+
449440
const { dragged, isDragging, handleDragStart } = useDragging({
450-
isSliding: isSliding.value,
441+
isSliding,
451442
onDrag,
452443
onDragEnd,
453444
})
454445

446+
const { handleScroll } = useWheel({
447+
isVertical,
448+
isReversed,
449+
isSliding,
450+
config,
451+
next,
452+
prev,
453+
})
454+
455455
function slideTo(slideIndex: number, skipTransition = false): void {
456456
if (!skipTransition && isSliding.value) {
457457
return

src/composables/useDragging.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
import { ref, reactive } from 'vue'
1+
import { ref, reactive, computed, Ref } from 'vue'
22

33
import { throttle } from '@/utils'
44

55
export interface UseDraggingOptions {
6+
isSliding: boolean | Ref<boolean>
67
onDrag?: (data: { delta: { x: number; y: number }; isTouch: boolean }) => void
78
onDragStart?: () => void
89
onDragEnd?: () => void
9-
isSliding?: boolean
1010
}
1111

12-
export function useDragging(options: UseDraggingOptions = {}) {
12+
export function useDragging(options: UseDraggingOptions) {
1313
let isTouch = false
1414
const startPosition = { x: 0, y: 0 }
1515
const dragged = reactive({ x: 0, y: 0 })
1616
const isDragging = ref(false)
1717

18+
const { isSliding } = options
19+
20+
const sliding = computed(() => {
21+
return typeof isSliding === 'boolean' ? isSliding : isSliding.value
22+
})
23+
1824
const handleDragStart = (event: MouseEvent | TouchEvent): void => {
1925
// Prevent drag initiation on input elements or if already sliding
2026
const targetTagName = (event.target as HTMLElement).tagName
21-
if (['INPUT', 'TEXTAREA', 'SELECT'].includes(targetTagName) || options.isSliding) {
27+
if (['INPUT', 'TEXTAREA', 'SELECT'].includes(targetTagName) || sliding.value) {
2228
return
2329
}
2430

0 commit comments

Comments
 (0)