|
| 1 | +import { useRouter } from 'next/router'; |
| 2 | +import { TouchEvent, useEffect, useRef, useState } from 'react'; |
| 3 | + |
| 4 | +const ease = (distance: number) => { |
| 5 | + return distance / 2; |
| 6 | +}; |
| 7 | + |
| 8 | +const PullRefresh = ({ children }: { children?: JSX.Element }) => { |
| 9 | + const [height, setHeight] = useState(0); |
| 10 | + const [isRefresh, setIsRefresh] = useState(false); |
| 11 | + const [startY, setStartY] = useState(0); |
| 12 | + const [shouldRefresh, setShouldRefresh] = useState(false); |
| 13 | + const el = useRef<HTMLDivElement>(null); |
| 14 | + const route = useRouter(); |
| 15 | + |
| 16 | + const handleTouchStart = (e: TouchEvent) => { |
| 17 | + const top = e.currentTarget.getBoundingClientRect().top; |
| 18 | + setStartY(e.touches[0].pageY - top); |
| 19 | + setHeight(0); |
| 20 | + setIsRefresh(false); |
| 21 | + setShouldRefresh(false); |
| 22 | + }; |
| 23 | + const handleTouchMove = (e: TouchEvent) => { |
| 24 | + const touch = e.touches[0]; |
| 25 | + const moveY = touch.pageY - startY; |
| 26 | + const top = e.currentTarget.getBoundingClientRect().top; |
| 27 | + |
| 28 | + if (top === 0 && moveY > 0) { |
| 29 | + document.documentElement.style.overflow = 'hidden'; |
| 30 | + document.body.style.overflow = 'hidden'; |
| 31 | + e.preventDefault(); |
| 32 | + setHeight(ease(moveY)); |
| 33 | + if (ease(moveY) > 80) { |
| 34 | + setShouldRefresh(true); |
| 35 | + } else { |
| 36 | + setShouldRefresh(false); |
| 37 | + } |
| 38 | + } |
| 39 | + }; |
| 40 | + const handleTouchEnd = () => { |
| 41 | + document.documentElement.style.overflow = 'initial'; |
| 42 | + document.body.style.overflow = 'initial'; |
| 43 | + if (shouldRefresh) { |
| 44 | + setHeight(50); |
| 45 | + setIsRefresh(true); |
| 46 | + route.reload(); |
| 47 | + } else { |
| 48 | + setHeight(0); |
| 49 | + } |
| 50 | + setShouldRefresh(false); |
| 51 | + }; |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + const elCurrent = el.current; |
| 55 | + elCurrent?.addEventListener('touchstart', handleTouchStart as any, { |
| 56 | + passive: false, |
| 57 | + }); |
| 58 | + elCurrent?.addEventListener('touchmove', handleTouchMove as any, { |
| 59 | + passive: false, |
| 60 | + }); |
| 61 | + elCurrent?.addEventListener('touchend', handleTouchEnd as any, { |
| 62 | + passive: false, |
| 63 | + }); |
| 64 | + elCurrent?.addEventListener('touchcancel', handleTouchEnd as any, { |
| 65 | + passive: false, |
| 66 | + }); |
| 67 | + |
| 68 | + return () => { |
| 69 | + elCurrent?.removeEventListener('touchstart', handleTouchStart as any); |
| 70 | + elCurrent?.removeEventListener('touchmove', handleTouchMove as any); |
| 71 | + elCurrent?.removeEventListener('touchend', handleTouchEnd as any); |
| 72 | + elCurrent?.removeEventListener('touchcancel', handleTouchEnd as any); |
| 73 | + }; |
| 74 | + }); |
| 75 | + |
| 76 | + return ( |
| 77 | + <div ref={el}> |
| 78 | + <div |
| 79 | + style={{ height }} |
| 80 | + className='flex items-center justify-center text-sm text-gray-800' |
| 81 | + > |
| 82 | + {shouldRefresh ? '松开刷新' : isRefresh ? '刷新中...' : '下拉刷新'} |
| 83 | + </div> |
| 84 | + {children} |
| 85 | + </div> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +export default PullRefresh; |
0 commit comments