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 all 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
36 changes: 32 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,24 @@ 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
//
// Safari, on iOS, supports touch action since v13.
// Over 80% of the iOS phones are compatible
// 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 +154,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 +634,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 +661,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