-
Notifications
You must be signed in to change notification settings - Fork 12
/
ModalWrapper.tsx
214 lines (198 loc) · 5.89 KB
/
ModalWrapper.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import React, { useState, useContext, ReactNode } from "react"
import styled from "styled-components"
import Dialog from "rc-dialog"
import "rc-dialog/assets/index.css"
import { ModalWrapperProps, IStyledDialog, TitleProps } from "./types"
import { ThemeContext } from "styled-components"
import { Header, Footer, Main } from "./components"
import {
Modal,
LeftPanelModal,
RightPanelModal,
BottomPanelModal,
} from "./variants/index"
import Icon from "../Icon"
import "rc-dialog/assets/index.css"
const sizeToWidth = {
small: "49rem",
medium: "63rem",
large: "91rem",
"x-large": "105rem",
}
// Animation related
const appearDuration = 250
const transitionName = `modal`
const maskTransitionName = `modalMask`
const maskDuration = 100
const modalEnterTransitionPath = `cubic-bezier(0, 0, 0, 1)`
const modalLeaveTransitionPath = `cubic-bezier(1, 0, 1, 1)`
const animationDelay = `50ms`
// Animations on leave are not working for rc-dialog, this happens only when
// styiling the Dialog using styled components. It is probably the case that
// the Dialog component is being re-created on close, causing the previous
// stored ref to be lost
const StyledDialog = styled(Dialog)<IStyledDialog>`
.rc-dialog-body {
padding: 0;
height: auto;
max-height: ${({ theme: { knitui } }) =>
`calc(100vh - ${knitui.modalMaxContentHeightOffset}rem)`};
min-height: ${({ theme: { knitui } }) => `${knitui.modalMinHeight}rem`};
}
.rc-dialog-content {
border-radius: ${({ theme: { knitui } }) => knitui.modalBorderRadius};
}
width: ${({ customProps: { size } }) => sizeToWidth[size!]} !important;
.rc-dialog-close {
right: -1rem;
top: -1rem;
opacity: unset;
padding: 0.15rem;
line-height: 0;
background-color: ${({ theme: { knitui } }) =>
knitui.chromaPalette.Neutral50};
border-radius: 999px;
svg {
fill: ${({ theme: { knitui } }) => knitui.chromaPalette.Neutral20};
}
}
}
&.${transitionName}-appear,
&.${transitionName}-enter {
opacity: 0;
transform: scale(0.9);
}
&.${transitionName}-appear.${transitionName}-appear-active,
&.${transitionName}-enter.${transitionName}-enter-active {
opacity: 1;
transform: scale(1.0);
transition: opacity ${appearDuration}ms ${modalEnterTransitionPath} ${animationDelay}, transform ${appearDuration}ms ${modalEnterTransitionPath} ${animationDelay};
}
&.${transitionName}-leave {
opacity: 1;
transform: scale(1);
}
&.${transitionName}-leave.${transitionName}-leave-active {
opacity: 0;
transform: scale(0.9);
transition: opacity ${appearDuration}ms ${modalLeaveTransitionPath}, transform ${appearDuration}ms ${modalLeaveTransitionPath};
}
&.${maskTransitionName}-appear,
&.${maskTransitionName}-enter {
opacity: 0;
}
&.${maskTransitionName}-appear.${maskTransitionName}-appear-active,
&.${maskTransitionName}-enter.${maskTransitionName}-enter-active {
opacity: 1;
transition: opacity ${maskDuration}ms ease-out;
}
&.${maskTransitionName}-leave {
opacity: 1;
}
&.${maskTransitionName}-leave.${maskTransitionName}-leave-active {
opacity: 0;
transition: opacity ${maskDuration}ms ease-out;
}
`
/**
* The external interface for the modal
*/
const ModalWrapper: React.FC<ModalWrapperProps> = ({
size,
header,
body,
footer,
getContainer,
visible,
onClose,
panel,
destroyOnClose,
className,
style,
padding,
}) => {
/**
* Renders the appopriate variant based on the availability of a
* panel prop and its position.
* @param modalProps
*/
const ModalProxy = (modalProps: {
maxContentHeight: string
minContentHeight: string
header: ReactNode
footer: ReactNode
body: ReactNode
}) => {
if (!panel) {
return <Modal {...modalProps} />
}
switch (panel.position) {
case "left":
return <LeftPanelModal {...modalProps} panelContent={panel.content} />
case "right":
return <RightPanelModal {...modalProps} panelContent={panel.content} />
case "bottom":
return <BottomPanelModal {...modalProps} panelContent={panel.content} />
default:
return <Modal {...modalProps} />
}
}
const [showFooterBorder, setShowFooterBorder] = useState(false)
const setBodyRef = (el: HTMLElement | null) => {
if (el && el.scrollHeight > el.clientHeight) {
setShowFooterBorder(true)
}
}
const themeContext = useContext(ThemeContext)
const { knitui } = themeContext
const typographySize =
(header.leftSection as TitleProps).fontSize ||
knitui.modalTitleTypographySize
const lineHeight = knitui.typography[typographySize].lineHeight
const verticalPadding = knitui.modalHeaderPadding.vertical
const headerHeight = lineHeight + 2 * verticalPadding
const totalOffset = knitui.modalMaxContentHeightOffset + headerHeight
const maxContentHeight = `calc(100vh - ${totalOffset}rem)`
const minContentHeight = `${knitui.modalMinHeight - headerHeight}rem`
return (
<StyledDialog
customProps={{
size,
}}
getContainer={getContainer}
visible={visible}
onClose={onClose}
closeIcon={<Icon type="oClear" />}
destroyOnClose={destroyOnClose}
transitionName={transitionName}
maskTransitionName={maskTransitionName}
className={className}
style={style}>
<ModalProxy
maxContentHeight={maxContentHeight}
minContentHeight={minContentHeight}
header={<Header {...header} />}
body={
<Main customProps={{ padding }} ref={setBodyRef}>
{body}
</Main>
}
footer={
<Footer customProps={{ showBorder: showFooterBorder }}>
{footer}
</Footer>
}
/>
</StyledDialog>
)
}
ModalWrapper.defaultProps = {
size: "medium",
padding: {
vertical: "2.8rem",
horizontal: "2.1rem",
},
visible: false,
destroyOnClose: false,
}
export default ModalWrapper