Skip to content

Commit

Permalink
[core] Improve the pointer event logic
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Dec 28, 2019
1 parent f3218f2 commit e86b7b3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 42 deletions.
43 changes: 19 additions & 24 deletions packages/material-ui/src/Slider/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import withStyles from '../styles/withStyles';
import useTheme from '../styles/useTheme';
import { fade, lighten, darken } from '../styles/colorManipulator';
import { useIsFocusVisible } from '../utils/focusVisible';
import ownerDocument from '../utils/ownerDocument';
import useEventCallback from '../utils/useEventCallback';
import useForkRef from '../utils/useForkRef';
import capitalize from '../utils/capitalize';
Expand Down Expand Up @@ -616,20 +617,13 @@ const Slider = React.forwardRef(function Slider(props, ref) {
}

touchId.current = undefined;
document.body.removeEventListener('mousemove', handleTouchMove);
document.body.removeEventListener('mouseup', handleTouchEnd);
// eslint-disable-next-line no-use-before-define
document.body.removeEventListener('mouseenter', handleMouseEnter);
document.body.removeEventListener('touchmove', handleTouchMove);
document.body.removeEventListener('touchend', handleTouchEnd);
});

const handleMouseEnter = useEventCallback(event => {
// If the slider was being interacted with but the mouse went off the window
// and then re-entered while unclicked then end the interaction.
if (event.buttons === 0) {
handleTouchEnd(event);
}
const doc = ownerDocument(sliderRef.current);
doc.removeEventListener('mousemove', handleTouchMove);
doc.removeEventListener('mouseup', handleTouchEnd);
// eslint-disable-next-line no-use-before-define
doc.removeEventListener('touchmove', handleTouchMove);
doc.removeEventListener('touchend', handleTouchEnd);
});

const handleTouchStart = useEventCallback(event => {
Expand All @@ -651,23 +645,24 @@ const Slider = React.forwardRef(function Slider(props, ref) {
onChange(event, newValue);
}

document.body.addEventListener('touchmove', handleTouchMove);
document.body.addEventListener('touchend', handleTouchEnd);
const doc = ownerDocument(sliderRef.current);
doc.addEventListener('touchmove', handleTouchMove);
doc.addEventListener('touchend', handleTouchEnd);
});

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

return () => {
slider.removeEventListener('touchstart', handleTouchStart);
document.body.removeEventListener('mousemove', handleTouchMove);
document.body.removeEventListener('mouseup', handleTouchEnd);
document.body.removeEventListener('mouseenter', handleMouseEnter);
document.body.removeEventListener('touchmove', handleTouchMove);
document.body.removeEventListener('touchend', handleTouchEnd);
doc.removeEventListener('mousemove', handleTouchMove);
doc.removeEventListener('mouseup', handleTouchEnd);
doc.removeEventListener('touchmove', handleTouchMove);
doc.removeEventListener('touchend', handleTouchEnd);
};
}, [handleMouseEnter, handleTouchEnd, handleTouchMove, handleTouchStart]);
}, [handleTouchEnd, handleTouchMove, handleTouchStart]);

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
Expand Down Expand Up @@ -705,9 +700,9 @@ const Slider = React.forwardRef(function Slider(props, ref) {
onChange(event, newValue);
}

document.body.addEventListener('mousemove', handleTouchMove);
document.body.addEventListener('mouseenter', handleMouseEnter);
document.body.addEventListener('mouseup', handleTouchEnd);
const doc = ownerDocument(sliderRef.current);
doc.addEventListener('mousemove', handleTouchMove);
doc.addEventListener('mouseup', handleTouchEnd);
});

const trackOffset = valueToPercent(range ? values[0] : min, min, max);
Expand Down
14 changes: 8 additions & 6 deletions packages/material-ui/src/SwipeableDrawer/SwipeableDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import { elementTypeAcceptingRef } from '@material-ui/utils';
import Drawer, { getAnchor, isHorizontal } from '../Drawer/Drawer';
import ownerDocument from '../utils/ownerDocument';
import useEventCallback from '../utils/useEventCallback';
import { duration } from '../styles/transitions';
import useTheme from '../styles/useTheme';
Expand Down Expand Up @@ -467,14 +468,15 @@ const SwipeableDrawer = React.forwardRef(function SwipeableDrawer(props, ref) {

React.useEffect(() => {
if (variant === 'temporary') {
document.body.addEventListener('touchstart', handleBodyTouchStart);
document.body.addEventListener('touchmove', handleBodyTouchMove, { passive: false });
document.body.addEventListener('touchend', handleBodyTouchEnd);
const doc = ownerDocument(paperRef.current);
doc.addEventListener('touchstart', handleBodyTouchStart);
doc.addEventListener('touchmove', handleBodyTouchMove, { passive: false });
doc.addEventListener('touchend', handleBodyTouchEnd);

return () => {
document.body.removeEventListener('touchstart', handleBodyTouchStart);
document.body.removeEventListener('touchmove', handleBodyTouchMove, { passive: false });
document.body.removeEventListener('touchend', handleBodyTouchEnd);
doc.removeEventListener('touchstart', handleBodyTouchStart);
doc.removeEventListener('touchmove', handleBodyTouchMove, { passive: false });
doc.removeEventListener('touchend', handleBodyTouchEnd);
};
}

Expand Down
24 changes: 12 additions & 12 deletions packages/material-ui/src/utils/focusVisible.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,20 @@ function handleVisibilityChange() {
}
}

function prepare(ownerDocument) {
ownerDocument.addEventListener('keydown', handleKeyDown, true);
ownerDocument.addEventListener('mousedown', handlePointerDown, true);
ownerDocument.addEventListener('pointerdown', handlePointerDown, true);
ownerDocument.addEventListener('touchstart', handlePointerDown, true);
ownerDocument.addEventListener('visibilitychange', handleVisibilityChange, true);
function prepare(doc) {
doc.addEventListener('keydown', handleKeyDown, true);
doc.addEventListener('mousedown', handlePointerDown, true);
doc.addEventListener('pointerdown', handlePointerDown, true);
doc.addEventListener('touchstart', handlePointerDown, true);
doc.addEventListener('visibilitychange', handleVisibilityChange, true);
}

export function teardown(ownerDocument) {
ownerDocument.removeEventListener('keydown', handleKeyDown, true);
ownerDocument.removeEventListener('mousedown', handlePointerDown, true);
ownerDocument.removeEventListener('pointerdown', handlePointerDown, true);
ownerDocument.removeEventListener('touchstart', handlePointerDown, true);
ownerDocument.removeEventListener('visibilitychange', handleVisibilityChange, true);
export function teardown(doc) {
doc.removeEventListener('keydown', handleKeyDown, true);
doc.removeEventListener('mousedown', handlePointerDown, true);
doc.removeEventListener('pointerdown', handlePointerDown, true);
doc.removeEventListener('touchstart', handlePointerDown, true);
doc.removeEventListener('visibilitychange', handleVisibilityChange, true);
}

function isFocusVisible(event) {
Expand Down

0 comments on commit e86b7b3

Please sign in to comment.