-
-
Notifications
You must be signed in to change notification settings - Fork 984
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description This PR adds `Text` component to **Gesture Handler**. Upon investigating #3159 we decided that it will be better to add our own `Text` component, instead of forcing users to create their own version of `Text` with `NativeViewGestureHandler`. ## Test plan <details> <summary>New example:</summary> ```jsx import { useState } from 'react'; import { StyleSheet } from 'react-native'; import { Text, GestureHandlerRootView, TouchableOpacity, } from 'react-native-gesture-handler'; export default function NestedText() { const [counter, setCounter] = useState(0); return ( <GestureHandlerRootView style={styles.container}> <Text style={{ fontSize: 30 }}>{`Counter: ${counter}`}</Text> <TouchableOpacity onPress={() => { console.log('Touchable'); setCounter((prev) => prev + 1); }}> <Text style={[styles.textCommon, styles.outerText]} onPress={() => { console.log('Outer text'); setCounter((prev) => prev + 1); }}> {'Outer Text '} <Text style={[styles.textCommon, styles.innerText]} onPress={() => { console.log('Nested text'); setCounter((prev) => prev + 1); }}> {'Nested Text'} </Text> </Text> </TouchableOpacity> </GestureHandlerRootView> ); } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', gap: 20, }, textCommon: { padding: 10, color: 'white', }, outerText: { fontSize: 30, borderWidth: 2, backgroundColor: '#131313', }, innerText: { fontSize: 25, backgroundColor: '#F06312', }, }); ``` </details>
- Loading branch information
Showing
6 changed files
with
161 additions
and
4 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
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,66 @@ | ||
import { useState } from 'react'; | ||
import { StyleSheet } from 'react-native'; | ||
import { | ||
Text, | ||
GestureHandlerRootView, | ||
TouchableOpacity, | ||
} from 'react-native-gesture-handler'; | ||
|
||
export default function NestedText() { | ||
const [counter, setCounter] = useState(0); | ||
|
||
return ( | ||
<GestureHandlerRootView style={styles.container}> | ||
<Text style={{ fontSize: 30 }}>{`Counter: ${counter}`}</Text> | ||
|
||
<TouchableOpacity | ||
onPress={() => { | ||
console.log('Touchable'); | ||
setCounter((prev) => prev + 1); | ||
}}> | ||
<Text | ||
style={[styles.textCommon, styles.outerText]} | ||
onPress={() => { | ||
console.log('Outer text'); | ||
setCounter((prev) => prev + 1); | ||
}}> | ||
{'Outer Text '} | ||
<Text | ||
style={[styles.textCommon, styles.innerText]} | ||
onPress={() => { | ||
console.log('Nested text'); | ||
setCounter((prev) => prev + 1); | ||
}}> | ||
{'Nested Text'} | ||
</Text> | ||
</Text> | ||
</TouchableOpacity> | ||
</GestureHandlerRootView> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
|
||
gap: 20, | ||
}, | ||
|
||
textCommon: { | ||
padding: 10, | ||
color: 'white', | ||
}, | ||
|
||
outerText: { | ||
fontSize: 30, | ||
borderWidth: 2, | ||
backgroundColor: '#131313', | ||
}, | ||
|
||
innerText: { | ||
fontSize: 25, | ||
backgroundColor: '#F06312', | ||
}, | ||
}); |
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,60 @@ | ||
import React, { | ||
ForwardedRef, | ||
forwardRef, | ||
RefObject, | ||
useEffect, | ||
useRef, | ||
} from 'react'; | ||
import { | ||
Platform, | ||
Text as RNText, | ||
TextProps as RNTextProps, | ||
} from 'react-native'; | ||
|
||
import { Gesture, GestureDetector } from '../'; | ||
|
||
export const Text = forwardRef( | ||
(props: RNTextProps, ref: ForwardedRef<RNText>) => { | ||
const { onPress, ...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; | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (Platform.OS !== 'web') { | ||
return; | ||
} | ||
|
||
const textElement = ref | ||
? (ref as RefObject<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 ( | ||
<GestureDetector gesture={native}> | ||
<RNText onPress={onPress} ref={refHandler} {...rest} /> | ||
</GestureDetector> | ||
); | ||
} | ||
); | ||
// eslint-disable-next-line @typescript-eslint/no-redeclare | ||
export type Text = typeof Text & RNText; |
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