forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModal.js
More file actions
99 lines (86 loc) · 2.13 KB
/
Modal.js
File metadata and controls
99 lines (86 loc) · 2.13 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
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
import React from 'react';
import styled, { createGlobalStyle } from 'styled-components';
import { ESC } from '@codesandbox/common/lib/utils/keycodes';
import { Spring, animated } from 'react-spring/renderprops.cjs';
const NoScroll = createGlobalStyle`
html {
overflow: hidden;
}
`;
const Wrapper = styled.div`
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
padding: 40px;
background-color: ${props => props.theme.background2};
display: flex;
flex-direction: column;
border-radius: 4px;
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.5);
`;
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.isOpen) {
this.props.onClose();
}
};
render() {
const { children } = this.props;
return (
<>
{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();
}
}}
>
<Wrapper>{children}</Wrapper>
</Content>
</Container>
)}
</Spring>
</>
);
}
}