-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathPopover.js
170 lines (154 loc) · 4.32 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
/**
* Popover
*/
import React, { useRef, useState, useEffect, forwardRef } from 'react';
import get from 'lodash/get';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import Transition from 'react-transition-group/Transition';
import RootCloseWrapper from '../../util/RootCloseWrapper';
import Popper, { AVAILABLE_PLACEMENTS } from '../Popper';
import css from './Popover.css';
import LegacyPopover from './LegacyPopover';
/* Check if the legacy implementation is used */
const isLegacy = (childrenProp) => {
return Array.isArray(childrenProp)
&& get(React.Children.toArray(childrenProp), '[0].props[data-role]') === 'target';
};
const Popover = forwardRef(({
anchorRef: anchorRefProp,
children,
className,
modifiers: modifiersProp = {},
noPadding = false,
onToggle,
offset = 5,
open: openProp,
placement = 'bottom',
popperProps = {},
renderTrigger,
}, ref) => {
const [open, setOpen] = useState(false);
const internalAnchorRef = useRef(null);
const internalPopoverRef = useRef(null);
const anchorRef = anchorRefProp || internalAnchorRef;
const popoverRef = ref || internalPopoverRef;
const internalToggleRef = useRef(() => {
setOpen((cur) => {
if (cur &&
(popoverRef.current === document.activeElement ||
popoverRef.current?.contains(document.activeElement)
)
) {
anchorRef.current?.focus(); // eslint-disable-line
}
return !cur;
});
}).current;
const handleToggle = typeof onToggle === 'function' ? onToggle : () => internalToggleRef();
const modifiers = { offset: { enabled: true, offset: `0,${offset}` }, ...modifiersProp };
const renderProps = {
ref: anchorRef,
toggle: handleToggle,
open,
};
/**
* Controlled externally
*/
useEffect(() => {
setOpen(openProp);
}, [openProp]);
/**
* Focus content when popover opens
*/
useEffect(() => {
if (open && popoverRef.current) {
popoverRef.current.focus();
}
}, [open, popoverRef]);
//
const handleKeyDown = (e) => {
if (e.key === 'Escape') {
e.stopPropagation();
}
};
const renderPopover = transitionStatus => (
// eslint-disable-next-line
<div
data-test-popover-overlay
className={
classnames(
css.popover,
{ [css.noPadding]: noPadding },
css.transition,
css[`transition-${transitionStatus}`],
className
)}
ref={popoverRef}
role="dialog"
tabIndex="-1"
onKeyDown={handleKeyDown}
>
{typeof children === 'function' ? children(renderProps) : children}
</div>
);
return (
<>
{typeof renderTrigger === 'function' && renderTrigger(renderProps)}
<Popper
isOpen={open}
anchorRef={anchorRef}
placement={placement}
modifiers={modifiers}
{...popperProps}
>
<RootCloseWrapper onRootClose={handleToggle} ref={popoverRef}>
<Transition appear timeout={70} in={open}>
{renderPopover}
</Transition>
</RootCloseWrapper>
</Popper>
</>
);
});
const checkForLegacyChildrenProp = (props) => {
if (isLegacy(props.children)) {
throw new Error(`
You are using a legacy implementation of the <Popover>-component.
See the documentation for this component to learn how to update
your implementation to use the new component API. Docs:
https://github.com/folio-org/stripes-components/blob/master/lib/Popover/readme.md
`);
}
};
Popover.propTypes = {
activeClass: PropTypes.string,
anchorRef: PropTypes.shape({
current: PropTypes.instanceOf(Element)
}),
children: PropTypes.oneOfType([
checkForLegacyChildrenProp,
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
PropTypes.func,
]).isRequired,
className: PropTypes.string,
modifiers: PropTypes.object,
noPadding: PropTypes.bool,
offset: PropTypes.number,
onToggle: PropTypes.func,
open: PropTypes.bool,
placement: PropTypes.oneOf(AVAILABLE_PLACEMENTS),
popperProps: PropTypes.object,
renderTrigger: PropTypes.func,
};
/**
* Legacy support
*/
const PopoverWithLegacySupport = (props) => { /* eslint-disable react/prop-types */
if (isLegacy(props.children)) {
return <LegacyPopover {...props} />;
}
return <Popover {...props} />;
};
export default PopoverWithLegacySupport;