|
| 1 | +'use client'; |
| 2 | + |
1 | 3 | import * as React from 'react'; |
2 | | -import { classNames, hasMouse as hasPointerLib, noop } from '@vkontakte/vkjs'; |
3 | | -import { usePlatform } from '../../hooks/usePlatform'; |
| 4 | +import { classNames, noop } from '@vkontakte/vkjs'; |
4 | 5 | import { getOffsetRect } from '../../lib/offset'; |
| 6 | +import { type CSSCustomProperties } from '../../types'; |
5 | 7 | import styles from './Tappable.module.css'; |
6 | 8 |
|
7 | 9 | /* eslint-disable jsdoc/require-jsdoc */ |
8 | 10 |
|
9 | | -/** |
10 | | - * Возможно нужен Ripple эффект. Данный хук нужен для отказа |
11 | | - * от двойного ререндера. |
12 | | - */ |
13 | | -export const useMaybeNeedRipple = ( |
14 | | - activeMode: string, |
15 | | - hasPointer: boolean | undefined, |
16 | | -): boolean => { |
17 | | - const platform = usePlatform(); |
18 | | - |
19 | | - return platform === 'android' && !hasPointer && activeMode === 'background'; |
20 | | -}; |
| 11 | +type WaveState = 'inactive' | 'fadeIn' | 'fadeOut'; |
21 | 12 |
|
22 | 13 | interface Wave { |
23 | 14 | x: number; |
24 | 15 | y: number; |
25 | | - id: number; |
26 | | - pointerId: number; |
| 16 | + rippleSize: number; |
| 17 | + rippleScale: number; |
| 18 | + state: WaveState; |
27 | 19 | } |
28 | 20 |
|
29 | | -const DELAY = 70; |
30 | | -const WAVE_LIVE = 225; |
| 21 | +const TOUCH_DELAY = 150; |
| 22 | + |
| 23 | +const INITIAL_ORIGIN_SCALE = 0.2; |
| 24 | +const SOFT_EDGE_MINIMUM_SIZE = 75; |
| 25 | +const SOFT_EDGE_CONTAINER_RATIO = 0.35; |
| 26 | +const PADDING = 10; |
| 27 | + |
| 28 | +function calcRippleSize(width: number, height: number): number { |
| 29 | + return Math.floor(Math.max(width, height) * INITIAL_ORIGIN_SCALE); |
| 30 | +} |
| 31 | + |
| 32 | +function calcRippleScale(initialRippleSize: number, width: number, height: number) { |
| 33 | + const maxDim = Math.max(width, height); |
| 34 | + const softEdgeSize = Math.max(SOFT_EDGE_CONTAINER_RATIO * maxDim, SOFT_EDGE_MINIMUM_SIZE); |
| 35 | + |
| 36 | + const diagonal = Math.sqrt(width ** 2 + height ** 2); |
| 37 | + const maxRadius = diagonal * 2 + PADDING; |
| 38 | + |
| 39 | + return (maxRadius + softEdgeSize) / initialRippleSize; |
| 40 | +} |
31 | 41 |
|
32 | 42 | /** |
33 | 43 | * Хук для создания Ripple эффектов. |
34 | 44 | */ |
35 | 45 | export const useRipple = ( |
36 | 46 | needRipple: boolean, |
37 | | - hasPointerContext: boolean | undefined, |
38 | 47 | ): { |
39 | | - clicks: Wave[]; |
| 48 | + wave: Wave; |
40 | 49 | onPointerDown: React.PointerEventHandler<HTMLSpanElement>; |
| 50 | + onPointerUp: React.PointerEventHandler<HTMLSpanElement>; |
41 | 51 | onPointerCancel: React.PointerEventHandler<HTMLSpanElement>; |
| 52 | + onWaveAnimationEnd: React.AnimationEventHandler<HTMLSpanElement>; |
42 | 53 | } => { |
43 | | - const [clicks, setClicks] = React.useState<Wave[]>([]); |
44 | | - |
45 | | - /** |
46 | | - * Коллекция нажатий и таймеров задержки появления волны. |
47 | | - */ |
48 | | - const pointerDelayTimers = React.useRef<Map<number, ReturnType<typeof setTimeout>>>(null); |
49 | | - if (pointerDelayTimers.current === null) { |
50 | | - pointerDelayTimers.current = new Map(); |
51 | | - } |
| 54 | + const pointerDelayTimerRef = React.useRef<ReturnType<typeof setTimeout>>(undefined); |
52 | 55 |
|
53 | | - React.useEffect( |
54 | | - function setClearClicksTimeout() { |
55 | | - const clicksTimeoutId = clicks.length > 0 ? setTimeout(() => setClicks([]), WAVE_LIVE) : null; |
56 | | - return function cancelClearClicksTimeout() { |
57 | | - if (clicksTimeoutId) { |
58 | | - clearTimeout(clicksTimeoutId); |
59 | | - } |
60 | | - }; |
61 | | - }, |
62 | | - [clicks], |
63 | | - ); |
| 56 | + const pointerDownRef = React.useRef(false); |
| 57 | + const fadeInAnimationInProgressRef = React.useRef(false); |
64 | 58 |
|
65 | | - function addClick(x: number, y: number, pointerId: number) { |
66 | | - const dateNow = Date.now(); |
67 | | - const filteredClicks = clicks.filter((click) => click.id + WAVE_LIVE > dateNow); |
| 59 | + const [state, setState] = React.useState<WaveState>('inactive'); |
68 | 60 |
|
69 | | - setClicks([...filteredClicks, { x, y, id: dateNow, pointerId }]); |
70 | | - pointerDelayTimers.current!.delete(pointerId); |
| 61 | + const [x, setX] = React.useState(0); |
| 62 | + const [y, setY] = React.useState(0); |
| 63 | + const [rippleSize, setRippleSize] = React.useState(0); |
| 64 | + const [rippleScale, setRippleScale] = React.useState(0); |
| 65 | + |
| 66 | + const checkEndFadeIn = () => { |
| 67 | + if (state !== 'fadeIn' || pointerDownRef.current || fadeInAnimationInProgressRef.current) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + setState('fadeOut'); |
| 72 | + }; |
| 73 | + |
| 74 | + const onAnimationEnd = () => { |
| 75 | + switch (state) { |
| 76 | + case 'fadeIn': |
| 77 | + fadeInAnimationInProgressRef.current = false; |
| 78 | + checkEndFadeIn(); |
| 79 | + break; |
| 80 | + case 'fadeOut': |
| 81 | + setState('inactive'); |
| 82 | + break; |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + function addClick(x: number, y: number, rippleSize: number, rippleScale: number) { |
| 87 | + setState('fadeIn'); |
| 88 | + fadeInAnimationInProgressRef.current = true; |
| 89 | + setX(x); |
| 90 | + setY(y); |
| 91 | + setRippleSize(rippleSize); |
| 92 | + setRippleScale(rippleScale); |
71 | 93 | } |
72 | 94 |
|
73 | | - /** |
74 | | - * Добавляем волну с задержкой. Задержка необходима при отмене волны. |
75 | | - */ |
76 | 95 | const onPointerDown: React.PointerEventHandler<HTMLSpanElement> = (e) => { |
77 | | - const { top, left } = getOffsetRect(e.currentTarget); |
| 96 | + setState('inactive'); |
| 97 | + const { top, left, width, height } = getOffsetRect(e.currentTarget); |
| 98 | + |
| 99 | + const rippleSize = calcRippleSize(width, height); |
| 100 | + const rippleScale = calcRippleScale(rippleSize, width, height); |
| 101 | + |
78 | 102 | const x = e.clientX - (left ?? 0); |
79 | 103 | const y = e.clientY - (top ?? 0); |
80 | 104 |
|
81 | | - pointerDelayTimers.current!.set( |
82 | | - e.pointerId, |
83 | | - setTimeout(() => addClick(x, y, e.pointerId), DELAY), |
84 | | - ); |
| 105 | + const delay = e.pointerType === 'touch' ? TOUCH_DELAY : 0; |
| 106 | + |
| 107 | + pointerDelayTimerRef.current = setTimeout(() => addClick(x, y, rippleSize, rippleScale), delay); |
| 108 | + pointerDownRef.current = true; |
85 | 109 | }; |
86 | 110 |
|
87 | | - const onPointerCancel: React.PointerEventHandler<HTMLSpanElement> = (e) => { |
88 | | - const timer = pointerDelayTimers.current!.get(e.pointerId); |
89 | | - clearTimeout(timer); |
90 | | - pointerDelayTimers.current!.delete(e.pointerId); |
| 111 | + const onPointerCancel: React.PointerEventHandler<HTMLSpanElement> = () => { |
| 112 | + clearTimeout(pointerDelayTimerRef.current); |
| 113 | + pointerDownRef.current = false; |
| 114 | + checkEndFadeIn(); |
91 | 115 | }; |
92 | 116 |
|
93 | | - // WARNING: не использовать для рендеринга |
94 | | - const reallyNeedRipple = (!hasPointerLib || hasPointerContext === false) && needRipple; |
| 117 | + const onPointerUp: React.PointerEventHandler<HTMLSpanElement> = () => { |
| 118 | + pointerDownRef.current = false; |
| 119 | + checkEndFadeIn(); |
| 120 | + }; |
95 | 121 |
|
96 | 122 | return { |
97 | | - clicks, |
98 | | - onPointerDown: reallyNeedRipple ? onPointerDown : noop, |
99 | | - onPointerCancel: reallyNeedRipple ? onPointerCancel : noop, |
| 123 | + wave: { |
| 124 | + x, |
| 125 | + y, |
| 126 | + rippleSize, |
| 127 | + rippleScale, |
| 128 | + state, |
| 129 | + }, |
| 130 | + onWaveAnimationEnd: onAnimationEnd, |
| 131 | + onPointerDown: needRipple ? onPointerDown : noop, |
| 132 | + onPointerUp: needRipple ? onPointerUp : noop, |
| 133 | + onPointerCancel: needRipple ? onPointerCancel : noop, |
100 | 134 | }; |
101 | 135 | }; |
102 | 136 |
|
103 | 137 | export interface RippleProps { |
104 | 138 | needRipple: boolean; |
105 | | - clicks: Wave[]; |
| 139 | + wave: Wave; |
| 140 | + onWaveAnimationEnd: React.AnimationEventHandler<HTMLSpanElement>; |
106 | 141 | } |
107 | 142 |
|
108 | | -export const Ripple = ({ needRipple = true, clicks }: RippleProps): React.ReactNode => { |
| 143 | +const stylesState: Record<WaveState, string | undefined> = { |
| 144 | + inactive: undefined, |
| 145 | + fadeIn: styles.waveFadeIn, |
| 146 | + fadeOut: styles.waveFadeOut, |
| 147 | +}; |
| 148 | + |
| 149 | +export const Ripple = ({ |
| 150 | + needRipple = true, |
| 151 | + wave, |
| 152 | + onWaveAnimationEnd, |
| 153 | +}: RippleProps): React.ReactNode => { |
| 154 | + const style: React.CSSProperties & CSSCustomProperties = { |
| 155 | + 'top': wave.y - wave.rippleSize / 2, |
| 156 | + 'left': wave.x - wave.rippleSize / 2, |
| 157 | + 'width': wave.rippleSize, |
| 158 | + 'height': wave.rippleSize, |
| 159 | + '--vkui_internal--Tappable-scale': wave.rippleScale.toString(), |
| 160 | + }; |
| 161 | + |
109 | 162 | return ( |
110 | 163 | <span aria-hidden className={classNames(styles.stateLayer, needRipple && styles.ripple)}> |
111 | | - {clicks.map((wave) => ( |
112 | | - <span key={wave.id} className={styles.wave} style={{ top: wave.y, left: wave.x }} /> |
113 | | - ))} |
| 164 | + <span |
| 165 | + className={classNames(styles.wave, stylesState[wave.state])} |
| 166 | + style={style} |
| 167 | + onAnimationEnd={onWaveAnimationEnd} |
| 168 | + /> |
114 | 169 | </span> |
115 | 170 | ); |
116 | 171 | }; |
0 commit comments