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 4 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
18 changes: 14 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,8 @@ const axisProps = {

const Identity = (x) => x;

const iOS = typeof navigator !== 'undefined' && /iPad|iPhone|iPod/.test(navigator.userAgent);

export const styles = (theme) => ({
/* Styles applied to the root element. */
root: {
Expand All @@ -136,6 +138,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 +618,10 @@ const Slider = React.forwardRef(function Slider(props, ref) {
});

const handleTouchStart = useEventCallback((event) => {
// Workaround as Safari has partial support for touchAction: 'none'.
event.preventDefault();
if (event.cancelable && iOS) {
// Workaround as Safari has partial support for touchAction: 'none'.
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 +644,16 @@ const Slider = React.forwardRef(function Slider(props, ref) {

React.useEffect(() => {
const { current: slider } = sliderRef;
slider.addEventListener('touchstart', handleTouchStart);
// https://caniuse.com/#search=touch-action
slider.addEventListener('touchstart', handleTouchStart, {
// Workaround as Safari has partial support for touchAction: 'none'.
passive: !iOS,
});

const doc = ownerDocument(slider);

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