-
Notifications
You must be signed in to change notification settings - Fork 91
/
Modal.js
152 lines (128 loc) · 4.34 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
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
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
import React, { Children, cloneElement, isValidElement, useRef, useState, useEffect } from 'react';
import { createPortal } from 'react-dom';
import PropTypes from 'prop-types';
import { useModal } from '@zendeskgarden/container-modal';
import { hasType } from '@zendeskgarden/react-utilities';
import isWindow from 'dom-helpers/isWindow';
import ownerDocument from 'dom-helpers/ownerDocument';
import ownerWindow from 'dom-helpers/ownerWindow';
import css from 'dom-helpers/css';
import getScrollbarSize from 'dom-helpers/scrollbarSize';
import ModalView from '../views/ModalView';
import Backdrop from '../views/Backdrop';
import Body from '../views/Body';
import Close from '../views/Close';
import Header from '../views/Header';
import useCombinedRefs from '../utils/useCombinedRefs';
const isOverflowing = element => {
const doc = ownerDocument(element);
const win = ownerWindow(doc);
const isBody = element && element.tagName.toLowerCase() === 'body';
/* istanbul ignore next */
if (!isWindow(doc) && !isBody) {
return element.scrollHeight > element.clientHeight;
}
const style = win.getComputedStyle(doc.body);
const marginLeft = parseInt(style.getPropertyValue('margin-left'), 10);
const marginRight = parseInt(style.getPropertyValue('margin-right'), 10);
return marginLeft + doc.body.clientWidth + marginRight < win.innerWidth;
};
/**
* High-level abstraction for basic Modal implementations. Accepts all `<div>` props.
*/
const Modal = React.forwardRef(
({ backdropProps, children, onClose, center, animate, id, appendToNode, ...modalProps }, ref) => {
const modalRef = useCombinedRefs(ref);
const {
getBackdropProps,
getModalProps,
getTitleProps,
getContentProps,
getCloseProps
} = useModal({ id, onClose, modalRef });
useEffect(() => {
const bodyElement = document.querySelector('body');
let previousBodyPaddingRight;
if (isOverflowing(bodyElement)) {
const scrollbarSize = getScrollbarSize();
const bodyPaddingRight = parseInt(css(bodyElement, 'paddingRight') || 0, 10);
previousBodyPaddingRight = bodyElement.style.paddingRight;
bodyElement.style.paddingRight = `${bodyPaddingRight + scrollbarSize}px`;
}
const previousBodyOverflow = bodyElement.style.overflow;
bodyElement.style.overflow = 'hidden';
return () => {
bodyElement.style.overflow = previousBodyOverflow;
bodyElement.style.paddingRight = previousBodyPaddingRight;
};
}, []);
return createPortal(
<Backdrop {...getBackdropProps({ center, animate, ...backdropProps })}>
<ModalView {...getModalProps({ animate, ref: modalRef, ...modalProps })}>
{Children.map(children, child => {
if (!isValidElement(child)) {
return child;
}
if (hasType(child, Header)) {
return cloneElement(child, getTitleProps(child.props));
}
if (hasType(child, Body)) {
return cloneElement(child, getContentProps(child.props));
}
if (hasType(child, Close)) {
return cloneElement(child, getCloseProps(child.props));
}
return child;
})}
</ModalView>
</Backdrop>,
appendToNode
);
}
);
Modal.propTypes = {
children: PropTypes.any,
/**
* Props to spread onto backdrop element
*/
backdropProps: PropTypes.object,
/**
* Enable large modal styling
*/
large: PropTypes.bool,
/**
* Enable modal animation
*/
animate: PropTypes.bool,
/**
* Center modal
*/
center: PropTypes.bool,
/**
* Callback when a close action has been completed.
* Can be triggered from the backdrop and Close icon.
* @param {Object} event - DOM event that triggered the close action
*/
onClose: PropTypes.func,
/**
* The DOM element where the modal will be rendered to
**/
appendToNode: PropTypes.instanceOf(Element),
/**
* The root ID to use for descendants. A unique ID is created if none is provided.
**/
id: PropTypes.string
};
Modal.defaultProps = {
animate: true,
center: true,
appendToNode: document.body
};
/** @component */
export default Modal;