1 | import * as React from 'react';
|
2 | import * as PropTypes from 'prop-types';
|
3 | import { PopoverProps as ReakitPopoverProps } from 'reakit/ts';
|
4 |
|
5 | import { isFunction } from '../_utils/assert';
|
6 | import TrapFocus from '../_utils/TrapFocus';
|
7 | import { Box } from '../primitives';
|
8 | import {
|
9 | AnimateProps,
|
10 | Placement,
|
11 | RestrictHideProps,
|
12 | animateDefaultProps,
|
13 | placementPropType,
|
14 | restrictDefaultProps,
|
15 | animatePropTypes,
|
16 | restrictHidePropTypes
|
17 | } from '../types';
|
18 | import _Popover from './styled';
|
19 |
|
20 | export type LocalPopoverPopoverProps = {
|
21 | children: React.ReactNode | (({ initialFocusRef }: { initialFocusRef?: React.RefObject<any> }) => React.ReactNode);
|
22 | className?: string;
|
23 | /** Whether or not to show the popover component */
|
24 | isVisible?: boolean;
|
25 | flip?: boolean;
|
26 | gutter?: number | string;
|
27 | placement?: Placement;
|
28 | shift?: boolean;
|
29 | /** Traps focus within the popover */
|
30 | trapFocus?: boolean;
|
31 | } & AnimateProps &
|
32 | RestrictHideProps;
|
33 | export type PopoverPopoverProps = LocalPopoverPopoverProps & ReakitPopoverProps;
|
34 |
|
35 | export const PopoverPopover: React.FunctionComponent<LocalPopoverPopoverProps> = ({
|
36 | children,
|
37 | isVisible,
|
38 | trapFocus,
|
39 | ...props
|
40 | }) => (
|
41 | <_Popover visible={isVisible} {...props}>
|
42 | {trapFocus ? (
|
43 | <TrapFocus isActive={isVisible}>
|
44 | {({ fallbackFocusRef, initialFocusRef }) => (
|
45 | <Box elementRef={fallbackFocusRef}>{isFunction(children) ? children({ initialFocusRef }) : children}</Box>
|
46 | )}
|
47 | </TrapFocus>
|
48 | ) : isFunction(children) ? (
|
49 | children({})
|
50 | ) : (
|
51 | children
|
52 | )}
|
53 | </_Popover>
|
54 | );
|
55 |
|
56 | export const popoverPopoverPropTypes = {
|
57 | children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
|
58 | className: PropTypes.string,
|
59 | isVisible: PropTypes.bool,
|
60 | flip: PropTypes.bool,
|
61 | gutter: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
62 | placement: placementPropType,
|
63 | shift: PropTypes.bool,
|
64 | trapFocus: PropTypes.bool,
|
65 | ...animatePropTypes,
|
66 | ...restrictHidePropTypes
|
67 | };
|
68 | PopoverPopover.propTypes = popoverPopoverPropTypes;
|
69 |
|
70 | export const popoverPopoverDefaultProps: Partial<LocalPopoverPopoverProps> = {
|
71 | className: undefined,
|
72 | flip: true,
|
73 | gutter: 12,
|
74 | isVisible: false,
|
75 | placement: 'bottom',
|
76 | shift: true,
|
77 | trapFocus: false,
|
78 | ...animateDefaultProps,
|
79 | ...restrictDefaultProps,
|
80 | hideOnEsc: true,
|
81 | hideOnClickOutside: true
|
82 | };
|
83 | PopoverPopover.defaultProps = popoverPopoverDefaultProps;
|
84 |
|
85 | const C: React.FunctionComponent<PopoverPopoverProps> = PopoverPopover;
|
86 | export default C;
|