-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
index.js
86 lines (75 loc) · 1.86 KB
/
index.js
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
import React from 'react';
import styled, { createGlobalStyle } from 'styled-components';
import Portal from '@codesandbox/common/lib/components/Portal';
import { ESC } from '@codesandbox/common/lib/utils/keycodes';
import { Spring, animated } from 'react-spring/renderprops';
const NoScroll = createGlobalStyle`
html {
overflow: hidden;
}
`;
const Container = styled(animated.div)`
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow: auto;
z-index: 10;
background-color: rgba(0, 0, 0, 0.5);
display: ${props => (props.isOpen ? 'flex' : 'none')};
justify-content: center;
`;
const Content = styled.div`
width: 1000px;
margin-top: 5%;
z-index: 20;
max-width: 90%;
`;
export default class Modal extends React.PureComponent {
componentDidMount() {
window.addEventListener('keydown', this.listenForEsc);
}
componentWillUnmount() {
window.removeEventListener('keydown', this.listenForEsc);
}
listenForEsc = e => {
if (e.keyCode === ESC) {
this.props.onClose();
}
};
render() {
const { children } = this.props;
return (
<Portal>
{this.props.isOpen && <NoScroll />}
<Spring
from={{ opacity: 0 }}
to={{ opacity: this.props.isOpen ? 1 : 0 }}
config={{ tension: 240, velocity: 10 }}
native
>
{props => (
<Container
role="dialog"
aria-modal="true"
style={props}
onClick={this.props.onClose}
isOpen={this.props.isOpen}
>
<Content
onClick={e => {
if (e) {
e.stopPropagation();
}
}}
>
{children}
</Content>
</Container>
)}
</Spring>
</Portal>
);
}
}