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 bug when dragging mouse outside #2896

Merged
merged 1 commit into from Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions src/mantine-core/src/Modal/Modal.story.tsx
@@ -1,6 +1,7 @@
import React, { useState } from 'react';
import { Modal } from './Modal';
import { Button } from '../Button';
import { ColorInput } from '../ColorInput';

export default { title: 'Modal' };

Expand Down Expand Up @@ -52,3 +53,14 @@ export function WithPageScrollbars() {
</div>
);
}

export function WithPortalChildren() {
return (
<div style={{ padding: 40 }}>
<WrappedModal>
<ColorInput label="No Portal" mb="md" />
<ColorInput label="Within Portal" mb="md" withinPortal />
</WrappedModal>
</div>
);
}
29 changes: 25 additions & 4 deletions src/mantine-core/src/Modal/Modal.tsx
@@ -1,5 +1,12 @@
import React, { useEffect } from 'react';
import { useScrollLock, useFocusTrap, useFocusReturn, useId } from '@mantine/hooks';
import React, { useEffect, useRef } from 'react';
import {
useScrollLock,
useFocusTrap,
useFocusReturn,
useId,
useWindowEvent,
useMergedRef,
} from '@mantine/hooks';
import {
DefaultProps,
MantineNumberSize,
Expand Down Expand Up @@ -171,6 +178,9 @@ export function Modal(props: ModalProps) {
{ unstyled, classNames, styles, name: 'Modal' }
);
const focusTrapRef = useFocusTrap(trapFocus && opened);
const overlayRef = useRef<HTMLDivElement>(null);
const mergedRef = useMergedRef(focusTrapRef, overlayRef);

const _overlayOpacity =
typeof overlayOpacity === 'number'
? overlayOpacity
Expand All @@ -197,6 +207,17 @@ export function Modal(props: ModalProps) {

useFocusReturn({ opened, shouldReturnFocus: trapFocus && withFocusReturn });

const clickTarget = useRef<EventTarget>(null);
useWindowEvent('mousedown', (e) => {
clickTarget.current = e.target;
});

const handleOutsideClick = () => {
if (clickTarget.current !== overlayRef.current) return;

closeOnClickOutside && onClose();
};

return (
<OptionalPortal withinPortal={withinPortal} target={target}>
<GroupedTransition
Expand Down Expand Up @@ -236,13 +257,13 @@ export function Modal(props: ModalProps) {
<div
role="presentation"
className={classes.inner}
onClick={() => closeOnClickOutside && onClose()}
onClick={handleOutsideClick}
onKeyDown={(event) => {
const shouldTrigger =
(event.target as any)?.getAttribute('data-mantine-stop-propagation') !== 'true';
shouldTrigger && event.key === 'Escape' && closeOnEscape && onClose();
}}
ref={focusTrapRef}
ref={mergedRef}
>
<Paper<'div'>
className={classes.modal}
Expand Down