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

[docs] Improve Reanimated Swipeable documentation. #3121

Merged
merged 16 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
123 changes: 62 additions & 61 deletions docs/docs/components/reanimated_swipeable.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,29 @@ an argument.

### `renderLeftActions`

method that is expected to return an action panel that is going to be revealed from the left side when user swipes right.
This map describes the values to use as inputRange for extra interpolation:
accepts a function which receives the following arguments:
latekvo marked this conversation as resolved.
Show resolved Hide resolved

SharedValue: [startValue, endValue]
- `progress` value representing swiping progress relative to the width of the returned element.
m-bert marked this conversation as resolved.
Show resolved Hide resolved
Equals `0` when `swipeable` is closed, `1` when left action is open.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Equals `0` when `swipeable` is closed, `1` when left action is open.
Equals `0` when `swipeable` is closed, `1` when `swipeable` is open.

Copy link
Contributor Author

@latekvo latekvo Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swipeable cannot be open, either the left action or the right action can be open.

Maybe it could be "open to the left" or "open to the right", but i think it's unnecessarily adding additional words, and i'm not sure if it resolves the issue you were referring to.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swipeable cannot be open

Why is that? For example, it has onSwipeableWillOpen and onSwipeableOpen props. What I would change here is I'd use passive voice, i.e.:

Suggested change
Equals `0` when `swipeable` is closed, `1` when left action is open.
Equals `0` when `swipeable` is closed, `1` when `swipeable` is opened.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onSwipeableWillOpen distinguishes between swipeable opening it's left and right element.
Here, the distinction of which side is open is equally necessary.

What about something like this?

Equals 0 when swipeable is closed, 1 when swipeable has its left element open.

or

Equals 0 when swipeable is closed, 1 when the left element is open.


element could be replaced with action, but I think you mentioned you'd prefer to avoid action here, was that a context-specific preference or a general rule?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, the distinction of which side is open is equally necessary.

Isn't it implied by the fact that we're in renderLeftActions section?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onSwipeableWillOpen distinguishes between swipeable opening it's left and right element.

Yes it does, but the name indicates that Swipeable itself will be opened. Why can't you say that it is opened if you can say that it is closed?

element could be replaced with action

I think the best we could replace it with would be action panel, as in tsdocs. But still, I believe that Swipeable fits better here. (cc @j-piasecki, @kacperkapusciak)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, if we want to avoid using terms like left/right action, since these are already implied by the function name, then standalone swipeable definitely fits best.

Fixed in df0ff8c throughout 9845471

Linearly increases to `Infinity` as left action overshoots it's open position.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Linearly increases to `Infinity` as left action overshoots it's open position.
When an element overshoots the opened position the value tends towards `Infinity`. Goes back to `1` when `swipeable` is released.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the second sentence is a good addition: Goes back to `1` when `swipeable` is released..
But for the first one, i think the original works a bit better, it's shorter and i think the wording it uses is a bit clearer

Perhaps the following would work better?

As left action progress overshoots it's open position, the value linearly increases to Infinity.
Reverts to 1 when swipeable is released.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We wanted to make it as close to natural sentence as possible. Also we'd like to avoid left action, that's why we used element

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We wanted to make it as close to natural sentence as possible. Also we'd like to avoid left action, that's why we used element

I believe this is resolved here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing something, or the only thing that has changed is left action?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing something, or the only thing that has changed is left action?

I'm not sure which PR you're referencing, in df0ff8c both left and right got replaced.

Are you talking about some PR between df0ff8c and 9845471?

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 620e247

- `translation` horizontal translation of the `swipeable` in pixels.
m-bert marked this conversation as resolved.
Show resolved Hide resolved
- `swipeable` provides an object exposing the methods listed [here](#methods)
m-bert marked this conversation as resolved.
Show resolved Hide resolved

progress: [0, 1]

drag: [0, +]
This function must return a `ReactNode`.

To support `rtl` flexbox layouts use `flexDirection` styling.

### `renderRightActions`

method that is expected to return an action panel that is going to be revealed from the right side when user swipes left.
This map describes the values to use as inputRange for extra interpolation:

SharedValue: [startValue, endValue]
accepts a function which receives the following arguments:

progress: [0, 1]
- `progress` value representing swiping progress relative to the width of the returned element.
Equals `0` when `swipeable` is closed, `1` when right action is open.
Linearly increases to `Infinity` as right action overshoots it's open position.
- `translation` horizontal translation of the `swipeable` in pixels.
- `swipeable` provides an object exposing the methods listed [here](#methods)

drag: [0, -]
This function must return a `ReactNode`.

To support `rtl` flexbox layouts use `flexDirection` styling.

Expand Down Expand Up @@ -158,62 +160,61 @@ Unlike method `close`, this method does not trigger any animation.

### Example:

See the [swipeable example](https://github.com/software-mansion/react-native-gesture-handler/blob/main/example/src/release_tests/swipeableReanimation/index.tsx) from GestureHandler Example App.
For a more in-depth presentation of differences between the new and the legacy implementations, see [swipeable example](https://github.com/software-mansion/react-native-gesture-handler/blob/main/example/src/release_tests/swipeableReanimation/index.tsx) from GestureHandler Example App.

```jsx
import React, { Component } from 'react';
import { Animated, StyleSheet, View } from 'react-native';
import { RectButton } from 'react-native-gesture-handler';
import Swipeable from 'react-native-gesture-handler/ReanimatedSwipeable';
import React from 'react';
import { Text, StyleSheet } from 'react-native';

import { GestureHandlerRootView } from 'react-native-gesture-handler';
import ReanimatedSwipeable from 'react-native-gesture-handler/ReanimatedSwipeable';
import Reanimated, {
SharedValue,
useAnimatedStyle,
} from 'react-native-reanimated';

function RightAction(prog: SharedValue<number>, drag: SharedValue<number>) {
const styleAnimation = useAnimatedStyle(() => {
console.log('showRightProgress:', prog.value);
console.log('appliedTranslation:', drag.value);

return {
transform: [{ translateX: drag.value + 50 }],
};
});

const LeftAction = ({ dragX, swipeableRef }: LeftActionsProps) => {
const animatedStyle = useAnimatedStyle(() => ({
transform: [
{
translateX: interpolate(
dragX.value,
[0, 50, 100, 101],
[-20, 0, 0, 1],
Extrapolation.CLAMP
),
},
],
}));
return (
<RectButton
style={{
flex: 1,
backgroundColor: '#497AFC',
justifyContent: 'center',
}}
onPress={() => swipeableRef.current!.close()}>
<Animated.Text>
Archive
</Animated.Text>
</RectButton>
<Reanimated.View style={styleAnimation}>
<Text style={styles.rightAction}>Text</Text>
</Reanimated.View>
);
};

const renderLeftActions = (
_progress: any,
translation: SharedValue<number>,
swipeableRef: React.RefObject<SwipeableMethods>
) => <LeftAction dragX={translation} swipeableRef={swipeableRef} />;

function AppleStyleSwipeableRow({ children }: AppleStyleSwipeableRowProps) {
const swipeableRow = useRef<SwipeableMethods>(null);
}

export default function Example() {
return (
<Swipeable
ref={swipeableRow}
friction={2}
enableTrackpadTwoFingerGesture
leftThreshold={30}
renderLeftActions={(_, progress) =>
renderLeftActions(_, progress, swipeableRow)
}>
<Text>Apple style swipeable</Text>
</Swipeable>
<GestureHandlerRootView>
<ReanimatedSwipeable
containerStyle={styles.swipeable}
friction={2}
enableTrackpadTwoFingerGesture
rightThreshold={40}
renderRightActions={RightAction}>
<Text>Swipe me!</Text>
</ReanimatedSwipeable>
</GestureHandlerRootView>
);
}

const styles = StyleSheet.create({
rightAction: { width: 50, height: 50, backgroundColor: 'purple' },
separator: {
width: '100%',
borderTopWidth: 1,
},
swipeable: {
height: 50,
backgroundColor: 'papayawhip',
alignItems: 'center',
},
});
```
25 changes: 11 additions & 14 deletions src/components/ReanimatedSwipeable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,31 +144,28 @@ export interface SwipeableProps
onSwipeableCloseStartDrag?: (direction: 'left' | 'right') => void;

/**
*
* This map describes the values to use as inputRange for extra interpolation:
* AnimatedValue: [startValue, endValue]
*
* progressAnimatedValue: [0, 1] dragAnimatedValue: [0, +]
* `progress`: Equals `0` when action is closed, `1` when action is open, linearly increases to `Infinity` as action overshoots it's open position.\
* `translation`: translation of the `swipeable` in pixels.
* `swipeable` provides an object exposing methods for controlling the `swipeable`
*
* To support `rtl` flexbox layouts use `flexDirection` styling.
* */
renderLeftActions?: (
progressAnimatedValue: SharedValue<number>,
dragAnimatedValue: SharedValue<number>,
progress: SharedValue<number>,
translation: SharedValue<number>,
swipeable: SwipeableMethods
) => React.ReactNode;

/**
*
* This map describes the values to use as inputRange for extra interpolation:
* AnimatedValue: [startValue, endValue]
*
* progressAnimatedValue: [0, 1] dragAnimatedValue: [0, -]
* `progress`: Equals `0` when action is closed, `1` when action is open, linearly increases to `Infinity` as action overshoots it's open position.\
* `translation`: translation of the `swipeable` in pixels.
* `swipeable` provides an object exposing methods for controlling the `swipeable`
*
* To support `rtl` flexbox layouts use `flexDirection` styling.
* */
renderRightActions?: (
progressAnimatedValue: SharedValue<number>,
dragAnimatedValue: SharedValue<number>,
progress: SharedValue<number>,
translation: SharedValue<number>,
swipeable: SwipeableMethods
) => React.ReactNode;

Expand Down
Loading