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

fix(composed-modal): don't close on mouse drag and release outside modal #18188

Merged
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,15 @@
"contributions": [
"code"
]
},
{
"login": "vsvsv",
"name": "Vsevolod Platunov",
"avatar_url": "https://avatars.githubusercontent.com/u/9214692?v=4",
"profile": "https://github.com/vsvsv",
"contributions": [
"code"
]
}
],
"commitConvention": "none"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ check out our [Contributing Guide](/.github/CONTRIBUTING.md) and our
<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>
</tr>
<tr>
<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>
<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>
</tr>
</table>
Expand Down
17 changes: 16 additions & 1 deletion packages/react/src/components/ComposedModal/ComposedModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React, {
type ReactNode,
type ReactElement,
type RefObject,
type MutableRefObject,
useMemo,
isValidElement,
} from 'react';
Expand Down Expand Up @@ -257,6 +258,8 @@ const ComposedModal = React.forwardRef<HTMLDivElement, ComposedModalProps>(
const button = useRef<HTMLButtonElement>(null);
const startSentinel = useRef<HTMLButtonElement>(null);
const endSentinel = useRef<HTMLButtonElement>(null);
const onMouseDownTarget: MutableRefObject<Node | null> =
useRef<Node | null>(null);
const focusTrapWithoutSentinels = useFeatureFlag(
'enable-experimental-focus-wrap-without-sentinels'
);
Expand Down Expand Up @@ -299,14 +302,22 @@ const ComposedModal = React.forwardRef<HTMLDivElement, ComposedModalProps>(
onKeyDown?.(event);
}

function handleOnMouseDown(evt: React.MouseEvent<HTMLDivElement>) {
const target = evt.target as Node;
onMouseDownTarget.current = target;
}

function handleOnClick(evt: React.MouseEvent<HTMLDivElement>) {
const target = evt.target as Node;
const mouseDownTarget = onMouseDownTarget.current as Node;
console.log({ target, mouseDownTarget });
vsvsv marked this conversation as resolved.
Show resolved Hide resolved
evt.stopPropagation();
if (
!preventCloseOnClickOutside &&
!elementOrParentIsFloatingMenu(target, selectorsFloatingMenus) &&
innerModal.current &&
!innerModal.current.contains(target)
!innerModal.current.contains(target) &&
!innerModal.current.contains(mouseDownTarget)
) {
closeModal(evt);
}
Expand Down Expand Up @@ -457,6 +468,10 @@ const ComposedModal = React.forwardRef<HTMLDivElement, ComposedModalProps>(
aria-hidden={!open}
onBlur={!focusTrapWithoutSentinels ? handleBlur : () => {}}
onClick={composeEventHandlers([rest?.onClick, handleOnClick])}
onMouseDown={composeEventHandlers([
rest?.onMouseDown,
handleOnMouseDown,
])}
onKeyDown={handleKeyDown}
className={modalClass}>
<div
Expand Down
Loading