forked from bmcmahen/sancho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDialog.tsx
175 lines (164 loc) · 5.56 KB
/
Dialog.tsx
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/** @jsx jsx */
import { jsx } from "@emotion/core";
import * as React from "react";
import { Text } from "./Text";
import { CloseButton } from "./IconButton";
import { useTransition, animated } from "react-spring";
import { Overlay } from "./Overlay";
import { useFocusElement } from "./Hooks/use-focus-trap";
import PropTypes from "prop-types";
import useScrollLock from "use-scroll-lock";
import { useTheme } from "./Theme/Providers";
export interface DialogProps extends React.HTMLAttributes<HTMLDivElement> {
/** Whether the dialog is showing */
isOpen: boolean;
/** An optional title. If set, a header will be added to your dialog. */
title?: string;
/** Fill the entire screen on mobile devices */
mobileFullscreen?: boolean;
/** A callback for closing the dialog. */
onRequestClose: () => void;
/** The contents of the dialog */
children: React.ReactNode;
}
/**
* A dialog is useful for displaying infomation that
* commands the user's attention.
*/
export const Dialog: React.FunctionComponent<DialogProps> = ({
isOpen,
onRequestClose,
mobileFullscreen,
title,
children,
...other
}) => {
const theme = useTheme();
const transitions = useTransition(isOpen, null, {
from: { opacity: 0, transform: "scale(0.9)" },
enter: { opacity: 1, transform: "scale(1)" },
leave: { opacity: 0, transform: "scale(0.9)" },
config: { mass: 1, tension: 185, friction: 26 }
});
const scrollableRef = React.useRef(null);
const ref = React.useRef<HTMLDivElement | null>(null);
useFocusElement(ref, isOpen);
useScrollLock(isOpen, scrollableRef);
return (
<React.Fragment>
<Overlay onRequestClose={onRequestClose} isOpen={isOpen}>
<React.Fragment>
{transitions.map(
({ item, key, props: animationProps }) =>
item && (
<animated.div
key={key}
className="Dialog"
aria-modal="true"
ref={ref}
tabIndex={-1}
onClick={(e: React.MouseEvent) => {
e.stopPropagation();
}}
style={
{
opacity: animationProps.opacity,
transform: animationProps.transform
} as any
}
css={[
{
zIndex: theme.zIndices.modal,
background: theme.colors.background.default,
boxShadow: theme.shadows.md,
borderRadius: theme.radii.lg,
margin: "16px",
width: "calc(100% - 32px)",
outline: "none",
[theme.mediaQueries.sm]: {
maxWidth: "500px",
margin: "30px auto"
},
[theme.mediaQueries.lg]: {
maxWidth: "650px",
margin: "30px auto"
}
},
mobileFullscreen && {
maxWidth: "none",
margin: 0,
width: "100vw",
height: "100vh",
borderRadius: 0,
boxShadow: "none",
[theme.mediaQueries.sm]: {
maxWidth: "none",
margin: "0"
},
[theme.mediaQueries.md]: {
maxWidth: "500px",
margin: "30px auto",
height: "auto",
boxShadow: theme.shadows.md,
borderRadius: theme.radii.lg,
width: "calc(100% - 32px)"
}
}
]}
{...other}
>
<React.Fragment>
{title && (
<DialogHeader
className="Dialog__header"
css={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
padding: `${theme.spaces.lg} ${theme.spaces.lg} 0 ${
theme.spaces.lg
}`
}}
title={title}
onRequestClose={onRequestClose}
/>
)}
<div ref={scrollableRef}>{children}</div>
</React.Fragment>
</animated.div>
)
)}
</React.Fragment>
</Overlay>
</React.Fragment>
);
};
Dialog.propTypes = {
isOpen: PropTypes.bool.isRequired,
onRequestClose: PropTypes.func.isRequired,
title: PropTypes.string,
mobileFullscreen: PropTypes.bool,
children: PropTypes.node
};
interface DialogHeaderProps extends React.HTMLAttributes<HTMLDivElement> {
/** The title of the header */
title: string;
/** An optional callback for closing the dialog. If set, a close button will be added to the header */
onRequestClose?: () => void;
}
export const DialogHeader: React.FunctionComponent<DialogHeaderProps> = ({
title,
onRequestClose,
...other
}) => (
<div {...other}>
<Text wrap={false} variant="h4">
{title}
</Text>
{onRequestClose && <CloseButton onClick={onRequestClose} />}
</div>
);
DialogHeader.propTypes = {
title: PropTypes.string.isRequired,
onRequestClose: PropTypes.func
};