-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
186 lines (171 loc) · 4.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
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
import React, { useState, useEffect, useRef } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import shortid from 'shortid';
import './modali.css';
const Button = ({
onClick, label, isStyleDefault, isStyleCancel, isStyleDestructive,
}) => {
const buttonClass = classNames({
'modali-button': true,
'modali-button-cancel': isStyleCancel,
'modali-button-default': isStyleDefault,
'modali-button-destructive': isStyleDestructive,
});
return (
<button
type="button"
className={buttonClass}
onClick={onClick}
>
{label}
</button>
);
};
Button.defaultProps = {
isStyleDefault: false,
isStyleCancel: false,
isStyleDestructive: false,
};
Button.propTypes = {
onClick: PropTypes.func.isRequired,
label: PropTypes.string.isRequired,
isStyleDefault: PropTypes.bool,
isStyleCancel: PropTypes.bool,
isStyleDestructive: PropTypes.bool,
};
const Modal = ({
isModalVisible, hide, options, children,
}) => {
function handleOverlayClicked(e) {
if (e.target.className !== 'modali-wrapper') {
return;
}
if (options === undefined) {
hide();
} else {
if (options.overlayClose !== false) {
hide();
}
if (options.onOverlayClicked) {
options.onOverlayClicked();
}
}
}
function renderBody() {
if (children) {
return children;
} if (options && options.message) {
return (
<div className="modali-body-style">
{options.message}
</div>
);
}
return false;
}
function renderFooter() {
const { buttons } = options;
return (
<div className="modali-footer">
{buttons.map(button => (
<React.Fragment
key={shortid.generate()}
>
{button}
</React.Fragment>
))}
</div>
);
}
const modaliWrapperClass = classNames({
'modali-wrapper': true,
'modali-wrapper-centered': options && options.centered,
});
const modaliClass = classNames({
modali: true,
'modali-size-large': options && options.large,
'modali-size-regular': !options || (options && !options.large),
'modali-animated modali-animation-fade-in': options && options.animated,
});
return isModalVisible ? ReactDOM.createPortal(
<React.Fragment>
<div className="modali-overlay" />
<div className={modaliWrapperClass} aria-modal aria-hidden tabIndex={-1} role="dialog" onClick={handleOverlayClicked}>
<div className={modaliClass}>
<div className="modali-content">
{options !== undefined && options.closeButton === false ? null : (
<div className="modali-header">
{options !== undefined && options.title !== undefined && (
<div className="modali-title">
{options.title}
</div>
)}
<button type="button" className="modali-close-button" data-dismiss="modal" aria-label="Close" onClick={hide}>
<span aria-hidden="true">×</span>
</button>
</div>
)}
<div className="modali-body">
{renderBody()}
</div>
{options && options.buttons && options.buttons.length > 0 && renderFooter()}
</div>
</div>
</div>
</React.Fragment>, document.body,
) : null;
};
const Modali = () => {};
Modali.Button = Button;
Modali.Modal = Modal;
export default Modali;
export const useModali = (options) => {
const [hasToggledBefore, setHasToggledBefore] = useState(false);
const [isModalVisible, setIsModalVisible] = useState(false);
const [isShown, setIsShown] = useState(false);
const isModalVisibleRef = useRef(isModalVisible);
isModalVisibleRef.current = isModalVisible;
let timeoutHack;
function toggle() {
timeoutHack = setTimeout(() => {
setIsModalVisible(!isModalVisibleRef.current);
clearTimeout(timeoutHack);
}, 10);
setIsShown(!isShown);
setHasToggledBefore(true);
}
function handleKeyDown(event) {
if (event.keyCode !== 27 || (options && options.keyboardClose === false)) return;
toggle();
if (options && options.onEscapeKeyDown) {
options.onEscapeKeyDown();
}
}
useEffect(() => {
if (isShown) {
if (options && options.onShow) {
options.onShow();
}
document.addEventListener('keydown', handleKeyDown);
document.body.classList.add('modali-open');
}
if (!isShown && hasToggledBefore) {
if (options && options.onHide) {
options.onHide();
}
document.body.classList.remove('modali-open');
}
return () => document.removeEventListener('keydown', handleKeyDown);
}, [isShown]);
return [
{
isShown,
isModalVisible,
hide: toggle,
options,
},
toggle,
];
};