|
1 | 1 | import React from 'react'; |
2 | | -import { StyleSheet, Text, View } from 'react-native'; |
| 2 | +import { ScrollView, StyleSheet, Text, View } from 'react-native'; |
3 | 3 | import Animated from 'react-native-reanimated'; |
4 | 4 |
|
5 | | -export default function PseudoActiveExample() { |
| 5 | +function BasicExample() { |
6 | 6 | return ( |
7 | | - <View style={styles.container}> |
8 | | - <Text style={styles.label}>Press and hold the box</Text> |
| 7 | + <View style={styles.section}> |
| 8 | + <Text style={styles.sectionTitle}>Basic</Text> |
| 9 | + <Text style={styles.hint}>Press and hold</Text> |
9 | 10 | <Animated.View |
10 | 11 | style={{ |
11 | 12 | ...styles.box, |
12 | | - transitionDuration: '1000ms', |
| 13 | + transitionDuration: '180ms', |
13 | 14 | transitionTimingFunction: 'ease-in-out', |
14 | 15 | opacity: { default: 1, ':active': 0.6 }, |
15 | 16 | transform: { |
16 | 17 | default: [{ scale: 1 }, { rotate: '0deg' }], |
17 | 18 | ':active': [{ scale: 0.7 }, { rotate: '45deg' }], |
18 | 19 | }, |
19 | | - backgroundColor: { default: '#4a90e2', ':active': 'pink' }, |
| 20 | + backgroundColor: { default: '#4a90e2', ':active': '#e84393' }, |
20 | 21 | }} |
21 | 22 | /> |
22 | | - <Text style={styles.description}> |
23 | | - Style changes happen in C++ on UI thread.{'\n'} |
24 | | - No JS frame should appear on the profiler. |
| 23 | + </View> |
| 24 | + ); |
| 25 | +} |
| 26 | + |
| 27 | +function TargetingExample() { |
| 28 | + return ( |
| 29 | + <View style={styles.section}> |
| 30 | + <Text style={styles.sectionTitle}>Bottommost element wins</Text> |
| 31 | + <Text style={styles.hint}> |
| 32 | + Unlike on the web where <Text style={styles.code}>:active</Text>{' '} |
| 33 | + propagates to all ancestors, here only the deepest element with{' '} |
| 34 | + <Text style={styles.code}>:active</Text> gets activated.{'\n\n'} |
| 35 | + Press a button → only the button activates.{'\n'} |
| 36 | + Press the card background → only the card activates. |
25 | 37 | </Text> |
| 38 | + |
| 39 | + <Animated.View |
| 40 | + style={{ |
| 41 | + ...styles.card, |
| 42 | + backgroundColor: { default: '#fff', ':active': '#fff3cd' }, |
| 43 | + transform: { |
| 44 | + default: [{ scale: 1 }], |
| 45 | + ':active': [{ scale: 0.97 }], |
| 46 | + }, |
| 47 | + transitionDuration: '120ms', |
| 48 | + }}> |
| 49 | + <Text style={styles.cardLabel}>Card (has :active)</Text> |
| 50 | + <View style={styles.row}> |
| 51 | + <Animated.View |
| 52 | + style={{ |
| 53 | + ...styles.pill, |
| 54 | + backgroundColor: { default: '#4a90e2', ':active': '#1a5fb4' }, |
| 55 | + transform: { |
| 56 | + default: [{ scale: 1 }], |
| 57 | + ':active': [{ scale: 0.88 }], |
| 58 | + }, |
| 59 | + transitionDuration: '100ms', |
| 60 | + }}> |
| 61 | + <Text style={styles.pillText}>Button A</Text> |
| 62 | + </Animated.View> |
| 63 | + <Animated.View |
| 64 | + style={{ |
| 65 | + ...styles.pill, |
| 66 | + backgroundColor: { default: '#e74c3c', ':active': '#922b21' }, |
| 67 | + transform: { |
| 68 | + default: [{ scale: 1 }], |
| 69 | + ':active': [{ scale: 0.88 }], |
| 70 | + }, |
| 71 | + transitionDuration: '100ms', |
| 72 | + }}> |
| 73 | + <Text style={styles.pillText}>Button B</Text> |
| 74 | + </Animated.View> |
| 75 | + </View> |
| 76 | + <Text style={styles.cardHint}>← press here to activate card</Text> |
| 77 | + </Animated.View> |
26 | 78 | </View> |
27 | 79 | ); |
28 | 80 | } |
29 | 81 |
|
| 82 | +export default function PseudoActiveExample() { |
| 83 | + return ( |
| 84 | + <ScrollView contentContainerStyle={styles.screen}> |
| 85 | + <BasicExample /> |
| 86 | + <TargetingExample /> |
| 87 | + <Text style={styles.footer}> |
| 88 | + All transitions run on the UI thread - no JS frames. |
| 89 | + </Text> |
| 90 | + </ScrollView> |
| 91 | + ); |
| 92 | +} |
| 93 | + |
30 | 94 | const styles = StyleSheet.create({ |
31 | | - container: { |
32 | | - flex: 1, |
33 | | - alignItems: 'center', |
34 | | - justifyContent: 'center', |
35 | | - gap: 24, |
| 95 | + screen: { |
36 | 96 | padding: 20, |
| 97 | + gap: 32, |
| 98 | + paddingBottom: 48, |
37 | 99 | }, |
38 | | - label: { |
39 | | - fontSize: 16, |
40 | | - fontWeight: '600', |
| 100 | + section: { |
| 101 | + gap: 12, |
| 102 | + }, |
| 103 | + sectionTitle: { |
| 104 | + fontSize: 17, |
| 105 | + fontWeight: '700', |
| 106 | + color: '#111', |
| 107 | + }, |
| 108 | + hint: { |
| 109 | + fontSize: 13, |
| 110 | + color: '#666', |
| 111 | + lineHeight: 20, |
| 112 | + }, |
| 113 | + code: { |
| 114 | + fontFamily: 'monospace', |
| 115 | + color: '#4a90e2', |
41 | 116 | }, |
42 | 117 | box: { |
43 | 118 | width: 150, |
44 | 119 | height: 150, |
45 | 120 | borderRadius: 16, |
| 121 | + alignSelf: 'center', |
46 | 122 | }, |
47 | | - description: { |
48 | | - textAlign: 'center', |
49 | | - color: '#666', |
| 123 | + |
| 124 | + card: { |
| 125 | + borderRadius: 16, |
| 126 | + padding: 16, |
| 127 | + gap: 12, |
| 128 | + borderWidth: 1, |
| 129 | + borderColor: '#e8e8e8', |
| 130 | + shadowColor: '#000', |
| 131 | + shadowOffset: { width: 0, height: 2 }, |
| 132 | + shadowOpacity: 0.06, |
| 133 | + shadowRadius: 8, |
| 134 | + elevation: 2, |
| 135 | + }, |
| 136 | + cardLabel: { |
| 137 | + fontSize: 13, |
| 138 | + fontWeight: '600', |
| 139 | + color: '#999', |
| 140 | + }, |
| 141 | + row: { |
| 142 | + flexDirection: 'row', |
| 143 | + gap: 10, |
| 144 | + }, |
| 145 | + pill: { |
| 146 | + paddingHorizontal: 20, |
| 147 | + paddingVertical: 10, |
| 148 | + borderRadius: 100, |
| 149 | + }, |
| 150 | + pillText: { |
| 151 | + color: '#fff', |
| 152 | + fontWeight: '600', |
50 | 153 | fontSize: 14, |
51 | | - lineHeight: 22, |
| 154 | + }, |
| 155 | + cardHint: { |
| 156 | + fontSize: 12, |
| 157 | + color: '#bbb', |
| 158 | + fontStyle: 'italic', |
| 159 | + }, |
| 160 | + footer: { |
| 161 | + textAlign: 'center', |
| 162 | + fontSize: 12, |
| 163 | + color: '#aaa', |
| 164 | + fontFamily: 'monospace', |
52 | 165 | }, |
53 | 166 | }); |
0 commit comments