|
| 1 | +import React from 'react'; |
| 2 | +import { View, Text, StyleSheet } from 'react-native'; |
| 3 | +import { Pressable } from 'react-native-gesture-handler'; |
| 4 | + |
| 5 | +export default function NestedPressableExample() { |
| 6 | + const pressIn = (name: string) => { |
| 7 | + console.log(`${name} Pressable pressed in`); |
| 8 | + }; |
| 9 | + |
| 10 | + const pressOut = (name: string) => { |
| 11 | + console.log(`${name} Pressable pressed out`); |
| 12 | + }; |
| 13 | + |
| 14 | + const press = (name: string) => { |
| 15 | + console.log(`${name} Pressable pressed`); |
| 16 | + }; |
| 17 | + |
| 18 | + const hoverIn = (name: string) => { |
| 19 | + console.log(`${name} Hovered in`); |
| 20 | + }; |
| 21 | + |
| 22 | + const hoverOut = (name: string) => { |
| 23 | + console.log(`${name} Hovered out`); |
| 24 | + }; |
| 25 | + |
| 26 | + const longPress = (name: string) => { |
| 27 | + console.log(`${name} Long pressed`); |
| 28 | + }; |
| 29 | + |
| 30 | + return ( |
| 31 | + <View style={{ flex: 1, backgroundColor: 'white' }}> |
| 32 | + <Pressable |
| 33 | + style={({ pressed }) => [ |
| 34 | + styles.pressableFirstLayer, |
| 35 | + pressed && styles.highlight, |
| 36 | + ]} |
| 37 | + onPressIn={() => pressIn('First')} |
| 38 | + onPressOut={() => pressOut('First')} |
| 39 | + onPress={() => press('First')} |
| 40 | + onHoverIn={() => hoverIn('First')} |
| 41 | + onHoverOut={() => hoverOut('First')} |
| 42 | + onLongPress={() => longPress('First')}> |
| 43 | + <Pressable |
| 44 | + style={({ pressed }) => [ |
| 45 | + styles.pressableSecondLayer, |
| 46 | + pressed && styles.highlight, |
| 47 | + ]} |
| 48 | + onPressIn={() => pressIn('Second')} |
| 49 | + onPressOut={() => pressOut('Second')} |
| 50 | + onPress={() => press('Second')} |
| 51 | + onHoverIn={() => hoverIn('Second')} |
| 52 | + onHoverOut={() => hoverOut('Second')} |
| 53 | + onLongPress={() => longPress('Second')}> |
| 54 | + <Pressable |
| 55 | + style={({ pressed }) => [ |
| 56 | + styles.pressableThirdLayer, |
| 57 | + pressed && styles.highlight, |
| 58 | + ]} |
| 59 | + onPressIn={() => pressIn('Third')} |
| 60 | + onPressOut={() => pressOut('Third')} |
| 61 | + onPress={() => press('Third')} |
| 62 | + onHoverIn={() => hoverIn('Third')} |
| 63 | + onHoverOut={() => hoverOut('Third')} |
| 64 | + onLongPress={() => longPress('Third')}> |
| 65 | + <Text style={styles.rectText}>Third Pressable</Text> |
| 66 | + </Pressable> |
| 67 | + <Text style={styles.rectText}>Second Pressable</Text> |
| 68 | + </Pressable> |
| 69 | + <Text style={styles.rectText}>First Pressable</Text> |
| 70 | + </Pressable> |
| 71 | + </View> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +const BACKGROUND_COLOR = '#F5FCFF'; |
| 76 | + |
| 77 | +const styles = StyleSheet.create({ |
| 78 | + pressableFirstLayer: { |
| 79 | + backgroundColor: '#FFD6E0', |
| 80 | + padding: 40, |
| 81 | + margin: 'auto', |
| 82 | + }, |
| 83 | + pressableSecondLayer: { |
| 84 | + backgroundColor: '#F29DC3', |
| 85 | + padding: 40, |
| 86 | + margin: 'auto', |
| 87 | + }, |
| 88 | + pressableThirdLayer: { |
| 89 | + width: 160, |
| 90 | + height: 160, |
| 91 | + margin: 'auto', |
| 92 | + backgroundColor: 'mediumpurple', |
| 93 | + }, |
| 94 | + textWrapper: { |
| 95 | + flex: 1, |
| 96 | + justifyContent: 'center', |
| 97 | + alignItems: 'center', |
| 98 | + }, |
| 99 | + text: { |
| 100 | + color: 'white', |
| 101 | + }, |
| 102 | + rectText: { |
| 103 | + color: BACKGROUND_COLOR, |
| 104 | + fontWeight: '700', |
| 105 | + position: 'absolute', |
| 106 | + right: 5, |
| 107 | + bottom: 2, |
| 108 | + }, |
| 109 | + highlight: { |
| 110 | + backgroundColor: 'red', |
| 111 | + }, |
| 112 | +}); |
0 commit comments