forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModal.jsx
64 lines (54 loc) · 1.53 KB
/
Modal.jsx
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
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React, { useEffect, useRef } from 'react';
import ExitIcon from '../../../images/exit.svg';
// Common logic from NewFolderModal, NewFileModal, UploadFileModal
const Modal = ({
title,
onClose,
closeAriaLabel,
contentClassName,
children
}) => {
const modalRef = useRef(null);
const handleOutsideClick = (e) => {
// ignore clicks on the component itself
if (e.path.includes(modalRef.current)) return;
onClose();
};
useEffect(() => {
modalRef.current.focus();
document.addEventListener('click', handleOutsideClick, false);
return () => {
document.removeEventListener('click', handleOutsideClick, false);
};
}, []);
return (
<section className="modal" ref={modalRef}>
<div className={classNames('modal-content', contentClassName)}>
<div className="modal__header">
<h2 className="modal__title">{title}</h2>
<button
className="modal__exit-button"
onClick={onClose}
aria-label={closeAriaLabel}
>
<ExitIcon focusable="false" aria-hidden="true" />
</button>
</div>
{children}
</div>
</section>
);
};
Modal.propTypes = {
title: PropTypes.string.isRequired,
onClose: PropTypes.func.isRequired,
closeAriaLabel: PropTypes.string.isRequired,
contentClassName: PropTypes.string,
children: PropTypes.node.isRequired
};
Modal.defaultProps = {
contentClassName: ''
};
export default Modal;