-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Popover.tsx
332 lines (285 loc) · 9.08 KB
/
Popover.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import { useCallback, useRef, useState } from 'react';
import { useClickOutside, useId } from '@mantine/hooks';
import {
createVarsResolver,
ExtendComponent,
Factory,
getDefaultZIndex,
getRadius,
getShadow,
MantineRadius,
MantineShadow,
StylesApiProps,
useDirection,
useProps,
useStyles,
} from '../../core';
import {
ArrowPosition,
FloatingAxesOffsets,
FloatingPosition,
FloatingStrategy,
getFloatingPosition,
} from '../Floating';
import { PortalProps } from '../Portal';
import { TransitionOverride } from '../Transition';
import { PopoverContextProvider } from './Popover.context';
import { PopoverMiddlewares, PopoverWidth } from './Popover.types';
import { PopoverDropdown } from './PopoverDropdown/PopoverDropdown';
import { PopoverTarget } from './PopoverTarget/PopoverTarget';
import { usePopover } from './use-popover';
import classes from './Popover.module.css';
export type PopoverStylesNames = 'dropdown' | 'arrow';
export type PopoverCssVariables = {
dropdown: '--popover-radius' | '--popover-shadow';
};
export interface __PopoverProps {
/** Dropdown position relative to the target element, `'bottom'` by default */
position?: FloatingPosition;
/** Offset of the dropdown element, `8` by default */
offset?: number | FloatingAxesOffsets;
/** Called when dropdown position changes */
onPositionChange?: (position: FloatingPosition) => void;
/** `useEffect` dependencies to force update dropdown position, `[]` by default */
positionDependencies?: any[];
/** Called when dropdown closes */
onClose?: () => void;
/** Called when dropdown opens */
onOpen?: () => void;
/** If set dropdown will not be unmounted from the DOM when it is hidden, `display: none` styles will be added instead */
keepMounted?: boolean;
/** Props passed down to the `Transition` component that used to animate dropdown presence, use to configure duration and animation type, `{ duration: 150, transition: 'fade' }` by default */
transitionProps?: TransitionOverride;
/** Dropdown width, or `'target'` to make dropdown width the same as target element, `'max-content'` by default */
width?: PopoverWidth;
/** Floating ui middlewares to configure position handling, `{ flip: true, shift: true, inline: false }` by default */
middlewares?: PopoverMiddlewares;
/** Determines whether component should have an arrow, `false` by default */
withArrow?: boolean;
/** Arrow size in px, `7` by default */
arrowSize?: number;
/** Arrow offset in px, `5` by default */
arrowOffset?: number;
/** Arrow `border-radius` in px, `0` by default */
arrowRadius?: number;
/** Arrow position */
arrowPosition?: ArrowPosition;
/** Determines whether dropdown should be rendered within the `Portal`, `true` by default */
withinPortal?: boolean;
/** Props to pass down to the `Portal` when `withinPortal` is true */
portalProps?: Omit<PortalProps, 'children'>;
/** Dropdown `z-index`, `300` by default */
zIndex?: string | number;
/** Key of `theme.radius` or any valid CSS value to set border-radius, `theme.defaultRadius` by default */
radius?: MantineRadius;
/** Key of `theme.shadows` or any other valid CSS `box-shadow` value */
shadow?: MantineShadow;
/** If set, popover dropdown will not be rendered */
disabled?: boolean;
/** Determines whether focus should be automatically returned to control when dropdown closes, `false` by default */
returnFocus?: boolean;
/** Changes floating ui [position strategy](https://floating-ui.com/docs/usefloating#strategy), `'absolute'` by default */
floatingStrategy?: FloatingStrategy;
}
export interface PopoverProps extends __PopoverProps, StylesApiProps<PopoverFactory> {
__staticSelector?: string;
/** `Popover.Target` and `Popover.Dropdown` components */
children?: React.ReactNode;
/** Initial opened state for uncontrolled component */
defaultOpened?: boolean;
/** Controlled dropdown opened state */
opened?: boolean;
/** Called with current state when dropdown opens or closes */
onChange?: (opened: boolean) => void;
/** Determines whether dropdown should be closed on outside clicks, `true` by default */
closeOnClickOutside?: boolean;
/** Events that trigger outside clicks */
clickOutsideEvents?: string[];
/** Determines whether focus should be trapped within dropdown, `false` by default */
trapFocus?: boolean;
/** Determines whether dropdown should be closed when `Escape` key is pressed, `true` by default */
closeOnEscape?: boolean;
/** id base to create accessibility connections */
id?: string;
/** Determines whether dropdown and target elements should have accessible roles, `true` by default */
withRoles?: boolean;
}
export type PopoverFactory = Factory<{
props: PopoverProps;
stylesNames: PopoverStylesNames;
vars: PopoverCssVariables;
}>;
const defaultProps: Partial<PopoverProps> = {
position: 'bottom',
offset: 8,
positionDependencies: [],
transitionProps: { transition: 'fade', duration: 150 },
middlewares: { flip: true, shift: true, inline: false },
arrowSize: 7,
arrowOffset: 5,
arrowRadius: 0,
arrowPosition: 'side',
closeOnClickOutside: true,
withinPortal: true,
closeOnEscape: true,
trapFocus: false,
withRoles: true,
returnFocus: false,
clickOutsideEvents: ['mousedown', 'touchstart'],
zIndex: getDefaultZIndex('popover'),
__staticSelector: 'Popover',
width: 'max-content',
};
const varsResolver = createVarsResolver<PopoverFactory>((_, { radius, shadow }) => ({
dropdown: {
'--popover-radius': radius === undefined ? undefined : getRadius(radius),
'--popover-shadow': getShadow(shadow),
},
}));
export function Popover(_props: PopoverProps) {
const props = useProps('Popover', defaultProps, _props);
const {
children,
position,
offset,
onPositionChange,
positionDependencies,
opened,
transitionProps,
width,
middlewares,
withArrow,
arrowSize,
arrowOffset,
arrowRadius,
arrowPosition,
unstyled,
classNames,
styles,
closeOnClickOutside,
withinPortal,
portalProps,
closeOnEscape,
clickOutsideEvents,
trapFocus,
onClose,
onOpen,
onChange,
zIndex,
radius,
shadow,
id,
defaultOpened,
__staticSelector,
withRoles,
disabled,
returnFocus,
variant,
keepMounted,
vars,
floatingStrategy,
...others
} = props;
const getStyles = useStyles<PopoverFactory>({
name: __staticSelector!,
props,
classes,
classNames,
styles,
unstyled,
rootSelector: 'dropdown',
vars,
varsResolver,
});
const arrowRef = useRef<HTMLDivElement | null>(null);
const [targetNode, setTargetNode] = useState<HTMLElement | null>(null);
const [dropdownNode, setDropdownNode] = useState<HTMLElement | null>(null);
const { dir } = useDirection();
const uid = useId(id);
const popover = usePopover({
middlewares,
width,
position: getFloatingPosition(dir, position!),
offset: typeof offset === 'number' ? offset + (withArrow ? arrowSize! / 2 : 0) : offset!,
arrowRef,
arrowOffset: arrowOffset!,
onPositionChange,
positionDependencies,
opened,
defaultOpened,
onChange,
onOpen,
onClose,
strategy: floatingStrategy,
});
useClickOutside(() => closeOnClickOutside && popover.onClose(), clickOutsideEvents, [
targetNode,
dropdownNode,
]);
const reference = useCallback(
(node: HTMLElement) => {
setTargetNode(node);
popover.floating.refs.setReference(node);
},
[popover.floating.refs.setReference]
);
const floating = useCallback(
(node: HTMLElement) => {
setDropdownNode(node);
popover.floating.refs.setFloating(node);
},
[popover.floating.refs.setFloating]
);
return (
<PopoverContextProvider
value={{
returnFocus,
disabled,
controlled: popover.controlled,
reference,
floating,
x: popover.floating.x!,
y: popover.floating.y!,
arrowX: popover.floating?.middlewareData?.arrow?.x,
arrowY: popover.floating?.middlewareData?.arrow?.y,
opened: popover.opened,
arrowRef,
transitionProps,
width,
withArrow,
arrowSize: arrowSize!,
arrowOffset: arrowOffset!,
arrowRadius: arrowRadius!,
arrowPosition: arrowPosition!,
placement: popover.floating.placement,
trapFocus,
withinPortal,
portalProps,
zIndex,
radius,
shadow,
closeOnEscape,
onClose: popover.onClose,
onToggle: popover.onToggle,
getTargetId: () => `${uid}-target`,
getDropdownId: () => `${uid}-dropdown`,
withRoles,
targetProps: others,
__staticSelector: __staticSelector!,
classNames,
styles,
unstyled,
variant,
keepMounted,
getStyles,
floatingStrategy,
}}
>
{children}
</PopoverContextProvider>
);
}
Popover.Target = PopoverTarget;
Popover.Dropdown = PopoverDropdown;
Popover.displayName = '@mantine/core/Popover';
Popover.extend = (input: ExtendComponent<PopoverFactory>) => input;