Repository navigation
Expand file tree
/
Copy pathModal.tsx
More file actions
78 lines (70 loc) · 1.76 KB
/
Copy pathModal.tsx
File metadata and controls
78 lines (70 loc) · 1.76 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
import { useDialog } from "@react-aria/dialog";
import { FocusScope } from "@react-aria/focus";
import {
OverlayContainer,
useModal,
useOverlay,
usePreventScroll,
} from "@react-aria/overlays";
import React, { useRef } from "react";
import { VisuallyHidden } from "react-aria";
import tw from "twin.macro";
export interface Props {
title: string;
isOpen: boolean;
onClose: () => void;
}
const ModalDialog: React.FC<Props> = ({ title, isOpen, onClose, children }) => {
// Handle interacting outside the dialog and pressing
// the Escape key to close the modal.
const ref = useRef<HTMLDivElement | null>(null);
const { overlayProps } = useOverlay(
{
isOpen,
onClose,
isDismissable: true,
},
ref,
);
// Prevent scrolling while the modal is open, and hide content
// outside the modal from screen readers.
usePreventScroll();
const { modalProps } = useModal();
// Get props for the dialog and its title
const { dialogProps, titleProps } = useDialog({}, ref);
return (
<div
css={[
tw`fixed top-0 right-0 bottom-0 left-0 select-none`,
tw`bg-black bg-opacity-50`,
]}
>
<FocusScope contain restoreFocus autoFocus>
<div
{...overlayProps}
{...dialogProps}
{...modalProps}
ref={ref}
tw="focus:outline-none"
>
<VisuallyHidden>
<h3 {...titleProps} style={{ marginTop: 0 }}>
{title}
</h3>
</VisuallyHidden>
{children}
</div>
</FocusScope>
</div>
);
};
export const Modal: React.FC<Props> = props => {
if (!props.isOpen) {
return null;
}
return (
<OverlayContainer>
<ModalDialog {...props} />
</OverlayContainer>
);
};