Update Swipeable types#4175
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the public TypeScript API for ReanimatedSwipeable to reflect that certain numeric configuration props may be provided as Reanimated SharedValue<number>, aligning the typings/docs with intended usage.
Changes:
- Expand
SwipeableProps.dragOffsetFromLeftandSwipeableProps.dragOffsetFromRightto acceptSharedValue<number>in addition tonumber. - Update the Reanimated Swipeable docs to reflect the updated prop types.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/react-native-gesture-handler/src/components/ReanimatedSwipeable/ReanimatedSwipeableProps.ts | Widens dragOffsetFromLeft/dragOffsetFromRight prop types to include SharedValue<number>. |
| packages/docs-gesture-handler/docs/components/reanimated_swipeable.mdx | Updates the documented TypeScript signatures for the same props. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (1)
packages/react-native-gesture-handler/src/components/ReanimatedSwipeable/ReanimatedSwipeable.tsx:489
activeOffsetXrequires the first tuple element to be <= 0 and the second to be >= 0 (validated byusePanGesturein DEV). Switching to[dragOffsetFromRight, dragOffsetFromLeft]changes the sign contract fordragOffsetFromRightcompared to the documented/default behavior ("Defaults to 10"), and will break existing callers passing a positive number. Prefer preserving the existing API (positive distance) and handling the negation internally in a way that supportsSharedValue, or update the default value + docs to explicitly require a non-positivedragOffsetFromRight.
const panGesture = usePanGesture({
enabled: enabled !== false,
enableTrackpadTwoFingerGesture: enableTrackpadTwoFingerGesture,
activeOffsetX: [dragOffsetFromRight, dragOffsetFromLeft],
simultaneousWith,
requireToFail,
| if (Reanimated?.isSharedValue<number>(dragOffsetFromRight)) { | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| Reanimated?.useDerivedValue(() => { | ||
| 'worklet'; | ||
| if (dragOffsetFromRight.value > 0) { | ||
| throw new Error( | ||
| tagMessage('dragOffsetFromRight should be non-positive.') | ||
| ); | ||
| } | ||
| return dragOffsetFromRight.value; | ||
| }); | ||
| } else { | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| useEffect(() => { | ||
| if ((dragOffsetFromRight as number) > 0) { | ||
| throw new Error( | ||
| tagMessage('dragOffsetFromRight should be non-positive.') | ||
| ); | ||
| } | ||
| }, [dragOffsetFromRight]); | ||
| } | ||
| } |
There was a problem hiding this comment.
I don't think this is correct. You can switch between a constant value and a constant.
This should be a single useEffect and if the value is a shared value, add a subscriber and remove it on cleanup.
There was a problem hiding this comment.
You can switch between a constant value and a constant.
You mean alternating between SharedValue and standard value? Isn't it weird use-case?
This should be a single useEffect and if the value is a shared value, add a subscriber and remove it on cleanup.
useDerivedValue was discussed with Reanimated team. But if you prefer manually adding listener than ok. wdyt?
There was a problem hiding this comment.
Isn't it weird use-case?
Yes, but it's valid and would crash now.
| useDerivedValue<T>( | ||
| updater: () => T, | ||
| dependencies?: readonly unknown[] | ||
| ): SharedValue<T>; // Not exactly, but we don't need that anyway |
There was a problem hiding this comment.
This will not be needed after that change.
There was a problem hiding this comment.
| 'worklet'; | ||
| if (maybeUnpackValue<number>(value) > 0) { | ||
| throw new Error( | ||
| tagMessage('dragOffsetFromRight should be non-negative.') |
There was a problem hiding this comment.
Is this not a contradiction? First check if the value is more than 0 and then throw an error that says its less than 0.
Description
Some of the props passed to
Swipeablecan beSharedValue. However, we do not allow that in types, so TypeScript throws errors.Thank you @coado with help ❤️
Test plan
Static checks