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

[SwipeableDrawer] Fix React 18 issues #34505

Merged
merged 7 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/mui-material/src/SwipeableDrawer/SwipeableDrawer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { flushSync } from 'react-dom';
import PropTypes from 'prop-types';
import { elementTypeAcceptingRef } from '@mui/utils';
import { useThemeProps } from '@mui/system';
Expand Down Expand Up @@ -234,7 +235,9 @@ const SwipeableDrawer = React.forwardRef(function SwipeableDrawer(inProps, ref)
}
claimedSwipeInstance = null;
touchDetected.current = false;
setMaybeSwiping(false);
flushSync(() => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Fixes the part of the issue where the partially opened drawer (> hysteresis) is not opening the drawer completely. Could probably go as a standalone fix.

setMaybeSwiping(false);
});

// The swipe wasn't started.
if (!swipeInstance.current.isSwiping) {
Expand Down Expand Up @@ -488,7 +491,10 @@ const SwipeableDrawer = React.forwardRef(function SwipeableDrawer(inProps, ref)
swipeInstance.current.startX = currentX;
swipeInstance.current.startY = currentY;

setMaybeSwiping(true);
flushSync(() => {
setMaybeSwiping(true);
});

if (!open && paperRef.current) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I assumed that here paperRef.current will be defined for sure because of the flushSync before, which would change the setMaybeSwiping state, which is used for setting the open prop on the Drawer, and hence the modal. However, this is not the case, so I needed to add the keepMounted prop on the ModalProps, to be sure that the ref would be assigned to a node.

Copy link
Contributor

Choose a reason for hiding this comment

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

We use ModalProps={{ keepMounted: true }} anyways at the moment

// The ref may be null when a parent component updates while swiping.
setPosition(
Expand Down Expand Up @@ -554,6 +560,11 @@ const SwipeableDrawer = React.forwardRef(function SwipeableDrawer(inProps, ref)
...BackdropProps,
ref: backdropRef,
},
// Ensures that paperRef.current will be defined inside the touch start event handler
// See https://github.com/mui/material-ui/issues/30414 for more information
...(variant === 'temporary' && {
keepMounted: true,
}),
...ModalPropsProp,
}}
hideBackdrop={hideBackdrop}
Expand Down
75 changes: 75 additions & 0 deletions packages/mui-material/src/SwipeableDrawer/SwipeableDrawer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,81 @@ describe('<SwipeableDrawer />', () => {
expect(handleClose.callCount).to.equal(1);
});

it('should open at correct position when swiping', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
// Need layout
this.skip();
}
const handleClose = spy();
const handleOpen = spy();
const { getByTestId, setProps } = render(
<SwipeableDrawer
anchor={params.anchor}
onOpen={handleOpen}
onClose={handleClose}
open={false}
PaperProps={{ component: FakePaper }}
transitionDuration={0}
>
<div data-testid="drawer">SwipeableDrawer</div>
</SwipeableDrawer>,
);

const testParam = params.anchor === 'left' || params.anchor === 'right' ? 'x' : 'y';

const DRAG_STARTED_SIGNAL = 20; // Same as in SwipeableDrawer
const DRAWER_SIZE = 250;
const bodyMargin = document.body.getBoundingClientRect().x;
const absoluteBodyWidth = bodyWidth + bodyMargin * 2;

const swipeArea = document.querySelector('[class*=PrivateSwipeArea-root]');

fireEvent.touchStart(swipeArea, {
touches: [new Touch({ identifier: 0, target: swipeArea, ...params.openTouches[0] })],
});

let startPosition = -1 * (DRAWER_SIZE - DRAG_STARTED_SIGNAL); // default value for left & top anchor

if (params.anchor === 'right') {
startPosition = absoluteBodyWidth - DRAG_STARTED_SIGNAL;
}

if (params.anchor === 'bottom') {
startPosition = windowHeight - DRAG_STARTED_SIGNAL;
}
// The 20 comes from the DRAG_STARTED_SIGNAL, defined in the SwipeableDrawer
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
expect(getByTestId('drawer').getBoundingClientRect()[testParam]).to.equal(startPosition);

fireEvent.touchMove(swipeArea, {
touches: [new Touch({ identifier: 0, target: swipeArea, ...params.openTouches[1] })],
});

fireEvent.touchMove(swipeArea, {
touches: [new Touch({ identifier: 0, target: swipeArea, ...params.openTouches[2] })],
});

fireEvent.touchEnd(swipeArea, {
changedTouches: [
new Touch({ identifier: 0, target: swipeArea, ...params.openTouches[2] }),
],
});

expect(handleOpen.callCount).to.equal(1);
setProps({ open: true });

let endPosition = 0; // default value for left & top anchor

if (params.anchor === 'right') {
endPosition = absoluteBodyWidth - DRAWER_SIZE;
}

if (params.anchor === 'bottom') {
endPosition = windowHeight - DRAWER_SIZE;
}

expect(getByTestId('drawer').getBoundingClientRect()[testParam]).to.equal(endPosition);
});

it('should stay closed when not swiping far enough', () => {
// simulate open swipe that doesn't swipe far enough
const handleOpen = spy();
Expand Down