-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathText.tsx
More file actions
84 lines (72 loc) · 2.28 KB
/
Copy pathText.tsx
File metadata and controls
84 lines (72 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React, {
ForwardedRef,
forwardRef,
RefObject,
useEffect,
useRef,
} from 'react';
import {
Platform,
Text as RNText,
TextProps as RNTextProps,
} from 'react-native';
import { GestureObjects as Gesture } from '../handlers/gestures/gestureObjects';
import { GestureDetector } from '../handlers/gestures/GestureDetector';
/**
* @deprecated `LegacyText` is deprecated. Since Gesture Handler 3 you can wrap `Text` with `GestureDetector`.
*/
export const LegacyText = forwardRef(
(
props: RNTextProps,
ref: ForwardedRef<React.ComponentRef<typeof RNText>>
) => {
const { onPress, onLongPress, ...rest } = props;
const textRef = useRef<RNText | null>(null);
const native = Gesture.Native().runOnJS(true);
const refHandler = (node: any) => {
textRef.current = node;
if (ref === null) {
return;
}
if (typeof ref === 'function') {
ref(node);
} else {
ref.current = node;
}
};
// This is a special case for `Text` component. After https://github.com/software-mansion/react-native-gesture-handler/pull/3379 we check for
// `displayName` field. However, `Text` from RN has this field set to `Text`, but is also present in `RNSVGElements` set.
// We don't want to treat our `Text` as the one from `SVG`, therefore we add special field to ref.
refHandler.rngh = true;
useEffect(() => {
if (Platform.OS !== 'web') {
return;
}
const textElement = ref
? (ref as RefObject<React.ComponentRef<typeof RNText>>).current
: textRef.current;
// At this point we are sure that textElement is div in HTML tree
(textElement as unknown as HTMLDivElement)?.setAttribute(
'rnghtext',
'true'
);
}, []);
return onPress || onLongPress ? (
<GestureDetector gesture={native}>
<RNText
onPress={onPress}
onLongPress={onLongPress}
ref={refHandler}
{...rest}
/>
</GestureDetector>
) : (
<RNText ref={ref} {...rest} />
);
}
);
/**
* @deprecated `LegacyText` is deprecated. Since Gesture Handler 3 you can wrap `Text` with `GestureDetector`.
*/
// eslint-disable-next-line @typescript-eslint/no-redeclare
export type LegacyText = typeof LegacyText & RNText;