|
| 1 | +import { useCallback, useRef } from 'react'; |
| 2 | + |
| 3 | +import { ICssAnimationOptions } from '@/components/cssAnimation'; |
| 4 | +import { convertDurationToNumber } from '@/utils/stringUtility/string.utility'; |
| 5 | + |
| 6 | +const distanceToTriggerClose = 30; |
| 7 | + |
| 8 | +type ReturnType = { |
| 9 | + setPopoverRef?: (node: HTMLElement) => void; |
| 10 | + setDragIconRef?: (node: HTMLElement) => void; |
| 11 | +}; |
| 12 | + |
| 13 | +export const useSwipeDown = ( |
| 14 | + animationOptions?: ICssAnimationOptions, |
| 15 | + handleClose?: () => void |
| 16 | +): ReturnType => { |
| 17 | + const containerRef = useRef<HTMLDivElement | null>(null); |
| 18 | + const dragRef = useRef<HTMLDivElement | null>(null); |
| 19 | + |
| 20 | + const setPopoverRef = useCallback(node => { |
| 21 | + if (node) { |
| 22 | + containerRef.current = node; |
| 23 | + } else { |
| 24 | + containerRef.current = null; |
| 25 | + } |
| 26 | + }, []); |
| 27 | + |
| 28 | + const setDragIconRef = useCallback(node => { |
| 29 | + if (node) { |
| 30 | + dragRef.current = node; |
| 31 | + dragRef?.current?.addEventListener('mousedown', startMove); |
| 32 | + dragRef?.current?.addEventListener('mousemove', currentMove); |
| 33 | + dragRef?.current?.addEventListener('mouseup', endMove); |
| 34 | + |
| 35 | + dragRef?.current?.addEventListener('touchstart', startMove); |
| 36 | + dragRef?.current?.addEventListener('touchmove', currentMove); |
| 37 | + dragRef?.current?.addEventListener('touchend', endMove); |
| 38 | + } else { |
| 39 | + dragRef?.current?.removeEventListener('mousedown', startMove); |
| 40 | + dragRef?.current?.removeEventListener('mousemove', currentMove); |
| 41 | + dragRef?.current?.removeEventListener('mouseup', endMove); |
| 42 | + |
| 43 | + dragRef?.current?.removeEventListener('touchstart', startMove); |
| 44 | + dragRef?.current?.removeEventListener('touchmove', currentMove); |
| 45 | + dragRef?.current?.removeEventListener('touchend', endMove); |
| 46 | + dragRef.current = null; |
| 47 | + } |
| 48 | + }, []); |
| 49 | + |
| 50 | + const animationExitDuration = |
| 51 | + ((convertDurationToNumber(animationOptions?.exitDuration) || |
| 52 | + convertDurationToNumber(animationOptions?.duration) || |
| 53 | + 0) + |
| 54 | + (convertDurationToNumber(animationOptions?.delay) || 0)) * |
| 55 | + 1000; |
| 56 | + |
| 57 | + const currentBottom = useRef(0); |
| 58 | + const yStart = useRef(0); |
| 59 | + const yEnd = useRef<number | null>(0); |
| 60 | + const dragMove = useRef(false); |
| 61 | + |
| 62 | + const startMove = e => { |
| 63 | + const swiperContent = containerRef?.current; |
| 64 | + if (!swiperContent) { |
| 65 | + return; |
| 66 | + } |
| 67 | + e.preventDefault?.(); |
| 68 | + swiperContent.style.removeProperty('transition'); |
| 69 | + |
| 70 | + if (e.type === 'touchstart') { |
| 71 | + yStart.current = e.touches[0].clientY; |
| 72 | + } else { |
| 73 | + yStart.current = e.clientY; |
| 74 | + } |
| 75 | + dragMove.current = true; |
| 76 | + yEnd.current = null; |
| 77 | + }; |
| 78 | + |
| 79 | + const currentMove = e => { |
| 80 | + const swiperContent = containerRef?.current; |
| 81 | + if (!dragMove.current || !swiperContent) { |
| 82 | + return; |
| 83 | + } |
| 84 | + if (e.type === 'touchmove' && yEnd !== null) { |
| 85 | + yEnd.current = e.touches[0].clientY as number; |
| 86 | + } else { |
| 87 | + yEnd.current = e.clientY as number; |
| 88 | + } |
| 89 | + const currentMove = yStart.current - yEnd.current; |
| 90 | + if (yEnd.current < yStart.current) { |
| 91 | + return; |
| 92 | + } |
| 93 | + swiperContent.style.bottom = `${currentBottom.current + currentMove}px`; |
| 94 | + }; |
| 95 | + |
| 96 | + const endMove = () => { |
| 97 | + const swiperContent = containerRef?.current; |
| 98 | + if (!swiperContent || !yEnd.current) { |
| 99 | + return; |
| 100 | + } |
| 101 | + dragMove.current = false; |
| 102 | + swiperContent.style.setProperty('transition', `bottom ${animationExitDuration}ms linear`); |
| 103 | + |
| 104 | + if (yEnd.current < yStart.current + distanceToTriggerClose) { |
| 105 | + swiperContent.style.bottom = '0px'; |
| 106 | + return; |
| 107 | + } |
| 108 | + // Move modal |
| 109 | + const distance = currentBottom.current + swiperContent.scrollHeight; |
| 110 | + swiperContent.style.bottom = `-${distance}px`; |
| 111 | + setTimeout(() => { |
| 112 | + handleClose?.(); |
| 113 | + }, animationExitDuration); |
| 114 | + }; |
| 115 | + |
| 116 | + return { setPopoverRef, setDragIconRef }; |
| 117 | +}; |
0 commit comments