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

Cancel outside click behavior on touch devices when scrolling #3266

Merged
merged 6 commits into from
Jun 3, 2024

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Jun 3, 2024

This PR improves the outside click behavior on touch devices.

Before this change, if a <Menu /> or <Dialog /> was open, then "scrolling" or "swiping" your finger on the screen would close the <Menu /> or <Dialog /> respectively.

This is because we also checked for touchend events (which are fired when you lift your finger). Right now, we will make sure that your finger has to move at least some amount (set it to 30px for now, which felt good while testing on mobile devices) in order to cancel the outside click behaviour. If you didn't move within 30px then we consider it an outside click.

Now, scrolling doesn't close the component, but a tap (touchend) will close it as expected.

Fixes: #3038

When on a touch device, then the `touchend` event will fire, even if you
scrolled a bit and scrolling was your intention.

This now tracks that touches were at least 30px apart in either the X or
Y direction. If that's the case, then we do not consider it an outside
click.
This now takes the new `enabled` value into account.
Copy link

vercel bot commented Jun 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
headlessui-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 3, 2024 11:25am
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 3, 2024 11:25am

@RobinMalfait RobinMalfait changed the title Improve outside click on touch devices Cancel outside click behavior on touch devices when scrolling Jun 3, 2024
Comment on lines -21 to -32
useEffect(
process.env.NODE_ENV === 'test'
? () => {
enabledRef.current = isTopLayer
}
: () => {
requestAnimationFrame(() => {
enabledRef.current = isTopLayer
})
},
[isTopLayer]
)
Copy link
Member Author

Choose a reason for hiding this comment

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

The linked issue was expected behavior, since then we've also restructured some of this logic and this doesn't happen anymore.

The issue we were seeing was that opening a <Dialog /> by setting state in an onClick, registered a click listener on the window and immediately triggered it so the <Dialog /> was immediately closed.

Comment on lines +27 to +34
"vue": "^3.4.27",
"vue-flatpickr-component": "^9.0.5",
"vue-router": "^4.0.0"
"vue-router": "^4.3.2"
},
"devDependencies": {
"@floating-ui/vue": "^1.0.2",
"@vitejs/plugin-vue": "^3.0.3",
"vite": "^3.0.0"
"@vitejs/plugin-vue": "^5.0.5",
"vite": "^5.2.12"
Copy link
Member Author

Choose a reason for hiding this comment

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

Noticed that playgrounds for Vue were broken, bumping these dependencies fixed the playground again.

@RobinMalfait RobinMalfait marked this pull request as ready for review June 3, 2024 12:43
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.

Mobile - Dropdowns automatically close when scrolling outside of the dropdown element.
2 participants