Skip to content

Commit 85158ba

Browse files
vsvsvtay1orjoneskennylam
authored
fix(composed-modal): don't close on mouse drag and release outside modal (#18188)
* fix(composed-modal): don't close on mouse drag and release outside modal * test(composed-modal): unit test coverage for #18188 --------- Co-authored-by: Taylor Jones <[email protected]> Co-authored-by: kennylam <[email protected]>
1 parent 03ec6cb commit 85158ba

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,15 @@
17401740
"contributions": [
17411741
"code"
17421742
]
1743+
},
1744+
{
1745+
"login": "vsvsv",
1746+
"name": "Vsevolod Platunov",
1747+
"avatar_url": "https://avatars.githubusercontent.com/u/9214692?v=4",
1748+
"profile": "https://github.com/vsvsv",
1749+
"contributions": [
1750+
"code"
1751+
]
17431752
}
17441753
],
17451754
"commitConvention": "none"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ check out our [Contributing Guide](/.github/CONTRIBUTING.md) and our
322322
<td align="center"><a href="https://github.com/a88zach"><img src="https://avatars.githubusercontent.com/u/1724822?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Zach Tindall</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=a88zach" title="Code">💻</a></td>
323323
</tr>
324324
<tr>
325+
<td align="center"><a href="https://github.com/vsvsv"><img src="https://avatars.githubusercontent.com/u/9214692?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vsevolod Platunov</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=vsvsv" title="Code">💻</a></td>
325326
<td align="center"><a href="https://github.com/ashna000"><img src="https://avatars.githubusercontent.com/u/12691034?s=96&v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ashna Thomas</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=ashna000" title="Code">💻</a></td>
326327
</tr>
327328
</table>

packages/react/src/components/ComposedModal/ComposedModal-test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/* eslint-disable jsx-a11y/label-has-associated-control */
99

1010
import React from 'react';
11-
import { render, screen } from '@testing-library/react';
11+
import { fireEvent, render, screen } from '@testing-library/react';
1212
import userEvent from '@testing-library/user-event';
1313
import ComposedModal, { ModalBody } from './ComposedModal';
1414
import { ModalHeader } from './ModalHeader';
@@ -396,4 +396,35 @@ describe('ComposedModal', () => {
396396
await userEvent.click(modal);
397397
expect(onClick).toHaveBeenCalled();
398398
});
399+
400+
it('should close when clicked on outside background layer', async () => {
401+
const onClose = jest.fn();
402+
render(
403+
<ComposedModal open onClose={onClose}>
404+
<ModalBody>This is the modal body content</ModalBody>
405+
</ComposedModal>
406+
);
407+
const backgroundLayer = screen.getByRole('presentation');
408+
await userEvent.click(backgroundLayer);
409+
expect(onClose).toHaveBeenCalled();
410+
});
411+
412+
it('should NOT close when clicked inside dialog window, dragged outside and released mouse button', async () => {
413+
const onClose = jest.fn();
414+
render(
415+
<ComposedModal open onClose={onClose}>
416+
<ModalBody data-testid="modal-body-1">
417+
This is the modal body content
418+
</ModalBody>
419+
</ComposedModal>
420+
);
421+
422+
const modalBody = screen.getByTestId('modal-body-1');
423+
const backgroundLayer = screen.getByRole('presentation');
424+
425+
fireEvent.mouseDown(modalBody, { target: modalBody });
426+
fireEvent.click(backgroundLayer, { target: backgroundLayer });
427+
428+
expect(onClose).not.toHaveBeenCalled();
429+
});
399430
});

packages/react/src/components/ComposedModal/ComposedModal.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React, {
88
type ReactNode,
99
type ReactElement,
1010
type RefObject,
11+
type MutableRefObject,
1112
useMemo,
1213
isValidElement,
1314
} from 'react';
@@ -257,6 +258,8 @@ const ComposedModal = React.forwardRef<HTMLDivElement, ComposedModalProps>(
257258
const button = useRef<HTMLButtonElement>(null);
258259
const startSentinel = useRef<HTMLButtonElement>(null);
259260
const endSentinel = useRef<HTMLButtonElement>(null);
261+
const onMouseDownTarget: MutableRefObject<Node | null> =
262+
useRef<Node | null>(null);
260263
const focusTrapWithoutSentinels = useFeatureFlag(
261264
'enable-experimental-focus-wrap-without-sentinels'
262265
);
@@ -299,14 +302,21 @@ const ComposedModal = React.forwardRef<HTMLDivElement, ComposedModalProps>(
299302
onKeyDown?.(event);
300303
}
301304

305+
function handleOnMouseDown(evt: React.MouseEvent<HTMLDivElement>) {
306+
const target = evt.target as Node;
307+
onMouseDownTarget.current = target;
308+
}
309+
302310
function handleOnClick(evt: React.MouseEvent<HTMLDivElement>) {
303311
const target = evt.target as Node;
312+
const mouseDownTarget = onMouseDownTarget.current as Node;
304313
evt.stopPropagation();
305314
if (
306315
!preventCloseOnClickOutside &&
307316
!elementOrParentIsFloatingMenu(target, selectorsFloatingMenus) &&
308317
innerModal.current &&
309-
!innerModal.current.contains(target)
318+
!innerModal.current.contains(target) &&
319+
!innerModal.current.contains(mouseDownTarget)
310320
) {
311321
closeModal(evt);
312322
}
@@ -457,6 +467,10 @@ const ComposedModal = React.forwardRef<HTMLDivElement, ComposedModalProps>(
457467
aria-hidden={!open}
458468
onBlur={!focusTrapWithoutSentinels ? handleBlur : () => {}}
459469
onClick={composeEventHandlers([rest?.onClick, handleOnClick])}
470+
onMouseDown={composeEventHandlers([
471+
rest?.onMouseDown,
472+
handleOnMouseDown,
473+
])}
460474
onKeyDown={handleKeyDown}
461475
className={modalClass}>
462476
<div

0 commit comments

Comments
 (0)