-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(ios): publish open swipeable frame for native-stack pop arbitration #4157
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
Open
Jasleen-Kaur96
wants to merge
2
commits into
software-mansion:main
Choose a base branch
from
Jasleen-Kaur96:feat/swipeable-pop-gesture-coordination
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+76
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| import type { ForwardedRef } from 'react'; | ||
| import { useCallback, useImperativeHandle, useMemo } from 'react'; | ||
| import { | ||
| useCallback, | ||
| useEffect, | ||
| useImperativeHandle, | ||
| useMemo, | ||
| useRef, | ||
| } from 'react'; | ||
| import type { LayoutChangeEvent } from 'react-native'; | ||
| import { I18nManager, StyleSheet, View } from 'react-native'; | ||
| import { I18nManager, NativeModules, StyleSheet, View } from 'react-native'; | ||
| import Animated, { | ||
| interpolate, | ||
| measure, | ||
|
|
@@ -31,6 +37,53 @@ const DEFAULT_DRAG_OFFSET = 10; | |
| const DEFAULT_ENABLE_TRACKING_TWO_FINGER_GESTURE = false; | ||
|
|
||
| const Swipeable = (props: SwipeableProps) => { | ||
| const containerRef = useRef<View>(null); | ||
| const isOpenRef = useRef(false); | ||
| const swipeableRegistry = NativeModules.RNSSwipeableRegistry as | ||
| | { | ||
| setOpenSwipeableFrame?: ( | ||
| x: number, | ||
| y: number, | ||
| width: number, | ||
| height: number | ||
| ) => void; | ||
| clearOpenSwipeable?: () => void; | ||
| } | ||
| | undefined; | ||
|
|
||
| const syncOpenSwipeableFrame = useCallback(() => { | ||
| const setOpenSwipeableFrame = swipeableRegistry?.setOpenSwipeableFrame; | ||
| if (!containerRef.current || !setOpenSwipeableFrame) { | ||
| return; | ||
| } | ||
|
|
||
| containerRef.current.measureInWindow( | ||
| (x: number, y: number, width: number, height: number) => { | ||
| setOpenSwipeableFrame(x, y, width, height); | ||
| } | ||
| ); | ||
| }, [swipeableRegistry]); | ||
|
|
||
| // Current coordination stores one active open swipeable frame. | ||
| // Multi-open ownership scoping (token/reactTag) can be added in follow-up. | ||
| const clearOpenSwipeableFrame = useCallback(() => { | ||
| const clearOpenSwipeable = swipeableRegistry?.clearOpenSwipeable; | ||
| if (!clearOpenSwipeable) { | ||
| return; | ||
| } | ||
| clearOpenSwipeable(); | ||
| }, [swipeableRegistry]); | ||
|
|
||
| const markOpen = useCallback(() => { | ||
| isOpenRef.current = true; | ||
| syncOpenSwipeableFrame(); | ||
| }, [syncOpenSwipeableFrame]); | ||
|
|
||
| const markClosed = useCallback(() => { | ||
| isOpenRef.current = false; | ||
| clearOpenSwipeableFrame(); | ||
| }, [clearOpenSwipeableFrame]); | ||
|
Comment on lines
+77
to
+85
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great point — agreed. reset() bypasses animateRow, so I added runOnJS(markClosed)() in reset() and updated dependencies accordingly. This clears isOpenRef and native registry state to avoid stale gesture arbitration after imperative resets. |
||
|
|
||
| const { | ||
| ref, | ||
| leftThreshold, | ||
|
|
@@ -154,8 +207,14 @@ const Swipeable = (props: SwipeableProps) => { | |
| fromValue > 0 ? SwipeDirection.LEFT : SwipeDirection.RIGHT | ||
| ); | ||
| } | ||
|
|
||
| if (toValue !== 0) { | ||
| runOnJS(markOpen)(); | ||
| } else { | ||
| runOnJS(markClosed)(); | ||
| } | ||
| }, | ||
| [onSwipeableWillClose, onSwipeableWillOpen] | ||
| [markClosed, markOpen, onSwipeableWillClose, onSwipeableWillOpen] | ||
| ); | ||
|
|
||
| const dispatchEndEvents = useCallback( | ||
|
|
@@ -320,6 +379,7 @@ const Swipeable = (props: SwipeableProps) => { | |
| showLeftProgress.value = 0; | ||
| appliedTranslation.value = 0; | ||
| rowState.value = 0; | ||
| runOnJS(markClosed)(); | ||
| }, | ||
| }), | ||
| [ | ||
|
|
@@ -337,10 +397,21 @@ const Swipeable = (props: SwipeableProps) => { | |
| const onRowLayout = useCallback( | ||
| ({ nativeEvent }: LayoutChangeEvent) => { | ||
| rowWidth.value = nativeEvent.layout.width; | ||
| if (isOpenRef.current) { | ||
| syncOpenSwipeableFrame(); | ||
| } | ||
| }, | ||
| [rowWidth] | ||
| [rowWidth, syncOpenSwipeableFrame] | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| if (isOpenRef.current) { | ||
| clearOpenSwipeableFrame(); | ||
| } | ||
| }; | ||
| }, [clearOpenSwipeableFrame]); | ||
|
|
||
| // As stated in `Dimensions.get` docstring, this function should be called on every render | ||
| // since dimensions may change (e.g. orientation change) | ||
|
|
||
|
|
@@ -523,6 +594,7 @@ const Swipeable = (props: SwipeableProps) => { | |
| const swipeableComponent = ( | ||
| <GestureDetector gesture={panGesture} touchAction="pan-y"> | ||
| <Animated.View | ||
| ref={containerRef} | ||
| {...remainingProps} | ||
| onLayout={onRowLayout} | ||
| style={[styles.container, containerStyle]}> | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point — the current registry is intentionally minimal and stores a single active open-swipeable frame.
For this first iteration, the goal is to establish native arbitration and validate behavior for the common pattern where only one row is open at a time.
I agree multi-open scenarios need stronger ownership semantics (e.g. token/reactTag-scoped set/clear), and I can follow up with a dedicated change once this direction is accepted.