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 24 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 ManualGestures from './src/new_api/manualGestures/index';
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/Swipeable';

import EmptyExample from './src/empty/EmptyExample';
import RectButtonBorders from './src/release_tests/rectButton';
Expand Down Expand Up @@ -160,6 +161,7 @@ const EXAMPLES: ExamplesSection[] = [
{ 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
127 changes: 127 additions & 0 deletions example/src/new_api/swipeable/AppleStyleSwipeableRow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import React, { Component, PropsWithChildren } from 'react';
import { Animated, StyleSheet, Text, View, I18nManager } from 'react-native';

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

export default class AppleStyleSwipeableRow extends Component<
PropsWithChildren<unknown>
> {
private renderLeftActions = (
_progress: SharedValue<number>,
dragX: SharedValue<number>
) => {
const trans = interpolate(
dragX.value,
[0, 50, 100, 101],
[-20, 0, 0, 1],
Extrapolation.CLAMP
);
return (
<RectButton style={styles.leftAction} onPress={this.close}>
<Animated.Text
style={[
styles.actionText,
{
transform: [{ translateX: trans }],
},
]}>
Archive
</Animated.Text>
</RectButton>
);
};

private renderRightAction = (
text: string,
color: string,
x: number,
progress: SharedValue<number>
) => {
const trans = interpolate(progress.value, [0, 1], [x, 0]);
const pressHandler = () => {
this.close();
// eslint-disable-next-line no-alert
window.alert(text);
};

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

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

private swipeableRow?: SwipeableRef;

private updateRef = (ref: SwipeableRef) => {
this.swipeableRow = ref;
};
private close = () => {
this.swipeableRow?.close();
};
render() {
const { children } = this.props;
return (
<Swipeable
ref={this.updateRef}
friction={2}
enableTrackpadTwoFingerGesture
leftThreshold={30}
rightThreshold={40}
renderLeftActions={this.renderLeftActions}
renderRightActions={this.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: 10,
},
rightAction: {
alignItems: 'center',
flex: 1,
justifyContent: 'center',
},
});
101 changes: 101 additions & 0 deletions example/src/new_api/swipeable/GmailStyleSwipeableRow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React, { Component, PropsWithChildren } from 'react';
import { StyleSheet, I18nManager } from 'react-native';

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

export default class GmailStyleSwipeableRow extends Component<
PropsWithChildren<unknown>
> {
private renderLeftActions = (
_progress: SharedValue<number>,
dragX: SharedValue<number>
) => {
const scale = interpolate(
dragX.value,
[0, 80],
[0, 1],
Extrapolation.CLAMP
);
return (
<RectButton style={styles.leftAction} onPress={this.close}>
{/* Change it to some icons */}
<Animated.View
style={[styles.actionIcon, { transform: [{ scale }] }]}
/>
</RectButton>
);
};
private renderRightActions = (
_progress: SharedValue<number>,
dragX: SharedValue<number>
) => {
const scale = interpolate(
dragX.value,
[-80, 0],
[1, 0],
Extrapolation.CLAMP
);

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

private swipeableRow?: SwipeableRef;

private updateRef = (ref: SwipeableRef) => {
this.swipeableRow = ref;
};
private close = () => {
this.swipeableRow?.close();
};
render() {
const { children } = this.props;
return (
<Swipeable
ref={this.updateRef}
friction={2}
leftThreshold={80}
enableTrackpadTwoFingerGesture
rightThreshold={40}
renderLeftActions={this.renderLeftActions}
renderRightActions={this.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