-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
useFloating.ts
173 lines (155 loc) · 4.94 KB
/
useFloating.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
import {useFloating as usePosition} from '@floating-ui/react-dom';
import {isElement} from '@floating-ui/utils/dom';
import * as React from 'react';
import useModernLayoutEffect from 'use-isomorphic-layout-effect';
import {
useFloatingParentNodeId,
useFloatingTree,
} from '../components/FloatingTree';
import type {
ContextData,
FloatingContext,
NarrowedElement,
OpenChangeReason,
ReferenceType,
UseFloatingOptions,
UseFloatingReturn,
} from '../types';
import {createPubSub} from '../utils/createPubSub';
import {useId} from './useId';
import {useEffectEvent} from './utils/useEffectEvent';
import {error} from '../utils/log';
/**
* Provides data to position a floating element and context to add interactions.
* @see https://floating-ui.com/docs/useFloating
*/
export function useFloating<RT extends ReferenceType = ReferenceType>(
options: Partial<UseFloatingOptions> = {},
): UseFloatingReturn<RT> {
const {open = false, onOpenChange: unstable_onOpenChange, nodeId} = options;
const [_domReference, setDomReference] =
React.useState<NarrowedElement<RT> | null>(null);
const [positionReference, _setPositionReference] =
React.useState<ReferenceType | null>(null);
const optionDomReference = options.elements?.reference;
const domReference = (optionDomReference ||
_domReference) as NarrowedElement<RT>;
if (__DEV__) {
if (optionDomReference && !isElement(optionDomReference)) {
error(
'Cannot pass a virtual element to the `elements.reference` option,',
'as it must be a real DOM element. Use `refs.setPositionReference()`',
'instead.',
);
}
}
useModernLayoutEffect(() => {
if (domReference) {
domReferenceRef.current = domReference;
}
}, [domReference]);
const position = usePosition({
...options,
elements: {
...options.elements,
...(positionReference && {reference: positionReference}),
},
});
const tree = useFloatingTree<RT>();
const nested = useFloatingParentNodeId() != null;
const onOpenChange = useEffectEvent(
(open: boolean, event?: Event, reason?: OpenChangeReason) => {
if (open) {
dataRef.current.openEvent = event;
}
events.emit('openchange', {open, event, reason, nested});
unstable_onOpenChange?.(open, event, reason);
},
);
const domReferenceRef = React.useRef<NarrowedElement<RT> | null>(null);
const dataRef = React.useRef<ContextData>({});
const events = React.useState(() => createPubSub())[0];
const floatingId = useId();
const setPositionReference = React.useCallback(
(node: ReferenceType | null) => {
const computedPositionReference = isElement(node)
? {
getBoundingClientRect: () => node.getBoundingClientRect(),
contextElement: node,
}
: node;
// Store the positionReference in state if the DOM reference is specified externally via the
// `elements.reference` option. This ensures that it won't be overridden on future renders.
_setPositionReference(computedPositionReference);
position.refs.setReference(computedPositionReference);
},
[position.refs],
);
const setReference = React.useCallback(
(node: RT | null) => {
if (isElement(node) || node === null) {
(domReferenceRef as React.MutableRefObject<Element | null>).current =
node;
setDomReference(node as NarrowedElement<RT> | null);
}
// Backwards-compatibility for passing a virtual element to `reference`
// after it has set the DOM reference.
if (
isElement(position.refs.reference.current) ||
position.refs.reference.current === null ||
// Don't allow setting virtual elements using the old technique back to
// `null` to support `positionReference` + an unstable `reference`
// callback ref.
(node !== null && !isElement(node))
) {
position.refs.setReference(node);
}
},
[position.refs],
);
const refs = React.useMemo(
() => ({
...position.refs,
setReference,
setPositionReference,
domReference: domReferenceRef,
}),
[position.refs, setReference, setPositionReference],
);
const elements = React.useMemo(
() => ({
...position.elements,
domReference: domReference,
}),
[position.elements, domReference],
);
const context = React.useMemo<FloatingContext<RT>>(
() => ({
...position,
refs,
elements,
dataRef,
nodeId,
floatingId,
events,
open,
onOpenChange,
}),
[position, nodeId, floatingId, events, open, onOpenChange, refs, elements],
);
useModernLayoutEffect(() => {
const node = tree?.nodesRef.current.find((node) => node.id === nodeId);
if (node) {
node.context = context;
}
});
return React.useMemo(
() => ({
...position,
context,
refs,
elements,
}),
[position, refs, elements, context],
) as UseFloatingReturn<RT>;
}