forked from mui/material-ui
/
Popover.js
402 lines (359 loc) · 10.7 KB
/
Popover.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// @flow
import React from 'react';
import type { Node } from 'react';
import ReactDOM from 'react-dom';
import warning from 'warning';
import contains from 'dom-helpers/query/contains';
import debounce from 'lodash/debounce';
import EventListener from 'react-event-listener';
import withStyles from '../styles/withStyles';
import Modal from '../internal/Modal';
import type { TransitionCallback, TransitionClasses } from '../internal/transition';
import Grow from '../transitions/Grow';
import Paper from '../Paper';
function getOffsetTop(rect, vertical) {
let offset = 0;
if (typeof vertical === 'number') {
offset = vertical;
} else if (vertical === 'center') {
offset = rect.height / 2;
} else if (vertical === 'bottom') {
offset = rect.height;
}
return offset;
}
function getOffsetLeft(rect, horizontal) {
let offset = 0;
if (typeof horizontal === 'number') {
offset = horizontal;
} else if (horizontal === 'center') {
offset = rect.width / 2;
} else if (horizontal === 'right') {
offset = rect.width;
}
return offset;
}
function getTransformOriginValue(transformOrigin) {
return [transformOrigin.horizontal, transformOrigin.vertical]
.map(n => {
return typeof n === 'number' ? `${n}px` : n;
})
.join(' ');
}
// Sum the scrollTop between two elements.
function getScrollParent(parent, child) {
let element = child;
let scrollTop = 0;
while (element && element !== parent) {
element = element.parentNode;
scrollTop += element.scrollTop;
}
return scrollTop;
}
export const styles = {
paper: {
position: 'absolute',
overflowY: 'auto',
overflowX: 'hidden',
'&:focus': {
outline: 'none',
},
},
};
export type Origin = {
horizontal: 'left' | 'center' | 'right' | number,
vertical: 'top' | 'center' | 'bottom' | number,
};
type ProvidedProps = {
anchorOrigin: Origin,
classes: Object,
transformOrigin: Origin,
marginThreshold: number,
};
export type Props = {
/**
* This is the DOM element that will be used
* to set the position of the popover.
*/
anchorEl?: ?HTMLElement,
/**
* This is the point on the anchor where the popover's
* `anchorEl` will attach to.
*
* Options:
* vertical: [top, center, bottom];
* horizontal: [left, center, right].
*/
anchorOrigin?: Origin,
/**
* The content of the component.
*/
children: Node,
/**
* Useful to extend the style applied to components.
*/
classes?: Object,
/**
* The elevation of the popover.
*/
elevation?: number,
/**
* @ignore
*/
getContentAnchorEl?: Function,
/**
* Specifies how close to the edge of the window the popover can appear.
*/
marginThreshold?: number,
/**
* Callback fired before the component is entering.
*/
onEnter?: TransitionCallback,
/**
* Callback fired when the component is entering.
*/
onEntering?: TransitionCallback,
/**
* Callback fired when the component has entered.
*/
onEntered?: TransitionCallback,
/**
* Callback fired before the component is exiting.
*/
onExit?: TransitionCallback,
/**
* Callback fired when the component is exiting.
*/
onExiting?: TransitionCallback,
/**
* Callback fired when the component has exited.
*/
onExited?: TransitionCallback,
/**
* Callback fired when the component requests to be closed.
*
* @param {object} event The event source of the callback.
*/
onRequestClose?: Function,
/**
* If `true`, the popover is visible.
*/
open: boolean,
/**
* Properties applied to the `Paper` element.
*/
PaperProps?: Object,
/**
* @ignore
*/
role?: string,
/**
* This is the point on the popover which
* will attach to the anchor's origin.
*
* Options:
* vertical: [top, center, bottom, x(px)];
* horizontal: [left, center, right, x(px)].
*/
transformOrigin?: Origin,
/**
* The animation classNames applied to the component as it enters or exits.
* This property is a direct binding to [`CSSTransition.classNames`](https://reactcommunity.org/react-transition-group/#CSSTransition-prop-classNames).
*/
transitionClasses?: TransitionClasses,
/**
* Set to 'auto' to automatically calculate transition time based on height.
*/
transitionDuration?: number | { enter?: number, exit?: number } | 'auto',
};
class Popover extends React.Component<ProvidedProps & Props> {
static defaultProps = {
anchorOrigin: {
vertical: 'top',
horizontal: 'left',
},
transformOrigin: {
vertical: 'top',
horizontal: 'left',
},
transitionDuration: 'auto',
elevation: 8,
marginThreshold: 16,
};
componentWillUnmount = () => {
this.handleResize.cancel();
};
transitionEl = undefined;
setPositioningStyles = (element: HTMLElement) => {
if (element && element.style) {
const positioning = this.getPositioningStyle(element);
element.style.top = positioning.top;
element.style.left = positioning.left;
element.style.transformOrigin = positioning.transformOrigin;
}
};
handleEnter = (element: HTMLElement) => {
if (this.props.onEnter) {
this.props.onEnter(element);
}
this.setPositioningStyles(element);
};
handleResize = debounce(() => {
const element: any = ReactDOM.findDOMNode(this.transitionEl);
this.setPositioningStyles(element);
}, 166);
getPositioningStyle = element => {
const { marginThreshold } = this.props;
// Check if the parent has requested anchoring on an inner content node
const contentAnchorOffset = this.getContentAnchorOffset(element);
// Get the offset of of the anchoring element
const anchorOffset = this.getAnchorOffset(contentAnchorOffset);
const elemRect = {
width: element.clientWidth,
height: element.clientHeight,
};
// Get the transform origin point on the element itself
const transformOrigin = this.getTransformOrigin(elemRect, contentAnchorOffset);
// Calculate element positioning
let top = anchorOffset.top - transformOrigin.vertical;
let left = anchorOffset.left - transformOrigin.horizontal;
const bottom = top + elemRect.height;
const right = left + elemRect.width;
// Window thresholds taking required margin into account
const heightThreshold = window.innerHeight - marginThreshold;
const widthThreshold = window.innerWidth - marginThreshold;
// Check if the vertical axis needs shifting
if (top < marginThreshold) {
const diff = top - marginThreshold;
top -= diff;
transformOrigin.vertical += diff;
} else if (bottom > heightThreshold) {
const diff = bottom - heightThreshold;
top -= diff;
transformOrigin.vertical += diff;
}
warning(
elemRect.height < heightThreshold || !elemRect.height || !heightThreshold,
[
'Material-UI: the Popover component is too tall.',
`Some part of it can not be seen on the screen (${elemRect.height - heightThreshold}px).`,
'Please consider adding a `max-height` to improve the user-experience.',
].join('\n'),
);
// Check if the horizontal axis needs shifting
if (left < marginThreshold) {
const diff = left - marginThreshold;
left -= diff;
transformOrigin.horizontal += diff;
} else if (right > widthThreshold) {
const diff = right - widthThreshold;
left -= diff;
transformOrigin.horizontal += diff;
}
return {
top: `${top}px`,
left: `${left}px`,
transformOrigin: getTransformOriginValue(transformOrigin),
};
};
handleGetOffsetTop = getOffsetTop;
handleGetOffsetLeft = getOffsetLeft;
// Returns the top/left offset of the position
// to attach to on the anchor element (or body if none is provided)
getAnchorOffset(contentAnchorOffset) {
// $FlowFixMe
const { anchorEl, anchorOrigin } = this.props;
const anchorElement = anchorEl || document.body;
const anchorRect = anchorElement.getBoundingClientRect();
const anchorVertical = contentAnchorOffset === 0 ? anchorOrigin.vertical : 'center';
return {
top: anchorRect.top + this.handleGetOffsetTop(anchorRect, anchorVertical),
left: anchorRect.left + this.handleGetOffsetLeft(anchorRect, anchorOrigin.horizontal),
};
}
// Returns the vertical offset of inner content to anchor the transform on if provided
getContentAnchorOffset(element) {
let contentAnchorOffset = 0;
if (this.props.getContentAnchorEl) {
const contentAnchorEl = this.props.getContentAnchorEl(element);
if (contentAnchorEl && contains(element, contentAnchorEl)) {
const scrollTop = getScrollParent(element, contentAnchorEl);
contentAnchorOffset =
contentAnchorEl.offsetTop + contentAnchorEl.clientHeight / 2 - scrollTop || 0;
}
// != the default value
warning(
this.props.anchorOrigin.vertical === 'top',
[
'Material-UI: you can not change the `anchorOrigin.vertical` value when also ',
'providing the `getContentAnchorEl` property. Pick one.',
].join(),
);
}
return contentAnchorOffset;
}
// Return the base transform origin using the element
// and taking the content anchor offset into account if in use
getTransformOrigin(elemRect, contentAnchorOffset = 0) {
const { transformOrigin } = this.props;
return {
vertical: this.handleGetOffsetTop(elemRect, transformOrigin.vertical) + contentAnchorOffset,
horizontal: this.handleGetOffsetLeft(elemRect, transformOrigin.horizontal),
};
}
render() {
const {
anchorEl,
anchorOrigin,
children,
classes,
elevation,
getContentAnchorEl,
marginThreshold,
onEnter,
onEntering,
onEntered,
onExit,
onExiting,
onExited,
open,
PaperProps,
role,
transformOrigin,
transitionClasses,
transitionDuration,
...other
} = this.props;
return (
<Modal show={open} BackdropInvisible {...other}>
<Grow
appear
in={open}
onEnter={this.handleEnter}
onEntering={onEntering}
onEntered={onEntered}
onExit={onExit}
onExiting={onExiting}
onExited={onExited}
role={role}
transitionClasses={transitionClasses}
timeout={transitionDuration}
rootRef={node => {
this.transitionEl = node;
}}
>
<Paper
data-mui-test="Popover"
className={classes.paper}
elevation={elevation}
{...PaperProps}
>
<EventListener target="window" onResize={this.handleResize} />
{children}
</Paper>
</Grow>
</Modal>
);
}
}
export default withStyles(styles, { name: 'MuiPopover' })(Popover);