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

native: simulate pressable event bubbling #4204

Merged
merged 2 commits into from
Nov 25, 2024

Conversation

davidisaaclee
Copy link
Contributor

To preface: I don't know if this is a great thing to add to the Pressable primitive – opening this for discussion. Alternatives include (1) plumbing a disabled prop to ChatMessage for my one use case, (2) adding a flag to Pressable to opt-in to this behavior, or (3) creating a new Pressable-like component with this behavior (OptionallyPressable? 🤷 ).


I was trying to add a long-press to a container view holding a ChatMessage using Pressable. My handler wouldn't get called – swapping out ChatMessage for a blank View worked.

ChatMessage has a Pressable which has no bound handlers in this scenario, and I expected events to bubble up the stack like on web – but it looks like RN doesn't have that behavior. Here's a recording of the fixture from this PR demonstrating this – the specific case is when Enable inner press handler is off and I'm tapping on the inner view – I expect that the inner view does not activate (because no handler) and the outer view's handler does activate (since I'm tapping over the outer view):

passthru-pressable-before.mov

To solve my initial problem:

  1. If I could "reach across" to the ChatMessage's Pressable, setting pointerEvents="none" would work
  2. Setting disabled on the inner Pressable passes events through to ancestors

(1) seems violent to do for every use case – I'd prefer a component that can be optionally interactive, and when the component is configured to be non-interactive, events naturally pass through to ancestors.

6847213 implements (2) – setting disabled={true} when there are no bound handlers.

passthru-pressable-after.mov

I think there's a solution here if we were to move to react-native-gesture-handler instead of the Tamagui pressable mixin, but I don't think it'd be very different in implementation – I think we'd still be disabling interaction when missing handlers. (There are APIs for composing gestures but they require references to the relevant gestures, which would be a lot of plumbing.)

Copy link
Member

@latter-bolden latter-bolden left a comment

Choose a reason for hiding this comment

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

This was definitely a point of confusion for me when first working with RN. The approach you implemented feels sensible, I think it will be less surprising to have the clicks bubble by default. Curious if others have thoughts!

We should however make sure this doesn't behave strangely anywhere in existing code. Would prefer to avoid merging until after we cut this week's release.

@dnbrwstr
Copy link
Contributor

Makes sense to me -- @patosullivan could you take a look? As you've spent time in pressable most recently.

@davidisaaclee
Copy link
Contributor Author

wanted to add more context that I realized was missing:

  • This is not really bubbling, since we can't receive the press event on both the outer and inner views (i.o.w. we can't optionally preventDefault) – this is more about treating an inert Pressable as any other non-interactive view
  • I'd be open to any alternative solutions to my initial issue – I just couldn't think of any apart from the ones I posted in OP

Copy link
Member

@patosullivan patosullivan left a comment

Choose a reason for hiding this comment

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

Makes sense to me!

@davidisaaclee
Copy link
Contributor Author

great – I'll merge this on Friday afternoon after the build review

Copy link
Contributor

@dnbrwstr dnbrwstr left a comment

Choose a reason for hiding this comment

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

Lgtm when you're ready to merge!

@davidisaaclee davidisaaclee merged commit 26b1645 into develop Nov 25, 2024
1 check passed
@davidisaaclee davidisaaclee deleted the dil/passthrough-unbound-pressable branch November 25, 2024 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants