-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
PopoverDropdown.tsx
142 lines (130 loc) · 3.96 KB
/
PopoverDropdown.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
import { useFocusReturn, useMergedRef } from '@mantine/hooks';
import {
Box,
BoxProps,
closeOnEscape,
CompoundStylesApiProps,
ElementProps,
factory,
Factory,
rem,
useProps,
} from '../../../core';
import { FloatingArrow } from '../../Floating';
import { FocusTrap } from '../../FocusTrap';
import { OptionalPortal } from '../../Portal';
import { Transition } from '../../Transition';
import type { PopoverStylesNames } from '../Popover';
import { usePopoverContext } from '../Popover.context';
import classes from '../Popover.module.css';
export interface PopoverDropdownProps
extends BoxProps,
CompoundStylesApiProps<PopoverDropdownFactory>,
ElementProps<'div'> {}
export type PopoverDropdownFactory = Factory<{
props: PopoverDropdownProps;
ref: HTMLDivElement;
stylesNames: PopoverStylesNames;
compound: true;
}>;
const defaultProps: Partial<PopoverDropdownProps> = {};
export const PopoverDropdown = factory<PopoverDropdownFactory>((_props, ref) => {
const props = useProps('PopoverDropdown', defaultProps, _props);
const {
className,
style,
vars,
children,
onKeyDownCapture,
variant,
classNames,
styles,
...others
} = props;
const ctx = usePopoverContext();
const returnFocus = useFocusReturn({
opened: ctx.opened,
shouldReturnFocus: ctx.returnFocus,
});
const accessibleProps = ctx.withRoles
? {
'aria-labelledby': ctx.getTargetId(),
id: ctx.getDropdownId(),
role: 'dialog',
tabIndex: -1,
}
: {};
const mergedRef = useMergedRef(ref, ctx.floating);
if (ctx.disabled) {
return null;
}
return (
<OptionalPortal {...ctx.portalProps} withinPortal={ctx.withinPortal}>
<Transition
mounted={ctx.opened}
{...ctx.transitionProps}
transition={ctx.transitionProps?.transition || 'fade'}
duration={ctx.transitionProps?.duration ?? 150}
keepMounted={ctx.keepMounted}
exitDuration={
typeof ctx.transitionProps?.exitDuration === 'number'
? ctx.transitionProps.exitDuration
: ctx.transitionProps?.duration
}
>
{(transitionStyles) => (
<FocusTrap active={ctx.trapFocus} innerRef={mergedRef}>
<Box
{...accessibleProps}
{...others}
variant={variant}
onKeyDownCapture={closeOnEscape(ctx.onClose, {
active: ctx.closeOnEscape,
onTrigger: returnFocus,
onKeyDown: onKeyDownCapture,
})}
data-position={ctx.placement}
data-fixed={ctx.floatingStrategy === 'fixed' || undefined}
{...ctx.getStyles('dropdown', {
className,
props,
classNames,
styles,
style: [
{
...transitionStyles,
zIndex: ctx.zIndex as React.CSSProperties['zIndex'],
top: ctx.y ?? 0,
left: ctx.x ?? 0,
width: ctx.width === 'target' ? undefined : rem(ctx.width),
},
style,
],
})}
>
{children}
<FloatingArrow
ref={ctx.arrowRef}
arrowX={ctx.arrowX}
arrowY={ctx.arrowY}
visible={ctx.withArrow}
position={ctx.placement}
arrowSize={ctx.arrowSize}
arrowRadius={ctx.arrowRadius}
arrowOffset={ctx.arrowOffset}
arrowPosition={ctx.arrowPosition}
{...ctx.getStyles('arrow', {
props,
classNames,
styles,
})}
/>
</Box>
</FocusTrap>
)}
</Transition>
</OptionalPortal>
);
});
PopoverDropdown.classes = classes;
PopoverDropdown.displayName = '@mantine/core/PopoverDropdown';