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

[Slider] Improve touch passive event handling #22269

Merged
merged 8 commits into from
Aug 22, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/material-ui/src/Slider/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ const axisProps = {
const Identity = (x) => x;

const iOS = typeof navigator !== 'undefined' && /iPad|iPhone|iPod/.test(navigator.userAgent);
// Approximate the support of touch-action to iOS.
// iOS support touch action since v13 which as over 80% of marketing
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
// in August 2020.
// Because the passive vs non passive event doesn't make a significant difference
// We can keep this logic until we increase the minimum supported version of Safari to v13.
const touchActionSupport = !iOS;

export const styles = (theme) => ({
/* Styles applied to the root element. */
Expand Down Expand Up @@ -618,8 +624,7 @@ const Slider = React.forwardRef(function Slider(props, ref) {
});

const handleTouchStart = useEventCallback((event) => {
if (event.cancelable && iOS) {
// Workaround as Safari has partial support for touchAction: 'none'.
if (!touchActionSupport) {
Copy link
Member

Choose a reason for hiding this comment

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

Why not just check event.cancelable? That way we don't accidentally leave this untouch if we change when we add passive listeners or not.

Copy link
Member

Choose a reason for hiding this comment

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

We get a warning in Chrome if we don't do so.

document this please

Copy link
Member

Choose a reason for hiding this comment

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

And we should investigate it further. This doesn't seem right.

Copy link
Member

@oliviertassinari oliviertassinari Aug 19, 2020

Choose a reason for hiding this comment

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

And we should investigate it further. This doesn't seem right.

Agree, it doesn't seem right, but I don't think that we should care either. Will it make a difference?

event.preventDefault();
}
const touch = event.changedTouches[0];
Expand All @@ -646,14 +651,13 @@ const Slider = React.forwardRef(function Slider(props, ref) {
const { current: slider } = sliderRef;
// https://caniuse.com/#search=touch-action
slider.addEventListener('touchstart', handleTouchStart, {
// Workaround as Safari has partial support for touchAction: 'none'.
passive: !iOS,
passive: touchActionSupport,
});

const doc = ownerDocument(slider);

return () => {
slider.removeEventListener('touchstart', handleTouchStart, { passive: !iOS });
slider.removeEventListener('touchstart', handleTouchStart, { passive: touchActionSupport });
doc.removeEventListener('mousemove', handleTouchMove);
doc.removeEventListener('mouseup', handleTouchEnd);
doc.removeEventListener('touchmove', handleTouchMove);
Expand Down