UNPKG

4.18 kBTypeScriptView Raw
1import * as React from "react";
2import PopperJS from "popper.js";
3import StyledSystem from "styled-system";
4import { BoxProps } from "../Box";
5import { PseudoBoxProps } from "../PseudoBox";
6import { PopperProps } from "../Popper";
7
8interface PopoverContextValue {
9 popoverRef: React.RefObject<HTMLElement>;
10 placement: PopperJS.Placement;
11 referenceRef: React.RefObject<HTMLElement>;
12 headerId: string;
13 bodyId: string;
14 popoverId: string;
15 onOpen: () => void;
16 onClose: () => void;
17 onToggle: () => void;
18 trigger: "hover" | "click";
19 isOpen: boolean;
20 onBlur: React.FocusEventHandler<HTMLElement>;
21 closeOnEsc: boolean;
22 initialFocusRef: boolean;
23 isHoveringRef: React.RefObject<boolean>;
24 usePortal: boolean;
25}
26
27declare const PopoverContext: React.Context<{}>;
28declare const usePopoverContext: () => PopoverContextValue | undefined;
29
30type InternalState = { isOpen?: boolean; onClose?: () => void };
31
32/**
33 * The content of the popover
34 */
35type PopoverChildren =
36 | {
37 children: React.ReactNode;
38 }
39 | { children: (props: InternalState) => React.ReactNode };
40
41interface IPopover {
42 /**
43 * The html `id` attribute of the popover.
44 * If not provided, we generate a unique id.
45 *
46 * This `id` is also used to auto-generate the `aria-labelledby`
47 * and `aria-decribedby` attributes that points to the `PopoverHeader` and `PopoverBody`
48 */
49 id?: string;
50 /**
51 * If `true`, the popover will be opened in controlled mode.
52 */
53 isOpen?: boolean;
54 /**
55 * If `true`, the popover will be initially opened.
56 */
57 defaultIsOpen?: boolean;
58 /**
59 * The `ref` of the element that should receive focus when the popover opens.
60 */
61 initialFocusRef?: React.RefObject<HTMLElement>;
62 /**
63 * The interaction that triggers the popover.
64 *
65 * `hover` - means the popover will open when you hover with mouse or
66 * focus with keyboard on the popover trigger
67 *
68 * `click` - means the popover will open on click or
69 * press `Enter` to `Space` on keyboard
70 */
71 trigger?: "hover" | "click";
72 /**
73 * If `true`, the popover will return focus to the trigger when it closes
74 */
75 returnFocusOnClose?: boolean;
76 /**
77 * The gap (in pixels) to apply between the popover and the target.
78 * Used by `popper.js`
79 */
80 gutter?: number;
81 /**
82 * The placment of the popover
83 */
84 placement?: PopperJS.Placement;
85 /**
86 * If `true`, the popover will close when you blur out it by
87 * clicking outside or tabbing out
88 */
89 closeOnBlur?: boolean;
90 /**
91 * If `true`, the popover will close when you hit the `Esc` key
92 */
93 closeOnEsc?: boolean;
94 /**
95 * Callback fired when the popover opens
96 */
97 onOpen?: () => void;
98 /**
99 * Callback fired when the popover closes
100 */
101 onClose?: () => void;
102 /**
103 * If true the popover is displayed with a Portal.
104 * Rendering content inside a Portal allows the popover content
105 * to escape the physical bounds of its parent while still being
106 * positioned correctly relative to its target
107 */
108 usePortal?: boolean;
109}
110
111type PopoverProps = IPopover & PopoverChildren;
112export const Popover: React.FC<PopoverProps>;
113
114interface IPopoverTrigger {
115 children: React.ReactNode;
116}
117export const PopoverTrigger: React.FC<IPopoverTrigger>;
118
119interface IPopoverContent {
120 onKeyDown?: React.KeyboardEventHandler<HTMLElement>;
121 onBlur?: React.FocusEventHandler<HTMLElement>;
122 onMouseLeave?: React.MouseEventHandler<HTMLElement>;
123 onMouseEnter?: React.MouseEventHandler<HTMLElement>;
124 onFocus?: React.FocusEventHandler<HTMLElement>;
125 gutter?: number;
126 /**
127 * If the `PopoverHeading` isn't rendered, use this prop to add
128 * an accessible label to the popover.
129 */
130 "aria-label"?: string;
131}
132type PopoverContentProps = IPopoverContent & PopperProps;
133export const PopoverContent: React.FC<PopoverContentProps>;
134
135export const PopoverArrow: React.FC<BoxProps>;
136export const PopoverHeader: React.FC<BoxProps>;
137export const PopoverFooter: React.FC<BoxProps>;
138export const PopoverBody: React.FC<BoxProps>;
139
140type PopoverCloseButtonProps = PseudoBoxProps & {
141 onClick?: React.MouseEventHandler<HTMLElement>;
142};
143export const PopoverCloseButton: React.FC<PopoverCloseButtonProps>;