| 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267 |
2x
2x
2x
7x
17x
6x
6x
3x
3x
7x
7x
7x
9x
9x
9x
9x
9x
9x
9x
9x
17x
17x
2x
17x
2x
2x
2x
17x
17x
17x
2x
2x
| // @flow
import React, {PureComponent} from 'react';
import noop from 'lodash/noop';
import styled, {keyframes} from 'styled-components';
import StateMachine from 'javascript-state-machine';
import ContextTypes from './types/contextTypes';
const animations = {
rightToLeft: 'right-to-left'
};
const states = {
requestOpen: 'requestOpen',
open: 'open',
requestClose: 'requestClose',
close: 'close'
};
const rightToLeftTranslates = {
from: 'translateX(100%)',
to: 'translateX(0)'
};
type State = {
children: any,
onRequestClose: Function,
className: string,
style: Object,
isOpen: boolean,
animation: string,
state?: string,
closeOnOverlayClick?: boolean
};
export default function modalComponentHOC(subscribe: Function, initialProps: State) {
return class extends PureComponent {
static defaultProps = {
className: ''
};
static childContextTypes = ContextTypes;
getChildContext() {
return {
closeModal: this.state.onRequestClose
};
}
mapCloseToNextState = () => {
const {animation} = this.state;
return animation ? states.requestOpen : states.open;
};
mapOpenToNextState = () => {
const {animation} = this.state;
return animation ? states.requestClose : states.close;
};
stateMachine: Object = new StateMachine({
init: states.close,
transitions: [
{name: 'next', from: states.close, to: this.mapCloseToNextState},
{name: 'next', from: states.requestOpen, to: states.open},
{name: 'next', from: states.open, to: this.mapOpenToNextState},
{name: 'next', from: states.requestClose, to: states.close}
]
});
constructor(props: Object, ctx: Object) {
super(props, ctx);
this.state = Object.assign(this.state, initialProps);
subscribe(this.handleChanges);
}
state: State = {
elementStyleName: noop,
children: null,
onRequestClose: noop,
className: '',
style: {},
isOpen: false,
animation: '',
state: this.stateMachine.state,
closeOnOverlayClick: false
};
onContentClick = (e: SyntheticEvent) => {
e.stopPropagation();
e.nativeEvent.stopImmediatePropagation();
};
handleChanges = (nextProps: State) => {
const {isOpen} = this.state;
const {isOpen: nextIsOpen} = nextProps;
Eif (isOpen !== nextIsOpen) {
this.nextState();
}
this.setState(nextProps);
};
nextState = () => {
const {stateMachine} = this;
stateMachine.next();
this.setState({
state: stateMachine.state
});
};
render() {
const {
children,
onRequestClose,
style,
animation,
state,
closeOnOverlayClick,
className,
isOpen
} = this.state;
return (
<Modal
state={state}
data-selenium="modal"
>
<ModalOverlay
data-selenium="modal-overlay"
/>
<ModalBody
data-selenium="modal-body"
onClick={closeOnOverlayClick ? onRequestClose : noop}
>
<ModalContent
className={className}
style={style}
data-selenium="modal-content"
onClick={this.onContentClick}
animation={animation}
state={state}
onAnimationEnd={this.nextState}
>
{isOpen && children}
</ModalContent>
</ModalBody>
</Modal>
);
}
};
}
const Modal = styled.div`
display: ${props => (props.state === states.close ? 'none' : 'table')};
height: 100%;
left: 0;
position: absolute;
right: 0;
table-layout: fixed;
top: 0;
width: 100%;
z-index: 120;
`;
const ModalOverlay = styled.div`
background-color: rgba(0, 0, 0, 0.4);
bottom: 0;
left: 0;
position: fixed;
right: 0;
top: 0;
`;
const ModalBody = styled.div`
display: table-cell;
margin-bottom: 20px;
padding: 24px 0;
position: relative;
text-align: center;
vertical-align: middle;
width: 100%;
`;
const ModalContent = styled.div`
background: #fff;
border-radius: 2px;
display: inline-block;
min-height: 150px;
min-width: 200px;
${props => mapAnimationToStyles(props.state, props.animation)}
`;
function mapAnimationToStyles(state: string, animation: string) {
switch (animation) {
case animations.rightToLeft:
// $FlowIgnore
return `
bottom: 0;
overflow: auto;
position: fixed;
right: 0;
top: 0;
transform: ${mapStateToTransform(state)};
animation: ${mapStateToAnimation(state, animation)} .5s linear;
@media screen and (max-width: 600px) {
left: 0;
}
@media print {
left: 0;
overflow: visible;
}
`;
default:
return '';
}
}
function mapStateToTransform(state: string) {
switch (state) {
case states.open:
return rightToLeftTranslates.to;
case states.close:
return rightToLeftTranslates.from;
default:
return undefined;
}
}
function mapStateToAnimation(state: string, animation: string) {
switch (state) {
case states.requestOpen:
return mapAnimationToKeyFrame(animation, true);
case states.requestClose:
return mapAnimationToKeyFrame(animation, false);
default:
return undefined;
}
}
const RightToLeftKeyFrameOpen = keyframes`
from {
transform: ${rightToLeftTranslates.from};
}
to {
transform: ${rightToLeftTranslates.to};
}
`;
const RightToLeftKeyFrameClose = keyframes`
from {
transform: ${rightToLeftTranslates.to};
}
to {
transform: ${rightToLeftTranslates.from};
}
`;
function mapAnimationToKeyFrame(animation: string, open: boolean) {
switch (animation) {
case animations.rightToLeft:
return open ? RightToLeftKeyFrameOpen : RightToLeftKeyFrameClose;
default:
return undefined;
}
}
|