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

refactor: Remove custom hit testing in usePress #7427

Merged
merged 4 commits into from
Nov 27, 2024
Merged

Conversation

devongovett
Copy link
Member

Related to #1720, #2506, #5290

Back in #62 we implemented our own hit testing in usePress in order to work around a bug in pointer events in iOS 13. Now that this version is long gone, we can probably remove this and use the onPointerLeave and onPointerEnter events instead. This depends on releasePointerCapture being called during onPointerDown on touch devices, so that the original target doesn't capture the pointer and cause the browser to retarget all events to it even outside the hit area.

// Release pointer capture so that touch interactions can leave the original target.
// This enables onPointerLeave and onPointerEnter to fire.
let target = e.target as Element;
if ('releasePointerCapture' in target) {
Copy link
Member Author

Choose a reason for hiding this comment

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

not supported in jsdom

// Safari on iOS sometimes fires pointerup events, even
// when the touch isn't over the target, so double check.
if (e.button === 0 && isOverTarget(e, e.currentTarget)) {
if (e.button === 0) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not certain yet if this bug still exists in Safari. This was added in #335. Will need to test.

@rspbot
Copy link

rspbot commented Nov 22, 2024

snowystinger
snowystinger previously approved these changes Nov 26, 2024
Copy link
Member

@snowystinger snowystinger left a comment

Choose a reason for hiding this comment

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

Excited to get this into testing!

@devongovett
Copy link
Member Author

We tested it yesterday! Got a few fixes coming.

@devongovett
Copy link
Member Author

Updated to address two issues found in testing:

On touch (ipad), open any sub menu and press down on a submenu item, without lifting, slide off the submenu and release. Menus all close now. The close behavior exists already with mouse.

This only happens in RSP v3, not in RAC, because we recently changed the DOM structure to no longer be nested there. In RSP we have an extra useInteractOutside in Menu, but it only checks if the interaction was outside the menu itself not any submenus. It checks that during onInteractOutside but not in the pointer down event. I moved this to the root menu trigger instead so submenus should be included.

On ipad, starting a touch event on one item and then dragging and releasing of a different item selects that item. Doesn't happen on Android and doesn't happen if the dropdown is scrollable. Didn't happen on main previously, same issue as row 7. Also occurs for Menu (DL)

This works as intended with mouse (matching native behavior), but it is weird with touch since the user might be trying to scroll. Updated this logic to only select during onPressUp if pointerType is mouse, otherwise wait for onPress which requires pressing down on the same target your release over.

@rspbot
Copy link

rspbot commented Nov 27, 2024

Copy link
Member

@snowystinger snowystinger left a comment

Choose a reason for hiding this comment

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

submenus appear to be working correctly in s1 and s2

Copy link
Member

@LFDanLu LFDanLu left a comment

Choose a reason for hiding this comment

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

LGTM, verified the menu/pickers/etc don't auto close on touch when attempting to scroll. Also verified submenus don't close when dragging off of them in touch anymore

@devongovett devongovett added this pull request to the merge queue Nov 27, 2024
Merged via the queue into main with commit b0f1569 Nov 27, 2024
30 checks passed
@devongovett devongovett deleted the usepress-hit-testing branch November 27, 2024 23:01
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