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 7 commits
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
35 changes: 31 additions & 4 deletions packages/material-ui/src/Slider/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ const axisProps = {

const Identity = (x) => x;

// TODO: remove support for Safari < 13.
Copy link
Member

Choose a reason for hiding this comment

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

Aside: I really need to get an implementation working with pointer events. With those we don't need touchAction: none if I remember correctly.

Copy link
Member

Choose a reason for hiding this comment

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

Would we need to drop the support of older phones if we move to pointer events?

Copy link
Member

Choose a reason for hiding this comment

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

I don't know what phones we're supporting. But even our current browserslist would match

// https://caniuse.com/#search=touch-action
//
// 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.
let cachedSupportsTouchActionNone;
function doesSupportTouchActionNone() {
if (cachedSupportsTouchActionNone === undefined) {
const element = document.createElement('div');
element.style.touchAction = 'none';
document.body.appendChild(element);
cachedSupportsTouchActionNone = window.getComputedStyle(element).touchAction === 'none';
element.parentElement.removeChild(element);
}
return cachedSupportsTouchActionNone;
}

export const styles = (theme) => ({
/* Styles applied to the root element. */
root: {
Expand All @@ -136,6 +153,7 @@ export const styles = (theme) => ({
display: 'inline-block',
position: 'relative',
cursor: 'pointer',
// Disable scroll capabilities.
touchAction: 'none',
color: theme.palette.primary.main,
WebkitTapHighlightColor: 'transparent',
Expand Down Expand Up @@ -615,8 +633,11 @@ const Slider = React.forwardRef(function Slider(props, ref) {
});

const handleTouchStart = useEventCallback((event) => {
// Workaround as Safari has partial support for touchAction: 'none'.
event.preventDefault();
// If touch-action: none; is not supported we need to prevent the scroll manually.
if (!doesSupportTouchActionNone()) {
event.preventDefault();
}

const touch = event.changedTouches[0];
if (touch != null) {
// A number that uniquely identifies the current finger in the touch session.
Expand All @@ -639,11 +660,17 @@ const Slider = React.forwardRef(function Slider(props, ref) {

React.useEffect(() => {
const { current: slider } = sliderRef;
slider.addEventListener('touchstart', handleTouchStart);
slider.addEventListener('touchstart', handleTouchStart, {
passive: doesSupportTouchActionNone(),
});

const doc = ownerDocument(slider);

return () => {
slider.removeEventListener('touchstart', handleTouchStart);
slider.removeEventListener('touchstart', handleTouchStart, {
passive: doesSupportTouchActionNone(),
});

doc.removeEventListener('mousemove', handleTouchMove);
doc.removeEventListener('mouseup', handleTouchEnd);
doc.removeEventListener('touchmove', handleTouchMove);
Expand Down