forked from smooth-code/smooth-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modal.js
94 lines (84 loc) · 1.83 KB
/
Modal.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
87
88
89
90
91
92
93
94
/* eslint-disable jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */
import React from 'react'
import { createPortal } from 'react-dom'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import classNames from 'classnames'
import * as defaultTheme from './theme/defaultTheme'
import { th } from './utils'
class Portal extends React.Component {
constructor(props) {
super(props)
if (!this.container && typeof document !== 'undefined') {
this.container = document.createElement('div')
document.body.appendChild(this.container)
}
}
componentWillUnmount() {
document.body.removeChild(this.container)
}
render() {
if (!this.container) return null
return createPortal(this.props.children, this.container)
}
}
const ModalComponent = ({
className,
theme,
opened,
onClose,
children,
...props
}) => (
<Portal>
<div
role="dialog"
tabIndex="-1"
className={classNames(
'sui-modal',
{ 'sui-modal-opened': opened },
className,
)}
{...props}
>
<div className="sui-modal-backdrop" onClick={onClose} />
{children}
</div>
</Portal>
)
const Modal = styled(ModalComponent)`
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: ${th('zIndexModal')};
display: none;
overflow: hidden;
outline: 0;
opacity: 0;
transition: opacity 1000ms;
&.sui-modal-opened {
display: block;
overflow-x: hidden;
overflow-y: auto;
opacity: 1;
}
.sui-modal-backdrop {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: ${th('modalBackdropBg')};
opacity: 0.5;
}
`
Modal.propTypes = {
children: PropTypes.node,
}
Modal.defaultProps = {
theme: defaultTheme,
}
/** @component */
export default Modal