-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[LA] Replace Layout Transitions example #6151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
patrycjakalinska
merged 5 commits into
main
from
@patrycjakalinska/replace-layout-transition-example
Jul 1, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f79d43
Replace layout transitions example
patrycjakalinska f528eb7
Stylistic fixes
patrycjakalinska 31c430e
refactor for simpler code
patrycjakalinska acd4d2d
Add fix for non-serializable values
patrycjakalinska d0bf50c
Small adjustments to comments
patrycjakalinska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
225 changes: 225 additions & 0 deletions
225
apps/common-app/src/examples/LayoutAnimations/LayoutTransitionExample.tsx
patrycjakalinska marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,225 @@ | ||
| import React, { useState } from 'react'; | ||
| import { | ||
| View, | ||
| Text, | ||
| Button, | ||
| SafeAreaView, | ||
| StyleSheet, | ||
| TouchableOpacity, | ||
| } from 'react-native'; | ||
| import Animated, { | ||
| LinearTransition, | ||
| SequencedTransition, | ||
| FadingTransition, | ||
| FadeOut, | ||
| JumpingTransition, | ||
| CurvedTransition, | ||
| EntryExitTransition, | ||
| FlipInEasyX, | ||
| FlipOutYRight, | ||
| } from 'react-native-reanimated'; | ||
| import { createStackNavigator } from '@react-navigation/stack'; | ||
| import { NavigationContainer } from '@react-navigation/native'; | ||
| import { type StackScreenProps } from '@react-navigation/stack'; | ||
|
|
||
| interface Item { | ||
| id: number; | ||
| content: string; | ||
| color: string; | ||
| } | ||
|
|
||
| const INITIAL_LIST = [ | ||
| { id: 1, content: '🍌', color: '#b58df1' }, | ||
| { id: 2, content: '🍎', color: '#ffe780' }, | ||
| { id: 3, content: '🥛', color: '#fa7f7c' }, | ||
| { id: 4, content: '🍙', color: '#82cab2' }, | ||
| { id: 5, content: '🍇', color: '#fa7f7c' }, | ||
| { id: 6, content: '🍕', color: '#b58df1' }, | ||
| { id: 7, content: '🍔', color: '#ffe780' }, | ||
| { id: 8, content: '🍟', color: '#b58df1' }, | ||
| { id: 9, content: '🍩', color: '#82cab2' }, | ||
| ]; | ||
|
|
||
| type RootStackParamList = { | ||
| Home: undefined; | ||
| TransitionScreen: { title: string; transitionName: string }; | ||
| }; | ||
|
|
||
| type HomeScreenProps = StackScreenProps<RootStackParamList, 'Home'>; | ||
|
|
||
| const TRANSITIONS = [ | ||
| { name: 'Linear', title: 'Linear Transition', transition: LinearTransition }, | ||
| { | ||
| name: 'Sequenced', | ||
| title: 'Sequenced Transition', | ||
| transition: SequencedTransition, | ||
| }, | ||
| { name: 'Fading', title: 'Fading Transition', transition: FadingTransition }, | ||
| { | ||
| name: 'Jumping', | ||
| title: 'Jumping Transition', | ||
| transition: JumpingTransition, | ||
| }, | ||
| { name: 'Curved', title: 'Curved Transition', transition: CurvedTransition }, | ||
| { | ||
| name: 'EntryExit', | ||
| title: 'Entry/Exit Transition', | ||
| transition: | ||
| EntryExitTransition.entering(FlipInEasyX).exiting(FlipOutYRight), | ||
| }, | ||
| ]; | ||
|
|
||
| function HomeScreen({ navigation }: HomeScreenProps) { | ||
| return ( | ||
| <View style={styles.homeContainer}> | ||
| {TRANSITIONS.map(({ name, title, transition }) => ( | ||
| <Button | ||
| key={name} | ||
| title={title} | ||
| onPress={() => | ||
| navigation.navigate('TransitionScreen', { | ||
| title, | ||
| transitionName: name, | ||
| }) | ||
| } | ||
| /> | ||
| ))} | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| const Stack = createStackNavigator<RootStackParamList>(); | ||
|
|
||
| export default function Layout() { | ||
| return ( | ||
| <NavigationContainer independent={true}> | ||
| <Stack.Navigator screenOptions={{ title: 'Layout Transitions 🔥' }}> | ||
| <Stack.Screen name="Home" component={HomeScreen} /> | ||
| <Stack.Screen name="TransitionScreen" component={Transition} /> | ||
| </Stack.Navigator> | ||
| </NavigationContainer> | ||
| ); | ||
| } | ||
|
|
||
| type TransitionProps = StackScreenProps<RootStackParamList, 'TransitionScreen'>; | ||
|
|
||
| function Transition({ route }: TransitionProps) { | ||
| const { title, transitionName } = route.params; | ||
| const [items, setItems] = useState(INITIAL_LIST); | ||
|
|
||
| const transition = | ||
| TRANSITIONS.find((t) => t.name === transitionName)?.transition || | ||
| LinearTransition; | ||
|
|
||
| const removeItem = (idToRemove: number) => { | ||
| const updatedItems = items.filter((item) => item.id !== idToRemove); | ||
| setItems(updatedItems); | ||
| }; | ||
|
|
||
| return ( | ||
| <SafeAreaView style={styles.container}> | ||
| <Text style={styles.title}>{title}</Text> | ||
| <View> | ||
| <Items | ||
| selected={{ label: title, transition }} | ||
| items={items} | ||
| onRemove={removeItem} | ||
| /> | ||
| </View> | ||
| </SafeAreaView> | ||
| ); | ||
| } | ||
|
|
||
| interface ItemsProps { | ||
| selected: { label: string; transition: any }; | ||
| items: Item[]; | ||
| onRemove: (id: number) => void; | ||
| } | ||
|
|
||
| function Items({ selected, items, onRemove }: ItemsProps) { | ||
| return ( | ||
| <View style={styles.gridContainer}> | ||
| {items.map((item: Item) => ( | ||
| <Animated.View | ||
| key={item.id} | ||
| layout={selected.transition} | ||
| exiting={FadeOut} | ||
| style={[styles.tileContainer, { backgroundColor: item.color }]}> | ||
| <Tile content={item.content} onRemove={() => onRemove(item.id)} /> | ||
| </Animated.View> | ||
| ))} | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| function Tile({ | ||
| content, | ||
| onRemove, | ||
| }: { | ||
| content: string; | ||
| onRemove: () => void; | ||
| }) { | ||
| return ( | ||
| <TouchableOpacity onPress={onRemove} style={styles.tile}> | ||
| <Animated.Text style={styles.tileLabel}>{content}</Animated.Text> | ||
| </TouchableOpacity> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| flex: 1, | ||
| padding: 32, | ||
| width: 'auto', | ||
| display: 'flex', | ||
| minHeight: 300, | ||
| }, | ||
| gridContainer: { | ||
| flexDirection: 'row', | ||
| flexWrap: 'wrap', | ||
| alignItems: 'flex-start', | ||
| justifyContent: 'center', | ||
| paddingHorizontal: 32, | ||
| }, | ||
| tileContainer: { | ||
| width: '20%', | ||
| margin: '1%', | ||
| borderRadius: 16, | ||
| minHeight: 80, | ||
| justifyContent: 'center', | ||
| alignItems: 'center', | ||
| }, | ||
| tile: { | ||
| flex: 1, | ||
| height: '100%', | ||
| width: ' 100%', | ||
| justifyContent: 'center', | ||
| alignItems: 'center', | ||
| }, | ||
| tileLabel: { | ||
| color: '#f8f9ff', | ||
| fontSize: 24, | ||
| }, | ||
| title: { | ||
| fontSize: 18, | ||
| color: '#001a72', | ||
| textAlign: 'center', | ||
| marginVertical: 16, | ||
| }, | ||
| wrapper: { | ||
| width: '100%', | ||
| position: 'absolute', | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| }, | ||
| animatedView: { | ||
| width: '100%', | ||
| overflow: 'hidden', | ||
| }, | ||
| homeContainer: { | ||
| flex: 1, | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| gap: 16, | ||
| }, | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.