This repository has been archived by the owner on May 13, 2022. It is now read-only.
forked from Faithlife/styled-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popover-base.jsx
208 lines (194 loc) · 5.46 KB
/
popover-base.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
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
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { ThemeProvider } from 'styled-components';
import { Popper } from 'react-popper';
import { colors } from '../shared-styles';
import { PlacementOptionsProps, FocusHandlerInboundsElement } from './popper-helpers';
import * as Styled from './styled';
/** Positioning helper used to display content above another element.
* Refs are not supported, please use PopoverManager and PopoverReference to handle positioning.
*/
export class PopoverBase extends Component {
static propTypes = {
/** Is the popover open */
isOpen: PropTypes.bool,
/** Where on the target the popover renders */
placement: PlacementOptionsProps,
/** Not all modifiers are shown. Refer to https://popper.js.org/popper-documentation.html#modifiers for a full list*/
modifiers: PropTypes.shape({
offset: PropTypes.shape({
enabled: PropTypes.bool,
offset: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
}),
preventOverflow: PropTypes.shape({
padding: PropTypes.number,
boundariesElement: PropTypes.any,
}),
flip: PropTypes.shape({
enabled: PropTypes.bool,
behavior: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
padding: PropTypes.number,
boundariesElement: PropTypes.any,
}),
}),
/** Contents of the Popover */
children: PropTypes.node,
/** Where to inject the popover. Defaults to inline */
container: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.object]),
/** Hide popover arrow */
hideArrow: PropTypes.bool,
/** Delay on popover showing in milliseconds*/
delay: PropTypes.shape({ show: PropTypes.number, hide: PropTypes.number }),
eventsEnabled: PropTypes.bool,
positionFixed: PropTypes.bool,
styleOverrides: PropTypes.shape({
background: PropTypes.string,
border: PropTypes.string,
borderRadius: PropTypes.string,
hideShadow: PropTypes.bool,
fontSize: PropTypes.string,
fontWeight: PropTypes.string,
height: PropTypes.string,
lineHeight: PropTypes.string,
margin: PropTypes.string,
maxHeight: PropTypes.string,
maxWidth: PropTypes.string,
minHeight: PropTypes.string,
minWidth: PropTypes.string,
outline: PropTypes.string,
padding: PropTypes.string,
textAlign: PropTypes.string,
width: PropTypes.string,
zIndex: PropTypes.number,
}),
theme: PropTypes.shape({
backgroundColor: PropTypes.string,
textColor: PropTypes.string,
}),
};
static defaultProps = {
placement: 'top',
theme: {
backgroundColor: colors.white,
},
modifiers: {},
styleOverrides: {},
};
state = {
showPopper: this.props.isOpen || false,
};
_timeout = null;
componentDidMount() {
const { container } = this.props;
if (container) {
if (typeof container === 'string') {
// must be an id or body
this.targetContainer =
container === 'body' ? document.body : document.getElementById(container);
} else {
// must be a ref
this.targetContainer = typeof container === 'object' ? container.current : container();
}
}
}
componentDidUpdate(prevProps) {
this.handleIsPopoverShowing(prevProps.isOpen, this.props.isOpen, this.props.delay);
}
componentWillUnmount() {
if (this._timeout) {
clearTimeout(this._timeout);
}
}
handleIsPopoverShowing = (prevIsOpen, isOpen, delay) => {
if (prevIsOpen !== isOpen && !delay) {
this.setState({ showPopper: isOpen });
} else if (prevIsOpen && !isOpen && !!delay) {
if (this._timeout) {
clearTimeout(this._timeout);
}
this._timeout = setTimeout(() => {
this.setState({ showPopper: false });
this._timeout = null;
}, delay.hide || 0);
} else if (!prevIsOpen && isOpen && !!delay) {
if (this._timeout) {
clearTimeout(this._timeout);
}
this._timeout = setTimeout(() => {
this.setState({ showPopper: true });
this._timeout = null;
}, delay.show || 0);
}
};
render() {
const {
children,
placement: popoverPlacement,
modifiers,
hideArrow,
delay,
theme,
styleOverrides,
eventsEnabled,
positionFixed,
} = this.props;
const { showPopper } = this.state;
const popperModifiers = {
...modifiers,
computeStyle: {
...modifiers.computeStyle,
gpuAcceleration: !(
typeof window !== `undefined` &&
window.devicePixelRatio < 1.5 &&
/Win/.test(navigator.platform)
),
},
};
const popover = (
<Popper
placement={popoverPlacement}
modifiers={popperModifiers}
eventsEnabled={eventsEnabled}
positionFixed={positionFixed}
>
{({ ref, style, placement, arrowProps }) => (
<ThemeProvider theme={theme}>
<Styled.PopoverBase
ref={ref}
placement={placement}
style={{
...style,
...(!hideArrow ? Styled.margins[Styled.getPlacement(placement)] : {}),
}}
delay={delay}
onAnimationEnd={this.handleTransition}
hideArrow={hideArrow}
styleOverrides={styleOverrides}
>
{children}
{!hideArrow && (
<Styled.Arrow
ref={arrowProps.ref}
placement={placement}
style={arrowProps.style}
styleOverrides={styleOverrides}
/>
)}
</Styled.PopoverBase>
</ThemeProvider>
)}
</Popper>
);
if (!!this.targetContainer && showPopper) {
return ReactDOM.createPortal(
<FocusHandlerInboundsElement>{popover}</FocusHandlerInboundsElement>,
this.targetContainer,
);
}
if (showPopper) {
return popover;
}
return null;
}
}