This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathDialog.js
87 lines (74 loc) · 2.82 KB
/
Dialog.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
import React from 'react';
import PropTypes from 'prop-types';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
const propTypes = {
className: PropTypes.string,
onCancel: PropTypes.func,
onBackdropClick: PropTypes.func,
open: PropTypes.bool
};
const defaultProps = {
onCancel: e => e.preventDefault()
};
class Dialog extends React.Component {
componentDidMount() {
this.backdropClickCallback = this.onDialogClick.bind(this);
this.dialogRef.addEventListener('click', this.backdropClickCallback);
this.dialogRef.addEventListener('cancel', this.props.onCancel);
if (this.props.open) {
findDOMNode(this).showModal();
}
}
componentDidUpdate(prevProps) {
if (this.props.open !== prevProps.open) {
if (this.props.open) {
findDOMNode(this).showModal();
// display the dialog at the right location
// needed for the polyfill, otherwise it's not at the right position
const windowHeight = window.innerHeight;
if (this.dialogRef) {
const dialogHeight = this.dialogRef.clientHeight;
this.dialogRef.style.position = 'fixed';
this.dialogRef.style.top = `${(windowHeight - dialogHeight) / 2}px`;
}
} else {
findDOMNode(this).close();
}
}
}
componentWillUnmount() {
this.dialogRef.removeEventListener('cancel', this.props.onCancel);
this.dialogRef.removeEventListener('click', this.backdropClickCallback);
}
onDialogClick(event) {
// http://stackoverflow.com/a/26984690
if (this.props.onBackdropClick && event.target === this.dialogRef) {
const rect = this.dialogRef.getBoundingClientRect();
const insideDialog = (
rect.top <= event.clientY &&
event.clientY <= rect.top + rect.height &&
rect.left <= event.clientX &&
event.clientX <= rect.left + rect.width
);
if (!insideDialog) {
this.props.onBackdropClick();
}
}
}
render() {
// We cannot set the `open` prop on the Dialog if we manage its state manually with `showModal`,
// thus the disabled eslint rule
// eslint-disable-next-line no-unused-vars
const { className, open, onCancel, children, onBackdropClick, ...otherProps } = this.props;
const classes = classNames('mdl-dialog', className);
return (
<dialog ref={(c) => (this.dialogRef = c)} className={classes} {...otherProps}>
{children}
</dialog>
);
}
}
Dialog.propTypes = propTypes;
Dialog.defaultProps = defaultProps;
export default Dialog;