Skip to content
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

Add swipeable example rewritten to new API #2934

Merged
merged 59 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
bde7c7d
create new swipeable example
latekvo Jun 5, 2024
e0a094f
added and started working on Swipeable component
latekvo Jun 5, 2024
6517b31
cleanup
latekvo Jun 5, 2024
8c651ac
complete gesture handling with new api
latekvo Jun 5, 2024
bf608c0
cleanup example
latekvo Jun 5, 2024
a6bf9b0
cleanup example
latekvo Jun 5, 2024
c0866a0
resolve all invalid interpolation, addition, and similar
latekvo Jun 6, 2024
53028f6
expose SwipeableActions, remove last errors
latekvo Jun 6, 2024
f3e8034
fix invalid hook call
latekvo Jun 6, 2024
0041442
expose swipeable actions using imperative handles
latekvo Jun 6, 2024
2a468eb
fix incorrect parameters being provided to exposed callbacks
latekvo Jun 6, 2024
4a2d585
copy initially working example
latekvo Jun 6, 2024
6c4b85f
initial working Swipeable version
latekvo Jun 6, 2024
9c43699
adjust spring animation to match the original
latekvo Jun 6, 2024
d6bea33
first smoothly working version
latekvo Jun 6, 2024
be9499d
remove unnecessary states and cleanup
latekvo Jun 7, 2024
283ce0d
fix incorrect gesture handling
latekvo Jun 7, 2024
3bc0baf
fix closing from outside not reading state correctly
latekvo Jun 7, 2024
cb5375c
update examples
latekvo Jun 7, 2024
40ad611
fix typing and cleanup
latekvo Jun 7, 2024
586615c
drop deprecated callback props
latekvo Jun 7, 2024
911d98d
fix ref typing as per suggestion
latekvo Jun 7, 2024
d2eb0b2
simplify return statements
latekvo Jun 7, 2024
cf8c9a4
Merge branch 'main' into @latekvo/update_swipeable_to_new_api
latekvo Jun 7, 2024
ea04821
remove completed todos and other comments
latekvo Jun 7, 2024
57ef0b7
apply suggestions
latekvo Jun 7, 2024
1d2ae81
pass swipeable methods instead of ref to callback functions
latekvo Jun 7, 2024
1bf0f69
update gesture handling code
latekvo Jun 7, 2024
e72d778
memoize exposed interface
latekvo Jun 7, 2024
f1ac2ea
align code to strict mode, partially fix for android, improve default…
latekvo Jun 7, 2024
6e69bf7
fix issues with android
latekvo Jun 7, 2024
a0fd9d2
fix callbacks needing to be worklets
latekvo Jun 7, 2024
441e2b2
remove unnecessary useStates
latekvo Jun 7, 2024
70e9de5
remove unnecessary comments and code
latekvo Jun 7, 2024
dddb575
update example to render correctly
latekvo Jun 7, 2024
9ae4e7e
fix errors in examples
latekvo Jun 7, 2024
f4c3213
convert example to use function components
latekvo Jun 7, 2024
06e373f
fix tap not working, improve clarity
latekvo Jun 7, 2024
630ae9d
fix examples not being reactive
latekvo Jun 7, 2024
b4020f9
fix gmail example
latekvo Jun 7, 2024
a9e9600
fix apple example
latekvo Jun 7, 2024
7f74520
fix invalid runOnJS usage
latekvo Jun 7, 2024
7af0d73
workletize exposed functions
latekvo Jun 7, 2024
1728786
apply review suggestions
latekvo Jun 7, 2024
34cb0f7
fix dragging from side alignment issues, some jitter still persists
latekvo Jun 7, 2024
6c55770
apply renaming suggestion
latekvo Jun 7, 2024
7d913b4
rename variables to reflect their function and remove redundant ones
latekvo Jun 10, 2024
06c69d5
fix jumpiness and jitter
latekvo Jun 10, 2024
216bb07
fix animation jump
latekvo Jun 10, 2024
e938366
bump reanimated version for types
latekvo Jun 10, 2024
91ef9ea
bump version cont.
latekvo Jun 10, 2024
a852c6c
change apple example to improve reactiveness
latekvo Jun 10, 2024
7ab13d3
fix eslint for main swipeable file
latekvo Jun 10, 2024
dc433ed
fix apple example linter errors
latekvo Jun 10, 2024
d95a063
apply suggestions
latekvo Jun 10, 2024
ca09441
fix gmail example linting issues
latekvo Jun 10, 2024
7bdea90
fix incorrect closing cb calling
latekvo Jun 10, 2024
f73cfbf
fix text wrapping on mobile
latekvo Jun 10, 2024
d72a98b
fix text alignment in apple example
latekvo Jun 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import Hover from './src/new_api/hover';
import HoverableIcons from './src/new_api/hoverable_icons';
import VelocityTest from './src/new_api/velocityTest';
import Swipeable from 'src/new_api/swipeable';

import EmptyExample from './src/empty/EmptyExample';
import RectButtonBorders from './src/release_tests/rectButton';
Expand Down Expand Up @@ -160,6 +161,7 @@
{ name: 'Bottom Sheet', component: BottomSheetNewApi },
{ name: 'Chat Heads', component: ChatHeadsNewApi },
{ name: 'Drag and drop', component: DragNDrop },
{ name: 'Swipeable', component: Swipeable },
{
name: 'Horizontal Drawer (Reanimated 2 & RNGH 2)',
component: BetterHorizontalDrawer,
Expand Down Expand Up @@ -258,7 +260,7 @@
renderSectionHeader={({ section: { sectionTitle } }) => (
<Text style={styles.sectionTitle}>{sectionTitle}</Text>
)}
ItemSeparatorComponent={() => <View style={styles.separator} />}

Check warning on line 263 in example/App.tsx

View workflow job for this annotation

GitHub Actions / check (example)

Do not define components during render. React will see a new component type on every render and destroy the entire subtree’s DOM nodes and state (https://reactjs.org/docs/reconciliation.html#elements-of-different-types). Instead, move this component definition out of the parent component “MainScreen” and pass data as props. If you want to allow component creation in props, set allowAsProps option to true
/>
</SafeAreaView>
);
Expand Down
131 changes: 131 additions & 0 deletions example/src/new_api/swipeable/AppleStyleSwipeableRow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import React, { ReactNode, useRef } from 'react';
import { StyleSheet, Text, View, I18nManager } from 'react-native';

import { RectButton } from 'react-native-gesture-handler';
import Animated, {
Extrapolation,
SharedValue,
interpolate,
useAnimatedStyle,
} from 'react-native-reanimated';
import Swipeable, { SwipeableMethods } from 'src/new_api/swipeable/Swipeable';

interface AppleStyleSwipeableRowProps {
children?: ReactNode;
}

export default function AppleStyleSwipeableRow({
children,
}: AppleStyleSwipeableRowProps) {
const renderLeftActions = (
_progress: SharedValue<number>,
dragX: SharedValue<number>
) => {
const animatedStyle = useAnimatedStyle(() => ({

Check failure on line 24 in example/src/new_api/swipeable/AppleStyleSwipeableRow.tsx

View workflow job for this annotation

GitHub Actions / check (example)

React Hook "useAnimatedStyle" is called in function "renderLeftActions" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use"
transform: [
{
translateX: interpolate(
dragX.value,
[0, 50, 100, 101],
[-20, 0, 0, 1],
Extrapolation.CLAMP
),
},
],
}));
return (
<RectButton style={styles.leftAction} onPress={close}>
<Animated.Text style={[styles.actionText, animatedStyle]}>
Archive
</Animated.Text>
</RectButton>
);
};

const renderRightAction = (
text: string,
color: string,
x: number,
progress: SharedValue<number>,
totalWidth: number
) => {
const animatedStyle = useAnimatedStyle(() => ({

Check failure on line 52 in example/src/new_api/swipeable/AppleStyleSwipeableRow.tsx

View workflow job for this annotation

GitHub Actions / check (example)

React Hook "useAnimatedStyle" is called in function "renderRightAction" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use"
transform: [
{
translateX: interpolate(progress.value, [0, -totalWidth], [x, 0]),
},
],
}));
const pressHandler = () => {
close();
// eslint-disable-next-line no-alert
window.alert(text);
};

return (
<Animated.View style={[{ flex: 1 }, animatedStyle]}>
<RectButton
style={[styles.rightAction, { backgroundColor: color }]}
onPress={pressHandler}>
<Text style={styles.actionText}>{text}</Text>
</RectButton>
</Animated.View>
);
};

const renderRightActions = (_: any, progress: SharedValue<number>) => (
<View
style={{
width: 192,
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
}}>
{renderRightAction('More', '#C8C7CD', 192, progress, 192)}
{renderRightAction('Flag', '#ffab00', 128, progress, 192)}
{renderRightAction('More', '#dd2c00', 64, progress, 192)}
</View>
);

const swipeableRow = useRef<SwipeableMethods>(null);

const close = () => {
swipeableRow.current?.close();
};

return (
<Swipeable
ref={swipeableRow}
friction={2}
enableTrackpadTwoFingerGesture
leftThreshold={30}
rightThreshold={40}
renderLeftActions={renderLeftActions}
renderRightActions={renderRightActions}
onSwipeableOpen={(direction) => {
console.log(`Opening swipeable from the ${direction}`);
}}
onSwipeableClose={(direction) => {
console.log(`Closing swipeable to the ${direction}`);
}}>
{children}
</Swipeable>
);
}

const styles = StyleSheet.create({
leftAction: {
flex: 1,
backgroundColor: '#497AFC',
justifyContent: 'center',
},
actionText: {
color: 'white',
fontSize: 16,
backgroundColor: 'transparent',
padding: 20,
},
rightAction: {
alignItems: 'center',
flex: 1,
justifyContent: 'center',
},
});
106 changes: 106 additions & 0 deletions example/src/new_api/swipeable/GmailStyleSwipeableRow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React, { ReactNode, useRef } from 'react';
import { StyleSheet, I18nManager } from 'react-native';

import { RectButton } from 'react-native-gesture-handler';
import Animated, {
Extrapolation,
SharedValue,
interpolate,
useAnimatedStyle,
} from 'react-native-reanimated';
import Swipeable, { SwipeableMethods } from 'src/new_api/swipeable/Swipeable';

interface GmailStyleSwipeableRowProps {
children?: ReactNode;
}

export default function GmailStyleSwipeableRow({
children,
}: GmailStyleSwipeableRowProps) {
const renderLeftActions = (
_progress: SharedValue<number>,
dragX: SharedValue<number>
) => {
const animatedStyle = useAnimatedStyle(() => ({

Check failure on line 24 in example/src/new_api/swipeable/GmailStyleSwipeableRow.tsx

View workflow job for this annotation

GitHub Actions / check (example)

React Hook "useAnimatedStyle" is called in function "renderLeftActions" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use"
transform: [
{
scale: interpolate(dragX.value, [0, 80], [0, 1], Extrapolation.CLAMP),
},
],
}));

return (
<RectButton style={styles.leftAction} onPress={close}>
{/* Change it to some icons */}
<Animated.View style={[styles.actionIcon, animatedStyle]} />
</RectButton>
);
};

const renderRightActions = (
_progress: SharedValue<number>,
dragX: SharedValue<number>
) => {
const animatedStyle = useAnimatedStyle(() => ({

Check failure on line 44 in example/src/new_api/swipeable/GmailStyleSwipeableRow.tsx

View workflow job for this annotation

GitHub Actions / check (example)

React Hook "useAnimatedStyle" is called in function "renderRightActions" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use"
transform: [
{
scale: interpolate(
dragX.value,
[-80, 0],
[1, 0],
Extrapolation.CLAMP
),
},
],
}));

return (
<RectButton style={styles.rightAction} onPress={close}>
{/* Change it to some icons */}
<Animated.View style={[styles.actionIcon, animatedStyle]} />
</RectButton>
);
};

const swipeableRow = useRef<SwipeableMethods>(null);

const close = () => {
swipeableRow.current?.close();
};

return (
<Swipeable
ref={swipeableRow}
friction={2}
leftThreshold={80}
enableTrackpadTwoFingerGesture
rightThreshold={40}
renderLeftActions={renderLeftActions}
renderRightActions={renderRightActions}>
{children}
</Swipeable>
);
}

const styles = StyleSheet.create({
leftAction: {
flex: 1,
backgroundColor: '#388e3c',
justifyContent: 'flex-end',
alignItems: 'center',
flexDirection: I18nManager.isRTL ? 'row' : 'row-reverse',
},
actionIcon: {
width: 30,
marginHorizontal: 10,
backgroundColor: 'plum',
height: 20,
},
rightAction: {
alignItems: 'center',
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
backgroundColor: '#dd2c00',
flex: 1,
justifyContent: 'flex-end',
},
});
Loading
Loading