forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModal.tsx
More file actions
68 lines (65 loc) · 1.62 KB
/
Modal.tsx
File metadata and controls
68 lines (65 loc) · 1.62 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
import React, { ComponentProps } from 'react';
import {
useDialogState,
DialogDisclosure,
DialogBackdrop,
Dialog,
} from 'reakit/Dialog';
import { Portal } from 'reakit/Portal';
import { Button } from '../Button';
import { Backdrop, Container } from './elements';
export interface IModalProps extends ComponentProps<typeof Button> {
label?: string;
button?: React.ElementType;
backdrop?: React.ElementType;
container?: React.ElementType;
isOpen?: boolean;
onClose?: () => void;
children:
| (({ open: boolean, toggleOpen: Function }) => React.ReactNode)
| React.ReactNode;
}
export const Modal: React.FC<IModalProps> = ({
label,
button = Button,
backdrop = Backdrop,
container = Container,
isOpen,
onClose = () => undefined,
children,
...props
}) => {
const dialog = useDialogState();
const onHide = () => {
dialog.hide();
onClose();
};
return (
<>
{label && (
<DialogDisclosure block as={button} {...dialog} {...props}>
{label}
</DialogDisclosure>
)}
<Portal>
<DialogBackdrop {...dialog} as={backdrop}>
<Dialog
{...dialog}
as={container}
aria-label={name}
modal={false}
visible={typeof isOpen !== 'undefined' ? isOpen : dialog.visible}
hide={onHide}
>
{typeof children === `function`
? (children as Function)({
open: dialog.visible,
toggleOpen: dialog.toggle,
})
: children}
</Dialog>
</DialogBackdrop>
</Portal>
</>
);
};