-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swipe left/right to seek through subtitles on mobile
- Loading branch information
1 parent
361ce95
commit e7c6872
Showing
3 changed files
with
119 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useEffect, useState, useCallback } from 'react'; | ||
|
||
export type Direction = 'left' | 'right'; | ||
|
||
interface Touch { | ||
x: number; | ||
timestamp: number; | ||
} | ||
|
||
export const useSwipe = ({ | ||
onSwipe, | ||
rect, | ||
distance, | ||
ms, | ||
}: { | ||
onSwipe: (direction: Direction) => void; | ||
rect?: DOMRect; | ||
distance: number; | ||
ms: number; | ||
}) => { | ||
const [start, setStart] = useState<Touch>(); | ||
const insideRect = useCallback( | ||
(x: number, y: number) => { | ||
if (rect === undefined) { | ||
return true; | ||
} | ||
|
||
return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom; | ||
}, | ||
[rect] | ||
); | ||
|
||
useEffect(() => { | ||
const onTouchStart = (e: TouchEvent) => { | ||
const x = e.changedTouches[0].clientX; | ||
const y = e.changedTouches[0].clientY; | ||
|
||
if (insideRect(x, y)) { | ||
setStart({ x, timestamp: Date.now() }); | ||
} | ||
}; | ||
|
||
document.addEventListener('touchstart', onTouchStart); | ||
return () => document.removeEventListener('touchstart', onTouchStart); | ||
}, [insideRect]); | ||
|
||
useEffect(() => { | ||
if (start === undefined) { | ||
return; | ||
} | ||
|
||
const onTouchEnd = (e: TouchEvent) => { | ||
const x = e.changedTouches[0].clientX; | ||
const y = e.changedTouches[0].clientY; | ||
|
||
if (insideRect(x, y) && Date.now() - start.timestamp <= ms) { | ||
if (start.x >= x + distance) { | ||
onSwipe('right'); | ||
} else if (start.x <= x - distance) { | ||
onSwipe('left'); | ||
} | ||
} | ||
|
||
setStart(undefined); | ||
}; | ||
|
||
document.addEventListener('touchend', onTouchEnd); | ||
return () => document.removeEventListener('touchend', onTouchEnd); | ||
}, [start, distance, ms, onSwipe, insideRect]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters