-
Notifications
You must be signed in to change notification settings - Fork 406
/
Copy pathOverlay.tsx
60 lines (49 loc) · 1.74 KB
/
Overlay.tsx
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
import PropTypes from 'prop-types';
import { CSSProperties, ReactElement, RefCallback } from 'react';
import useOverlay, { OverlayOptions, ReferenceElement } from './useOverlay';
import { ALIGN_VALUES } from '../../constants';
import { noop } from '../../utils';
// `Element` is not defined during server-side rendering, so shim it here.
/* istanbul ignore next */
const SafeElement = typeof Element === 'undefined' ? noop : Element;
const propTypes = {
/**
* Specify menu alignment. The default value is `justify`, which makes the
* menu as wide as the input and truncates long values. Specifying `left`
* or `right` will align the menu to that side and the width will be
* determined by the length of menu item values.
*/
align: PropTypes.oneOf(ALIGN_VALUES),
children: PropTypes.func.isRequired,
/**
* Specify whether the menu should appear above the input.
*/
dropup: PropTypes.bool,
/**
* Whether or not to automatically adjust the position of the menu when it
* reaches the viewport boundaries.
*/
flip: PropTypes.bool,
isMenuShown: PropTypes.bool,
positionFixed: PropTypes.bool,
// @ts-ignore
referenceElement: PropTypes.instanceOf(SafeElement),
};
export interface OverlayRenderProps {
innerRef: RefCallback<HTMLElement>;
style: CSSProperties;
}
export interface OverlayProps extends OverlayOptions {
children: (props: OverlayRenderProps) => ReactElement | null;
isMenuShown: boolean;
referenceElement: ReferenceElement;
}
const Overlay = ({ referenceElement, isMenuShown, ...props }: OverlayProps) => {
const overlayProps = useOverlay(referenceElement, props);
if (!isMenuShown) {
return null;
}
return props.children(overlayProps);
};
Overlay.propTypes = propTypes;
export default Overlay;