mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1/**
2 * Copyright (c) JOB TODAY S.A. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9import React, {useCallback} from 'react'
10import {ScrollView, NativeTouchEvent, NativeSyntheticEvent} from 'react-native'
11
12import {Dimensions} from '../@types'
13
14const DOUBLE_TAP_DELAY = 300
15let lastTapTS: number | null = null
16
17/**
18 * This is iOS only.
19 * Same functionality for Android implemented inside usePanResponder hook.
20 */
21function useDoubleTapToZoom(
22 scrollViewRef: React.RefObject<ScrollView>,
23 scaled: boolean,
24 screen: Dimensions,
25) {
26 const handleDoubleTap = useCallback(
27 (event: NativeSyntheticEvent<NativeTouchEvent>) => {
28 const nowTS = new Date().getTime()
29 const scrollResponderRef = scrollViewRef?.current?.getScrollResponder()
30
31 if (lastTapTS && nowTS - lastTapTS < DOUBLE_TAP_DELAY) {
32 const {pageX, pageY} = event.nativeEvent
33 let targetX = 0
34 let targetY = 0
35 let targetWidth = screen.width
36 let targetHeight = screen.height
37
38 // Zooming in
39 // TODO: Add more precise calculation of targetX, targetY based on touch
40 if (!scaled) {
41 targetX = pageX / 2
42 targetY = pageY / 2
43 targetWidth = screen.width / 2
44 targetHeight = screen.height / 2
45 }
46
47 // @ts-ignore
48 scrollResponderRef?.scrollResponderZoomTo({
49 x: targetX,
50 y: targetY,
51 width: targetWidth,
52 height: targetHeight,
53 animated: true,
54 })
55 } else {
56 lastTapTS = nowTS
57 }
58 },
59 [scaled, screen.height, screen.width, scrollViewRef],
60 )
61
62 return handleDoubleTap
63}
64
65export default useDoubleTapToZoom