-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathuse-popover.ts
179 lines (157 loc) · 4.57 KB
/
use-popover.ts
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
import {
arrow,
flip,
inline,
limitShift,
Middleware,
offset,
shift,
size,
useFloating,
UseFloatingReturn,
} from '@floating-ui/react';
import { useDidUpdate, useUncontrolled } from '@mantine/hooks';
import {
FloatingAxesOffsets,
FloatingPosition,
FloatingStrategy,
useFloatingAutoUpdate,
} from '../Floating';
import { PopoverMiddlewares, PopoverWidth } from './Popover.types';
interface UsePopoverOptions {
offset: number | FloatingAxesOffsets;
position: FloatingPosition;
positionDependencies: any[] | undefined;
onPositionChange?: (position: FloatingPosition) => void;
opened: boolean | undefined;
defaultOpened: boolean | undefined;
onChange?: (opened: boolean) => void;
onClose?: () => void;
onOpen?: () => void;
width: PopoverWidth;
middlewares: PopoverMiddlewares | undefined;
arrowRef: React.RefObject<HTMLDivElement>;
arrowOffset: number;
strategy?: FloatingStrategy;
}
function getDefaultMiddlewares(middlewares: PopoverMiddlewares | undefined): PopoverMiddlewares {
if (middlewares === undefined) {
return { shift: true, flip: true };
}
const result = { ...middlewares };
if (middlewares.shift === undefined) {
result.shift = true;
}
if (middlewares.flip === undefined) {
result.flip = true;
}
return result;
}
function getPopoverMiddlewares(
options: UsePopoverOptions,
getFloating: () => UseFloatingReturn<Element>
) {
const middlewaresOptions = getDefaultMiddlewares(options.middlewares);
const middlewares: Middleware[] = [offset(options.offset)];
if (middlewaresOptions.shift) {
middlewares.push(
shift(
typeof middlewaresOptions.shift === 'boolean'
? { limiter: limitShift(), padding: 5 }
: { limiter: limitShift(), padding: 5, ...middlewaresOptions.shift }
)
);
}
if (middlewaresOptions.flip) {
middlewares.push(
typeof middlewaresOptions.flip === 'boolean' ? flip() : flip(middlewaresOptions.flip)
);
}
if (middlewaresOptions.inline) {
middlewares.push(
typeof middlewaresOptions.inline === 'boolean' ? inline() : inline(middlewaresOptions.inline)
);
}
middlewares.push(arrow({ element: options.arrowRef, padding: options.arrowOffset }));
if (middlewaresOptions.size || options.width === 'target') {
middlewares.push(
size({
...(typeof middlewaresOptions.size === 'boolean' ? {} : middlewaresOptions.size),
apply({ rects, availableWidth, availableHeight, ...rest }) {
const floating = getFloating();
const styles = floating.refs.floating.current?.style ?? {};
if (middlewaresOptions.size) {
//If custom apply function is given use that else set defaults
if (typeof middlewaresOptions.size === 'object' && !!middlewaresOptions.size.apply) {
middlewaresOptions.size.apply({ rects, availableWidth, availableHeight, ...rest });
} else {
Object.assign(styles, {
maxWidth: `${availableWidth}px`,
maxHeight: `${availableHeight}px`,
});
}
}
if (options.width === 'target') {
Object.assign(styles, {
width: `${rects.reference.width}px`,
});
}
},
})
);
}
return middlewares;
}
export function usePopover(options: UsePopoverOptions) {
const [_opened, setOpened, isControlled] = useUncontrolled({
value: options.opened,
defaultValue: options.defaultOpened,
finalValue: false,
onChange: options.onChange,
});
const onClose = () => {
if (_opened) {
options.onClose?.();
setOpened(false);
}
};
const onToggle = () => {
if (_opened) {
options.onClose?.();
setOpened(false);
} else {
options.onOpen?.();
setOpened(true);
}
};
const floating: UseFloatingReturn<Element> = useFloating({
strategy: options.strategy,
placement: options.position,
middleware: getPopoverMiddlewares(options, () => floating),
});
useFloatingAutoUpdate({
opened: _opened,
position: options.position,
positionDependencies: options.positionDependencies || [],
floating,
});
useDidUpdate(() => {
options.onPositionChange?.(floating.placement);
}, [floating.placement]);
useDidUpdate(() => {
if (!isControlled) {
if (!options.opened) {
options.onClose?.();
} else {
options.onOpen?.();
}
}
}, [options.opened, isControlled]);
return {
floating,
controlled: typeof options.opened === 'boolean',
opened: _opened,
onClose,
onToggle,
};
}