|
| 1 | +import React, { memo, useCallback, useState } from 'react'; |
| 2 | +import type { ListRenderItem } from 'react-native'; |
| 3 | +import { |
| 4 | + Dimensions, |
| 5 | + Pressable, |
| 6 | + SafeAreaView, |
| 7 | + StyleSheet, |
| 8 | + Text, |
| 9 | + TouchableOpacity, |
| 10 | + View, |
| 11 | +} from 'react-native'; |
| 12 | +import Animated, { |
| 13 | + CurvedTransition, |
| 14 | + EntryExitTransition, |
| 15 | + FadeOut, |
| 16 | + FadeIn, |
| 17 | + FadingTransition, |
| 18 | + JumpingTransition, |
| 19 | + LayoutAnimationConfig, |
| 20 | + LinearTransition, |
| 21 | + SequencedTransition, |
| 22 | +} from 'react-native-reanimated'; |
| 23 | + |
| 24 | +const ITEMS = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']; |
| 25 | +const LAYOUT_TRANSITIONS = [ |
| 26 | + LinearTransition, |
| 27 | + FadingTransition, |
| 28 | + SequencedTransition, |
| 29 | + JumpingTransition, |
| 30 | + CurvedTransition, |
| 31 | + EntryExitTransition, |
| 32 | +] as const; |
| 33 | + |
| 34 | +type ListItemProps = { |
| 35 | + id: string; |
| 36 | + text: string; |
| 37 | + onPress: (id: string) => void; |
| 38 | +}; |
| 39 | + |
| 40 | +const ListItem = memo(function ({ id, text, onPress }: ListItemProps) { |
| 41 | + return ( |
| 42 | + <Pressable onPress={() => onPress(id)} style={styles.listItem}> |
| 43 | + <Text style={styles.itemText}>{text}</Text> |
| 44 | + </Pressable> |
| 45 | + ); |
| 46 | +}); |
| 47 | + |
| 48 | +export default function ListItemLayoutAnimation() { |
| 49 | + const [layoutTransitionEnabled, setLayoutTransitionEnabled] = useState(true); |
| 50 | + const [currentTransitionIndex, setCurrentTransitionIndex] = useState(0); |
| 51 | + const [items, setItems] = useState(ITEMS); |
| 52 | + |
| 53 | + const removeItem = useCallback((id: string) => { |
| 54 | + setItems((prevItems) => prevItems.filter((item) => item !== id)); |
| 55 | + }, []); |
| 56 | + |
| 57 | + const renderItem = useCallback<ListRenderItem<string>>( |
| 58 | + ({ item }) => <ListItem id={item} text={item} onPress={removeItem} />, |
| 59 | + [removeItem] |
| 60 | + ); |
| 61 | + |
| 62 | + const getNewItemName = useCallback(() => { |
| 63 | + let i = 1; |
| 64 | + while (items.includes(`Item ${i}`)) { |
| 65 | + i++; |
| 66 | + } |
| 67 | + return `Item ${i}`; |
| 68 | + }, [items]); |
| 69 | + |
| 70 | + const reorderItems = useCallback(() => { |
| 71 | + setItems((prevItems) => { |
| 72 | + const newItems = [...prevItems]; |
| 73 | + newItems.sort(() => Math.random() - 0.5); |
| 74 | + return newItems; |
| 75 | + }); |
| 76 | + }, []); |
| 77 | + |
| 78 | + const resetOrder = useCallback(() => { |
| 79 | + setItems((prevItems) => { |
| 80 | + const newItems = [...prevItems]; |
| 81 | + newItems.sort((left, right) => { |
| 82 | + const aNum = parseInt(left.match(/\d+$/)![0], 10); |
| 83 | + const bNum = parseInt(right.match(/\d+$/)![0], 10); |
| 84 | + return aNum - bNum; |
| 85 | + }); |
| 86 | + return newItems; |
| 87 | + }); |
| 88 | + }, []); |
| 89 | + |
| 90 | + const transition = layoutTransitionEnabled |
| 91 | + ? LAYOUT_TRANSITIONS[currentTransitionIndex] |
| 92 | + : undefined; |
| 93 | + |
| 94 | + return ( |
| 95 | + <LayoutAnimationConfig skipEntering> |
| 96 | + <SafeAreaView style={styles.container}> |
| 97 | + <View style={styles.menu}> |
| 98 | + <View style={styles.row}> |
| 99 | + <Text style={styles.infoText}>Layout animation: </Text> |
| 100 | + <TouchableOpacity |
| 101 | + onPress={() => { |
| 102 | + setLayoutTransitionEnabled((prev) => !prev); |
| 103 | + }}> |
| 104 | + <Text style={styles.buttonText}> |
| 105 | + {layoutTransitionEnabled ? 'Enabled' : 'Disabled'} |
| 106 | + </Text> |
| 107 | + </TouchableOpacity> |
| 108 | + </View> |
| 109 | + {transition && ( |
| 110 | + <Animated.View |
| 111 | + style={styles.row} |
| 112 | + entering={FadeIn} |
| 113 | + exiting={FadeOut}> |
| 114 | + <Text style={styles.infoText}> |
| 115 | + Current: {transition?.presetName} |
| 116 | + </Text> |
| 117 | + <TouchableOpacity |
| 118 | + onPress={() => { |
| 119 | + setCurrentTransitionIndex( |
| 120 | + (prev) => (prev + 1) % LAYOUT_TRANSITIONS.length |
| 121 | + ); |
| 122 | + }}> |
| 123 | + <Text style={styles.buttonText}>Change</Text> |
| 124 | + </TouchableOpacity> |
| 125 | + </Animated.View> |
| 126 | + )} |
| 127 | + </View> |
| 128 | + |
| 129 | + <Animated.FlatList |
| 130 | + style={styles.list} |
| 131 | + data={items} |
| 132 | + renderItem={renderItem} |
| 133 | + keyExtractor={(item) => item} |
| 134 | + contentContainerStyle={styles.contentContainer} |
| 135 | + itemLayoutAnimation={layoutTransitionEnabled ? transition : undefined} |
| 136 | + layout={transition} |
| 137 | + /> |
| 138 | + |
| 139 | + <Animated.View style={styles.menu} layout={transition}> |
| 140 | + <Text style={styles.infoText}>Press an item to remove it</Text> |
| 141 | + <TouchableOpacity |
| 142 | + onPress={() => setItems([...items, getNewItemName()])}> |
| 143 | + <Text style={styles.buttonText}>Add item</Text> |
| 144 | + </TouchableOpacity> |
| 145 | + <Animated.View style={styles.row} layout={transition}> |
| 146 | + <TouchableOpacity onPress={reorderItems}> |
| 147 | + <Text style={styles.buttonText}>Reorder</Text> |
| 148 | + </TouchableOpacity> |
| 149 | + <TouchableOpacity onPress={resetOrder}> |
| 150 | + <Text style={styles.buttonText}>Reset order</Text> |
| 151 | + </TouchableOpacity> |
| 152 | + </Animated.View> |
| 153 | + </Animated.View> |
| 154 | + </SafeAreaView> |
| 155 | + </LayoutAnimationConfig> |
| 156 | + ); |
| 157 | +} |
| 158 | + |
| 159 | +const styles = StyleSheet.create({ |
| 160 | + container: { |
| 161 | + flex: 1, |
| 162 | + }, |
| 163 | + contentContainer: { |
| 164 | + padding: 16, |
| 165 | + gap: 16, |
| 166 | + }, |
| 167 | + row: { |
| 168 | + flexDirection: 'row', |
| 169 | + gap: 16, |
| 170 | + alignItems: 'center', |
| 171 | + }, |
| 172 | + list: { |
| 173 | + flexGrow: 0, |
| 174 | + maxHeight: Dimensions.get('window').height - 300, |
| 175 | + }, |
| 176 | + listItem: { |
| 177 | + padding: 20, |
| 178 | + backgroundColor: '#ad8ee9', |
| 179 | + shadowColor: '#000', |
| 180 | + shadowOpacity: 0.05, |
| 181 | + }, |
| 182 | + itemText: { |
| 183 | + color: 'white', |
| 184 | + fontSize: 22, |
| 185 | + }, |
| 186 | + menu: { |
| 187 | + padding: 16, |
| 188 | + alignItems: 'center', |
| 189 | + justifyContent: 'center', |
| 190 | + paddingTop: 16, |
| 191 | + gap: 8, |
| 192 | + }, |
| 193 | + infoText: { |
| 194 | + color: '#222534', |
| 195 | + fontSize: 18, |
| 196 | + }, |
| 197 | + buttonText: { |
| 198 | + fontSize: 18, |
| 199 | + fontWeight: 'bold', |
| 200 | + color: '#b59aeb', |
| 201 | + }, |
| 202 | +}); |
0 commit comments