-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathGlobalModal.tsx
More file actions
102 lines (89 loc) · 2.84 KB
/
GlobalModal.tsx
File metadata and controls
102 lines (89 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import clsx from 'clsx';
import type { PropsWithChildren } from 'react';
import { useCallback } from 'react';
import React, { useEffect, useRef } from 'react';
import { FocusScope } from '@react-aria/focus';
import { CloseIconRound } from './icons';
import { modalDialogManagerId, useTranslationContext } from '../../context';
import {
DialogPortalEntry,
modalDialogId,
useModalDialog,
useModalDialogIsOpen,
} from '../Dialog';
import type { ModalCloseEvent, ModalCloseSource, ModalProps } from './Modal';
export const GlobalModal = ({
children,
className,
onClose,
onCloseAttempt,
open,
}: PropsWithChildren<ModalProps>) => {
const { t } = useTranslationContext('Modal');
const dialog = useModalDialog();
const isOpen = useModalDialogIsOpen();
const innerRef = useRef<HTMLDivElement | null>(null);
const closeButtonRef = useRef<HTMLButtonElement | null>(null);
const maybeClose = useCallback(
(source: ModalCloseSource, event: ModalCloseEvent) => {
const allow = onCloseAttempt?.(source, event);
if (allow !== false) {
onClose?.(event);
dialog.close();
}
},
[dialog, onClose, onCloseAttempt],
);
const handleClick = (event: React.MouseEvent<HTMLButtonElement | HTMLDivElement>) => {
const target = event.target as HTMLButtonElement | HTMLDivElement;
if (!innerRef.current || !closeButtonRef.current) return;
if (innerRef.current?.contains(target)) return;
if (closeButtonRef.current.contains(target)) {
maybeClose('button', event);
} else if (!innerRef.current.contains(target)) {
maybeClose('overlay', event);
}
};
useEffect(() => {
if (!isOpen) return;
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') maybeClose('escape', event);
};
document.addEventListener('keydown', handleKeyDown);
return () => document.removeEventListener('keydown', handleKeyDown);
}, [isOpen, maybeClose]);
useEffect(() => {
if (open && !dialog.isOpen) {
dialog.open();
}
}, [dialog, open]);
if (!open || !isOpen) return null;
return (
<DialogPortalEntry dialogId={modalDialogId} dialogManagerId={modalDialogManagerId}>
<div
className={clsx(
'str-chat str-chat__modal str-chat-react__modal str-chat__modal--open',
className,
)}
onClick={handleClick}
>
<FocusScope autoFocus contain>
<button
className='str-chat__modal__close-button'
ref={closeButtonRef}
title={t('Close')}
type='button'
>
<CloseIconRound />
</button>
<div
className='str-chat__modal__inner str-chat-react__modal__inner'
ref={innerRef}
>
{children}
</div>
</FocusScope>
</div>
</DialogPortalEntry>
);
};