-
Notifications
You must be signed in to change notification settings - Fork 2
/
Popover.js
106 lines (93 loc) · 2.52 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
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import ListSpacingContext from './contexts/listSpacing';
import icBEM from './utils/icBEM';
import prefixClass from './utils/prefixClass';
import anchored, {
anchoredPropTypes,
ANCHORED_PLACEMENT,
} from './mixins/anchored';
import closable from './mixins/closable';
import renderToLayer from './mixins/renderToLayer';
import './styles/Popover.scss';
export const COMPONENT_NAME = prefixClass('popover');
const ROOT_BEM = icBEM(COMPONENT_NAME);
export const BEM = {
root: ROOT_BEM,
arrow: ROOT_BEM.element('arrow'),
container: ROOT_BEM.element('container'),
};
const POPOVER_PADDING = 24;
function Popover({
onClick,
// from anchored()
placement,
arrowStyle,
nodeRef,
remainingSpace,
// from closable()
onInsideClick,
// React props
className,
children,
...otherProps
}) {
const bemClass = BEM.root.modifier(placement);
const rootClassName = classNames(bemClass.toString(), className);
/**
* The `remainingSpace` is the space for whole popover.
* What we want here is to always show keep `remainingSpace === popoverHeight`
* The `maxHeight` is for `BEM.container`, which doesn't include root class padding.
* So we need to minus POPOVER_PADDING here.
*/
const maxHeight = remainingSpace ? remainingSpace - POPOVER_PADDING : undefined;
const handleWrapperClick = (event) => {
onInsideClick(event);
onClick(event);
};
return (
<ListSpacingContext.Provider value={false}>
<div
role="presentation"
className={rootClassName}
ref={nodeRef}
onClick={handleWrapperClick}
{...otherProps}
>
<span className={BEM.arrow} style={arrowStyle} />
<div
className={BEM.container}
style={{ maxHeight }}
>
{children}
</div>
</div>
</ListSpacingContext.Provider>
);
}
Popover.propTypes = {
onClick: PropTypes.func,
placement: anchoredPropTypes.placement,
arrowStyle: anchoredPropTypes.arrowStyle,
nodeRef: anchoredPropTypes.nodeRef,
remainingSpace: anchoredPropTypes.remainingSpace,
onInsideClick: PropTypes.func.isRequired,
};
Popover.defaultProps = {
onClick: () => {},
placement: ANCHORED_PLACEMENT.BOTTOM,
arrowStyle: {},
nodeRef: undefined,
remainingSpace: undefined,
};
export { Popover as PurePopover };
export default renderToLayer(
closable({
onEscape: true,
onClickOutside: true,
onClickInside: true,
})(
anchored()(Popover)
)
);