@octanejs/floating-ui 0.1.46 → 0.1.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -0
- package/LICENSE.react.upstream +20 -0
- package/LICENSE.upstream +21 -0
- package/README.md +3 -2
- package/UPSTREAM.md +41 -0
- package/dist/cjs/FloatingFocusManager.cjs +10 -5
- package/dist/cjs/FloatingPortal.cjs +39 -7
- package/dist/cjs/delayGroup.cjs +115 -2
- package/dist/cjs/index.cjs +9 -0
- package/dist/cjs/inner.cjs +193 -0
- package/dist/cjs/useDismiss.cjs +2 -0
- package/dist/cjs/useFloating.cjs +31 -5
- package/dist/cjs/utils/index.cjs +4 -40
- package/dist/cjs/utils/nodes.cjs +87 -0
- package/package.json +39 -8
- package/src/FloatingFocusManager.ts +53 -46
- package/src/FloatingPortal.ts +70 -35
- package/src/delayGroup.ts +154 -1
- package/src/index.ts +9 -0
- package/src/inner.ts +218 -0
- package/src/useDismiss.ts +1 -1
- package/src/useFloating.ts +39 -5
- package/src/utils/index.ts +3 -49
- package/src/utils/nodes.ts +75 -0
package/src/delayGroup.ts
CHANGED
|
@@ -9,11 +9,12 @@ import {
|
|
|
9
9
|
useMemo,
|
|
10
10
|
useReducer,
|
|
11
11
|
useRef,
|
|
12
|
+
useState,
|
|
12
13
|
} from 'octane';
|
|
13
14
|
import type { OctaneNode } from 'octane';
|
|
14
15
|
|
|
15
16
|
import { S, splitSlot, subSlot } from './internal';
|
|
16
|
-
import { getDelay, useModernLayoutEffect } from './utils';
|
|
17
|
+
import { clearTimeoutIfSet, getDelay, useModernLayoutEffect } from './utils';
|
|
17
18
|
import type { Delay, FloatingRootContext } from './types';
|
|
18
19
|
|
|
19
20
|
export interface GroupState {
|
|
@@ -198,3 +199,155 @@ export function useDelayGroup(...args: any[]): GroupContext {
|
|
|
198
199
|
|
|
199
200
|
return groupContext;
|
|
200
201
|
}
|
|
202
|
+
|
|
203
|
+
interface NextContextValue {
|
|
204
|
+
hasProvider: boolean;
|
|
205
|
+
timeoutMs: number;
|
|
206
|
+
delayRef: { current: Delay };
|
|
207
|
+
initialDelayRef: { current: Delay };
|
|
208
|
+
timeoutIdRef: { current: number };
|
|
209
|
+
currentIdRef: { current: any };
|
|
210
|
+
currentContextRef: {
|
|
211
|
+
current: {
|
|
212
|
+
onOpenChange: (open: boolean) => void;
|
|
213
|
+
setIsInstantPhase: (value: boolean) => void;
|
|
214
|
+
} | null;
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const NextFloatingDelayGroupContext = createContext<NextContextValue>({
|
|
219
|
+
hasProvider: false,
|
|
220
|
+
timeoutMs: 0,
|
|
221
|
+
delayRef: { current: 0 },
|
|
222
|
+
initialDelayRef: { current: 0 },
|
|
223
|
+
timeoutIdRef: { current: -1 },
|
|
224
|
+
currentIdRef: { current: null },
|
|
225
|
+
currentContextRef: { current: null },
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
export interface NextFloatingDelayGroupProps {
|
|
229
|
+
children?: OctaneNode;
|
|
230
|
+
delay: Delay;
|
|
231
|
+
timeoutMs?: number;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export function NextFloatingDelayGroup(props: NextFloatingDelayGroupProps): OctaneNode {
|
|
235
|
+
const children = props.children;
|
|
236
|
+
const delay = props.delay;
|
|
237
|
+
const timeoutMs = props.timeoutMs ?? 0;
|
|
238
|
+
const delayRef = useRef(delay, S('NextFloatingDelayGroup:delay'));
|
|
239
|
+
const initialDelayRef = useRef(delay, S('NextFloatingDelayGroup:initialDelay'));
|
|
240
|
+
const currentIdRef = useRef<string | null>(null, S('NextFloatingDelayGroup:currentId'));
|
|
241
|
+
const currentContextRef = useRef<NextContextValue['currentContextRef']['current']>(
|
|
242
|
+
null,
|
|
243
|
+
S('NextFloatingDelayGroup:currentContext'),
|
|
244
|
+
);
|
|
245
|
+
const timeoutIdRef = useRef(-1, S('NextFloatingDelayGroup:timeoutId'));
|
|
246
|
+
const value = useMemo<NextContextValue>(
|
|
247
|
+
() => ({
|
|
248
|
+
hasProvider: true,
|
|
249
|
+
delayRef,
|
|
250
|
+
initialDelayRef,
|
|
251
|
+
currentIdRef,
|
|
252
|
+
timeoutMs,
|
|
253
|
+
currentContextRef,
|
|
254
|
+
timeoutIdRef,
|
|
255
|
+
}),
|
|
256
|
+
[timeoutMs],
|
|
257
|
+
S('NextFloatingDelayGroup:value'),
|
|
258
|
+
);
|
|
259
|
+
return createElement(NextFloatingDelayGroupContext.Provider, { value, children });
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export interface UseNextDelayGroupOptions {
|
|
263
|
+
enabled?: boolean;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export interface UseNextDelayGroupReturn {
|
|
267
|
+
delayRef: { current: Delay };
|
|
268
|
+
isInstantPhase: boolean;
|
|
269
|
+
hasProvider: boolean;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function useNextDelayGroup(
|
|
273
|
+
context: FloatingRootContext,
|
|
274
|
+
options?: UseNextDelayGroupOptions,
|
|
275
|
+
slot?: symbol,
|
|
276
|
+
): UseNextDelayGroupReturn;
|
|
277
|
+
export function useNextDelayGroup(...args: any[]): UseNextDelayGroupReturn {
|
|
278
|
+
const [user, slot] = splitSlot(args);
|
|
279
|
+
const context = user[0] as FloatingRootContext;
|
|
280
|
+
const options = (user[1] as UseNextDelayGroupOptions) ?? {};
|
|
281
|
+
const enabled = options.enabled ?? true;
|
|
282
|
+
const { open, onOpenChange, floatingId } = context;
|
|
283
|
+
const groupContext = useContext(NextFloatingDelayGroupContext);
|
|
284
|
+
const {
|
|
285
|
+
currentIdRef,
|
|
286
|
+
delayRef,
|
|
287
|
+
timeoutMs,
|
|
288
|
+
initialDelayRef,
|
|
289
|
+
currentContextRef,
|
|
290
|
+
hasProvider,
|
|
291
|
+
timeoutIdRef,
|
|
292
|
+
} = groupContext;
|
|
293
|
+
const [isInstantPhase, setIsInstantPhase] = useState(false, subSlot(slot, 'state'));
|
|
294
|
+
|
|
295
|
+
useModernLayoutEffect(
|
|
296
|
+
() => {
|
|
297
|
+
function unset() {
|
|
298
|
+
setIsInstantPhase(false);
|
|
299
|
+
currentContextRef.current?.setIsInstantPhase(false);
|
|
300
|
+
currentIdRef.current = null;
|
|
301
|
+
currentContextRef.current = null;
|
|
302
|
+
delayRef.current = initialDelayRef.current;
|
|
303
|
+
}
|
|
304
|
+
if (!enabled || !currentIdRef.current) return;
|
|
305
|
+
if (!open && currentIdRef.current === floatingId) {
|
|
306
|
+
setIsInstantPhase(false);
|
|
307
|
+
if (timeoutMs) {
|
|
308
|
+
timeoutIdRef.current = window.setTimeout(unset, timeoutMs);
|
|
309
|
+
return () => clearTimeout(timeoutIdRef.current);
|
|
310
|
+
}
|
|
311
|
+
unset();
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
[enabled, open, floatingId, timeoutMs],
|
|
315
|
+
subSlot(slot, 'e:close'),
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
useModernLayoutEffect(
|
|
319
|
+
() => {
|
|
320
|
+
if (!enabled || !open) return;
|
|
321
|
+
const prevContext = currentContextRef.current;
|
|
322
|
+
const prevId = currentIdRef.current;
|
|
323
|
+
currentContextRef.current = { onOpenChange, setIsInstantPhase };
|
|
324
|
+
currentIdRef.current = floatingId;
|
|
325
|
+
delayRef.current = { open: 0, close: getDelay(initialDelayRef.current, 'close') };
|
|
326
|
+
if (prevId !== null && prevId !== floatingId) {
|
|
327
|
+
clearTimeoutIfSet(timeoutIdRef);
|
|
328
|
+
setIsInstantPhase(true);
|
|
329
|
+
prevContext?.setIsInstantPhase(true);
|
|
330
|
+
prevContext?.onOpenChange(false);
|
|
331
|
+
} else {
|
|
332
|
+
setIsInstantPhase(false);
|
|
333
|
+
prevContext?.setIsInstantPhase(false);
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
[enabled, open, floatingId, onOpenChange],
|
|
337
|
+
subSlot(slot, 'e:open'),
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
useModernLayoutEffect(
|
|
341
|
+
() => () => {
|
|
342
|
+
currentContextRef.current = null;
|
|
343
|
+
},
|
|
344
|
+
[currentContextRef],
|
|
345
|
+
subSlot(slot, 'e:cleanup'),
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
return useMemo(
|
|
349
|
+
() => ({ hasProvider, delayRef, isInstantPhase }),
|
|
350
|
+
[hasProvider, delayRef, isInstantPhase],
|
|
351
|
+
subSlot(slot, 'result'),
|
|
352
|
+
);
|
|
353
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -165,7 +165,10 @@ export {
|
|
|
165
165
|
useDelayGroup,
|
|
166
166
|
useDelayGroupContext,
|
|
167
167
|
FloatingDelayGroupContext,
|
|
168
|
+
NextFloatingDelayGroup,
|
|
169
|
+
useNextDelayGroup,
|
|
168
170
|
} from './delayGroup';
|
|
171
|
+
export { inner, useInnerOffset } from './inner';
|
|
169
172
|
|
|
170
173
|
// Component / transition prop types (mirror upstream's exported names; the
|
|
171
174
|
// octane adaptations — OctaneNode children, ref-as-prop, native events — are
|
|
@@ -185,3 +188,9 @@ export type {
|
|
|
185
188
|
UseTransitionStylesProps,
|
|
186
189
|
} from './transitions';
|
|
187
190
|
export type { FloatingDelayGroupProps } from './delayGroup';
|
|
191
|
+
export type {
|
|
192
|
+
NextFloatingDelayGroupProps,
|
|
193
|
+
UseNextDelayGroupOptions,
|
|
194
|
+
UseNextDelayGroupReturn,
|
|
195
|
+
} from './delayGroup';
|
|
196
|
+
export type { InnerProps, UseInnerOffsetProps } from './inner';
|
package/src/inner.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// Ported from @floating-ui/react 0.27.19's deprecated inner middleware and hook.
|
|
2
|
+
import {
|
|
3
|
+
offset,
|
|
4
|
+
type Derivable,
|
|
5
|
+
type DetectOverflowOptions,
|
|
6
|
+
type Middleware,
|
|
7
|
+
type MiddlewareState,
|
|
8
|
+
type SideObject,
|
|
9
|
+
} from '@floating-ui/dom';
|
|
10
|
+
import { evaluate, max, min, round } from '@floating-ui/utils';
|
|
11
|
+
import { flushSync, useMemo, useRef } from 'octane';
|
|
12
|
+
|
|
13
|
+
import { splitSlot, subSlot } from './internal';
|
|
14
|
+
import type { ElementProps, FloatingRootContext, MutableRefObject } from './types';
|
|
15
|
+
import { getUserAgent, useEffectEvent, useModernLayoutEffect } from './utils';
|
|
16
|
+
|
|
17
|
+
function getArgsWithCustomFloatingHeight(state: MiddlewareState, height: number) {
|
|
18
|
+
return {
|
|
19
|
+
...state,
|
|
20
|
+
rects: {
|
|
21
|
+
...state.rects,
|
|
22
|
+
floating: { ...state.rects.floating, height },
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface InnerProps extends DetectOverflowOptions {
|
|
28
|
+
listRef: MutableRefObject<Array<HTMLElement | null>>;
|
|
29
|
+
index: number;
|
|
30
|
+
onFallbackChange?: null | ((fallback: boolean) => void);
|
|
31
|
+
offset?: number;
|
|
32
|
+
overflowRef?: MutableRefObject<SideObject | null>;
|
|
33
|
+
scrollRef?: MutableRefObject<HTMLElement | null>;
|
|
34
|
+
minItemsVisible?: number;
|
|
35
|
+
referenceOverflowThreshold?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** @deprecated Positions an inner list item against the reference element. */
|
|
39
|
+
export const inner = (props: InnerProps | Derivable<InnerProps>): Middleware => ({
|
|
40
|
+
name: 'inner',
|
|
41
|
+
options: props,
|
|
42
|
+
async fn(state) {
|
|
43
|
+
const {
|
|
44
|
+
listRef,
|
|
45
|
+
overflowRef,
|
|
46
|
+
onFallbackChange,
|
|
47
|
+
offset: innerOffset = 0,
|
|
48
|
+
index = 0,
|
|
49
|
+
minItemsVisible = 4,
|
|
50
|
+
referenceOverflowThreshold = 0,
|
|
51
|
+
scrollRef,
|
|
52
|
+
...detectOverflowOptions
|
|
53
|
+
} = evaluate(props, state);
|
|
54
|
+
const {
|
|
55
|
+
rects,
|
|
56
|
+
platform,
|
|
57
|
+
elements: { floating },
|
|
58
|
+
} = state;
|
|
59
|
+
const item = listRef.current[index];
|
|
60
|
+
const scrollEl = scrollRef?.current || floating;
|
|
61
|
+
const clientTop = floating.clientTop || scrollEl.clientTop;
|
|
62
|
+
const floatingIsBordered = floating.clientTop !== 0;
|
|
63
|
+
const scrollElIsBordered = scrollEl.clientTop !== 0;
|
|
64
|
+
const floatingIsScrollEl = floating === scrollEl;
|
|
65
|
+
if (!item) return {};
|
|
66
|
+
const nextArgs = {
|
|
67
|
+
...state,
|
|
68
|
+
...(await offset(
|
|
69
|
+
-item.offsetTop -
|
|
70
|
+
floating.clientTop -
|
|
71
|
+
rects.reference.height / 2 -
|
|
72
|
+
item.offsetHeight / 2 -
|
|
73
|
+
innerOffset,
|
|
74
|
+
).fn(state)),
|
|
75
|
+
};
|
|
76
|
+
const overflow = await platform.detectOverflow(
|
|
77
|
+
getArgsWithCustomFloatingHeight(
|
|
78
|
+
nextArgs,
|
|
79
|
+
scrollEl.scrollHeight + clientTop + floating.clientTop,
|
|
80
|
+
),
|
|
81
|
+
detectOverflowOptions,
|
|
82
|
+
);
|
|
83
|
+
const refOverflow = await platform.detectOverflow(nextArgs, {
|
|
84
|
+
...detectOverflowOptions,
|
|
85
|
+
elementContext: 'reference',
|
|
86
|
+
});
|
|
87
|
+
const diffY = max(0, overflow.top);
|
|
88
|
+
const nextY = nextArgs.y + diffY;
|
|
89
|
+
const isScrollable = scrollEl.scrollHeight > scrollEl.clientHeight;
|
|
90
|
+
const rounder = isScrollable ? (value: number) => value : round;
|
|
91
|
+
const maxHeight = rounder(
|
|
92
|
+
max(
|
|
93
|
+
0,
|
|
94
|
+
scrollEl.scrollHeight +
|
|
95
|
+
((floatingIsBordered && floatingIsScrollEl) || scrollElIsBordered ? clientTop * 2 : 0) -
|
|
96
|
+
diffY -
|
|
97
|
+
max(0, overflow.bottom),
|
|
98
|
+
),
|
|
99
|
+
);
|
|
100
|
+
scrollEl.style.maxHeight = `${maxHeight}px`;
|
|
101
|
+
scrollEl.scrollTop = diffY;
|
|
102
|
+
if (onFallbackChange) {
|
|
103
|
+
const shouldFallback =
|
|
104
|
+
scrollEl.offsetHeight <
|
|
105
|
+
item.offsetHeight * min(minItemsVisible, listRef.current.length) - 1 ||
|
|
106
|
+
refOverflow.top >= -referenceOverflowThreshold ||
|
|
107
|
+
refOverflow.bottom >= -referenceOverflowThreshold;
|
|
108
|
+
flushSync(() => onFallbackChange(shouldFallback));
|
|
109
|
+
}
|
|
110
|
+
if (overflowRef) {
|
|
111
|
+
overflowRef.current = await platform.detectOverflow(
|
|
112
|
+
getArgsWithCustomFloatingHeight(
|
|
113
|
+
{ ...nextArgs, y: nextY },
|
|
114
|
+
scrollEl.offsetHeight + clientTop + floating.clientTop,
|
|
115
|
+
),
|
|
116
|
+
detectOverflowOptions,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
return { y: nextY };
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
export interface UseInnerOffsetProps {
|
|
124
|
+
enabled?: boolean;
|
|
125
|
+
overflowRef: MutableRefObject<SideObject | null>;
|
|
126
|
+
scrollRef?: MutableRefObject<HTMLElement | null>;
|
|
127
|
+
onChange: (offset: number | ((offset: number) => number)) => void;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** @deprecated Expands an inner-positioned list in response to wheel and scroll events. */
|
|
131
|
+
export function useInnerOffset(
|
|
132
|
+
context: FloatingRootContext,
|
|
133
|
+
props: UseInnerOffsetProps,
|
|
134
|
+
slot?: symbol,
|
|
135
|
+
): ElementProps;
|
|
136
|
+
export function useInnerOffset(...args: any[]): ElementProps {
|
|
137
|
+
const [user, slot] = splitSlot(args);
|
|
138
|
+
const context = user[0] as FloatingRootContext;
|
|
139
|
+
const props = user[1] as UseInnerOffsetProps;
|
|
140
|
+
const { open, elements } = context;
|
|
141
|
+
const { enabled = true, overflowRef, scrollRef, onChange: unstableOnChange } = props;
|
|
142
|
+
const onChange = useEffectEvent(unstableOnChange, subSlot(slot, 'change'));
|
|
143
|
+
const controlledScrollingRef = useRef(false, subSlot(slot, 'controlled'));
|
|
144
|
+
const prevScrollTopRef = useRef<number | null>(null, subSlot(slot, 'prevScrollTop'));
|
|
145
|
+
const initialOverflowRef = useRef<SideObject | null>(null, subSlot(slot, 'initialOverflow'));
|
|
146
|
+
|
|
147
|
+
useModernLayoutEffect(
|
|
148
|
+
() => {
|
|
149
|
+
if (!enabled) return;
|
|
150
|
+
const el = scrollRef?.current || elements.floating;
|
|
151
|
+
if (!open || !el) return;
|
|
152
|
+
const scrollElement = el;
|
|
153
|
+
function onWheel(event: WheelEvent) {
|
|
154
|
+
if (event.ctrlKey || overflowRef.current == null) return;
|
|
155
|
+
const deltaY = event.deltaY;
|
|
156
|
+
const isAtTop = overflowRef.current.top >= -0.5;
|
|
157
|
+
const isAtBottom = overflowRef.current.bottom >= -0.5;
|
|
158
|
+
const remainingScroll = scrollElement.scrollHeight - scrollElement.clientHeight;
|
|
159
|
+
const sign = deltaY < 0 ? -1 : 1;
|
|
160
|
+
const method = deltaY < 0 ? 'max' : 'min';
|
|
161
|
+
if (scrollElement.scrollHeight <= scrollElement.clientHeight) return;
|
|
162
|
+
if ((!isAtTop && deltaY > 0) || (!isAtBottom && deltaY < 0)) {
|
|
163
|
+
event.preventDefault();
|
|
164
|
+
flushSync(() =>
|
|
165
|
+
onChange((value) => value + Math[method](deltaY, remainingScroll * sign)),
|
|
166
|
+
);
|
|
167
|
+
} else if (/firefox/i.test(getUserAgent())) {
|
|
168
|
+
scrollElement.scrollTop += deltaY;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
scrollElement.addEventListener('wheel', onWheel);
|
|
172
|
+
requestAnimationFrame(() => {
|
|
173
|
+
prevScrollTopRef.current = scrollElement.scrollTop;
|
|
174
|
+
if (overflowRef.current != null) initialOverflowRef.current = { ...overflowRef.current };
|
|
175
|
+
});
|
|
176
|
+
return () => {
|
|
177
|
+
prevScrollTopRef.current = null;
|
|
178
|
+
initialOverflowRef.current = null;
|
|
179
|
+
scrollElement.removeEventListener('wheel', onWheel);
|
|
180
|
+
};
|
|
181
|
+
},
|
|
182
|
+
[enabled, open, elements.floating, overflowRef, scrollRef, onChange],
|
|
183
|
+
subSlot(slot, 'e:wheel'),
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
const floating: NonNullable<ElementProps['floating']> = useMemo(
|
|
187
|
+
() => ({
|
|
188
|
+
onKeyDown() {
|
|
189
|
+
controlledScrollingRef.current = true;
|
|
190
|
+
},
|
|
191
|
+
onWheel() {
|
|
192
|
+
controlledScrollingRef.current = false;
|
|
193
|
+
},
|
|
194
|
+
onPointerMove() {
|
|
195
|
+
controlledScrollingRef.current = false;
|
|
196
|
+
},
|
|
197
|
+
onScroll() {
|
|
198
|
+
const el = scrollRef?.current || elements.floating;
|
|
199
|
+
if (!overflowRef.current || !el || !controlledScrollingRef.current) return;
|
|
200
|
+
if (prevScrollTopRef.current !== null) {
|
|
201
|
+
const scrollDiff = el.scrollTop - prevScrollTopRef.current;
|
|
202
|
+
if (
|
|
203
|
+
(overflowRef.current.bottom < -0.5 && scrollDiff < -1) ||
|
|
204
|
+
(overflowRef.current.top < -0.5 && scrollDiff > 1)
|
|
205
|
+
) {
|
|
206
|
+
flushSync(() => onChange((value) => value + scrollDiff));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
requestAnimationFrame(() => {
|
|
210
|
+
prevScrollTopRef.current = el.scrollTop;
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
}),
|
|
214
|
+
[elements.floating, onChange, overflowRef, scrollRef],
|
|
215
|
+
subSlot(slot, 'floating'),
|
|
216
|
+
);
|
|
217
|
+
return useMemo(() => (enabled ? { floating } : {}), [enabled, floating], subSlot(slot, 'result'));
|
|
218
|
+
}
|
package/src/useDismiss.ts
CHANGED
|
@@ -101,7 +101,7 @@ const captureHandlerKeys: Record<PressHandlerKey, string> = {
|
|
|
101
101
|
mousedown: 'onMouseDownCapture',
|
|
102
102
|
click: 'onClickCapture',
|
|
103
103
|
};
|
|
104
|
-
const normalizeProp = (
|
|
104
|
+
export const normalizeProp = (
|
|
105
105
|
normalizable?: boolean | { escapeKey?: boolean; outsidePress?: boolean },
|
|
106
106
|
) => ({
|
|
107
107
|
escapeKey: typeof normalizable === 'boolean' ? normalizable : (normalizable?.escapeKey ?? false),
|
package/src/useFloating.ts
CHANGED
|
@@ -78,19 +78,42 @@ export function usePositionFloating(args: any[]): any {
|
|
|
78
78
|
subSlot(slot, 'data'),
|
|
79
79
|
);
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
81
|
+
// Octane state updates requested while rendering are scheduled after the
|
|
82
|
+
// current pass. Keep the deep-equal upstream stabilization synchronously in
|
|
83
|
+
// a ref so `update` always observes the middleware from this render without
|
|
84
|
+
// making fresh-but-equivalent arrays invalidate the positioning effect.
|
|
85
|
+
const latestMiddlewareRef = useRef({ value: middleware, revision: 0 }, subSlot(slot, 'mw'));
|
|
86
|
+
if (!deepEqual(latestMiddlewareRef.current.value, middleware)) {
|
|
87
|
+
latestMiddlewareRef.current.value = middleware;
|
|
88
|
+
latestMiddlewareRef.current.revision += 1;
|
|
84
89
|
}
|
|
90
|
+
const middlewareRevision = latestMiddlewareRef.current.revision;
|
|
85
91
|
|
|
86
92
|
const [_reference, _setReference] = useState<ReferenceType | null>(null, subSlot(slot, 'ref'));
|
|
87
93
|
const [_floating, _setFloating] = useState<HTMLElement | null>(null, subSlot(slot, 'flo'));
|
|
88
94
|
|
|
89
95
|
const referenceRef = useRef<ReferenceType | null>(null, subSlot(slot, 'rref'));
|
|
90
96
|
const floatingRef = useRef<HTMLElement | null>(null, subSlot(slot, 'rflo'));
|
|
97
|
+
const referenceDetachRef = useRef(0, subSlot(slot, 'rdetach'));
|
|
98
|
+
const floatingDetachRef = useRef(0, subSlot(slot, 'fdetach'));
|
|
91
99
|
|
|
92
100
|
const setReference = useCallback(
|
|
93
101
|
(node: ReferenceType | null) => {
|
|
102
|
+
const detach = ++referenceDetachRef.current;
|
|
103
|
+
if (node === null) {
|
|
104
|
+
queueMicrotask(() => {
|
|
105
|
+
const current = referenceRef.current;
|
|
106
|
+
if (
|
|
107
|
+
referenceDetachRef.current === detach &&
|
|
108
|
+
current !== null &&
|
|
109
|
+
(!(current instanceof Element) || !current.isConnected)
|
|
110
|
+
) {
|
|
111
|
+
referenceRef.current = null;
|
|
112
|
+
_setReference(null);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
94
117
|
if (node !== referenceRef.current) {
|
|
95
118
|
referenceRef.current = node;
|
|
96
119
|
_setReference(node);
|
|
@@ -101,6 +124,17 @@ export function usePositionFloating(args: any[]): any {
|
|
|
101
124
|
);
|
|
102
125
|
const setFloating = useCallback(
|
|
103
126
|
(node: HTMLElement | null) => {
|
|
127
|
+
const detach = ++floatingDetachRef.current;
|
|
128
|
+
if (node === null) {
|
|
129
|
+
queueMicrotask(() => {
|
|
130
|
+
const current = floatingRef.current;
|
|
131
|
+
if (floatingDetachRef.current === detach && current !== null && !current.isConnected) {
|
|
132
|
+
floatingRef.current = null;
|
|
133
|
+
_setFloating(null);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
104
138
|
if (node !== floatingRef.current) {
|
|
105
139
|
floatingRef.current = node;
|
|
106
140
|
_setFloating(node);
|
|
@@ -128,7 +162,7 @@ export function usePositionFloating(args: any[]): any {
|
|
|
128
162
|
const config: Partial<ComputePositionConfig> = {
|
|
129
163
|
placement,
|
|
130
164
|
strategy,
|
|
131
|
-
middleware:
|
|
165
|
+
middleware: latestMiddlewareRef.current.value,
|
|
132
166
|
};
|
|
133
167
|
if (platformRef.current) {
|
|
134
168
|
config.platform = platformRef.current;
|
|
@@ -146,7 +180,7 @@ export function usePositionFloating(args: any[]): any {
|
|
|
146
180
|
}
|
|
147
181
|
});
|
|
148
182
|
},
|
|
149
|
-
[
|
|
183
|
+
[middlewareRevision, placement, strategy, platformRef, openRef],
|
|
150
184
|
subSlot(slot, 'upd'),
|
|
151
185
|
);
|
|
152
186
|
|
package/src/utils/index.ts
CHANGED
|
@@ -12,7 +12,9 @@ import type { Octane } from 'octane/jsx-runtime';
|
|
|
12
12
|
import type { Dimensions } from '@floating-ui/dom';
|
|
13
13
|
|
|
14
14
|
import { subSlot } from '../internal';
|
|
15
|
-
import type { Delay,
|
|
15
|
+
import type { Delay, MutableRefObject } from '../types';
|
|
16
|
+
|
|
17
|
+
export { getDeepestNode, getNodeAncestors, getNodeChildren } from './nodes';
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* The OBJECT form of octane's `style` prop — the port's analog of
|
|
@@ -155,54 +157,6 @@ export function getFloatingFocusElement(
|
|
|
155
157
|
floatingElement;
|
|
156
158
|
}
|
|
157
159
|
|
|
158
|
-
export function getNodeChildren<RT extends ReferenceType = ReferenceType>(
|
|
159
|
-
nodes: Array<FloatingNodeType<RT>>,
|
|
160
|
-
id: string | undefined,
|
|
161
|
-
onlyOpenChildren = true,
|
|
162
|
-
): Array<FloatingNodeType<RT>> {
|
|
163
|
-
const directChildren = nodes.filter(
|
|
164
|
-
(node) => node.parentId === id && (!onlyOpenChildren || node.context?.open),
|
|
165
|
-
);
|
|
166
|
-
return directChildren.flatMap((child) => [
|
|
167
|
-
child,
|
|
168
|
-
...getNodeChildren(nodes, child.id, onlyOpenChildren),
|
|
169
|
-
]);
|
|
170
|
-
}
|
|
171
|
-
export function getDeepestNode<RT extends ReferenceType = ReferenceType>(
|
|
172
|
-
nodes: Array<FloatingNodeType<RT>>,
|
|
173
|
-
id: string | undefined,
|
|
174
|
-
): FloatingNodeType<RT> | undefined {
|
|
175
|
-
let deepestNodeId: string | undefined;
|
|
176
|
-
let maxDepth = -1;
|
|
177
|
-
function findDeepest(nodeId: string | undefined, depth: number) {
|
|
178
|
-
if (depth > maxDepth) {
|
|
179
|
-
deepestNodeId = nodeId;
|
|
180
|
-
maxDepth = depth;
|
|
181
|
-
}
|
|
182
|
-
const children = getNodeChildren(nodes, nodeId);
|
|
183
|
-
children.forEach((child) => {
|
|
184
|
-
findDeepest(child.id, depth + 1);
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
findDeepest(id, 0);
|
|
188
|
-
return nodes.find((node) => node.id === deepestNodeId);
|
|
189
|
-
}
|
|
190
|
-
export function getNodeAncestors<RT extends ReferenceType = ReferenceType>(
|
|
191
|
-
nodes: Array<FloatingNodeType<RT>>,
|
|
192
|
-
id: string | undefined,
|
|
193
|
-
): Array<FloatingNodeType<RT>> {
|
|
194
|
-
let allAncestors: Array<FloatingNodeType<RT>> = [];
|
|
195
|
-
let currentParentId = nodes.find((node) => node.id === id)?.parentId;
|
|
196
|
-
while (currentParentId) {
|
|
197
|
-
const currentNode = nodes.find((node) => node.id === currentParentId);
|
|
198
|
-
currentParentId = currentNode?.parentId;
|
|
199
|
-
if (currentNode) {
|
|
200
|
-
allAncestors = allAncestors.concat(currentNode);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
return allAncestors;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
160
|
export function stopEvent(event: Event): void {
|
|
207
161
|
event.preventDefault();
|
|
208
162
|
event.stopPropagation();
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Ported from @floating-ui/react/utils/nodes. Kept separate from the DOM and
|
|
2
|
+
// hook helpers so tree algorithms can be exercised without a browser runtime.
|
|
3
|
+
import type { FloatingNodeType, ReferenceType } from '../types';
|
|
4
|
+
|
|
5
|
+
export function getNodeChildren<RT extends ReferenceType = ReferenceType>(
|
|
6
|
+
nodes: Array<FloatingNodeType<RT>>,
|
|
7
|
+
id: string | undefined,
|
|
8
|
+
onlyOpenChildren = true,
|
|
9
|
+
): Array<FloatingNodeType<RT>> {
|
|
10
|
+
const directChildren = nodes.filter(
|
|
11
|
+
(node) => node.parentId === id && (!onlyOpenChildren || node.context?.open),
|
|
12
|
+
);
|
|
13
|
+
return directChildren.flatMap((child) => [
|
|
14
|
+
child,
|
|
15
|
+
...getNodeChildren(nodes, child.id, onlyOpenChildren),
|
|
16
|
+
]);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getDeepestNode<RT extends ReferenceType = ReferenceType>(
|
|
20
|
+
nodes: Array<FloatingNodeType<RT>>,
|
|
21
|
+
id: string | undefined,
|
|
22
|
+
): FloatingNodeType<RT> | undefined {
|
|
23
|
+
const childrenByParent = new Map<string | null | undefined, Array<FloatingNodeType<RT>>>();
|
|
24
|
+
for (const node of nodes) {
|
|
25
|
+
if (!node.context?.open) continue;
|
|
26
|
+
const siblings = childrenByParent.get(node.parentId);
|
|
27
|
+
if (siblings) {
|
|
28
|
+
siblings.push(node);
|
|
29
|
+
} else {
|
|
30
|
+
childrenByParent.set(node.parentId, [node]);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let deepestNodeId = id;
|
|
35
|
+
let maxDepth = 0;
|
|
36
|
+
const stack: Array<[FloatingNodeType<RT>, number]> = [];
|
|
37
|
+
const rootChildren = childrenByParent.get(id);
|
|
38
|
+
if (rootChildren) {
|
|
39
|
+
for (let index = rootChildren.length - 1; index >= 0; index--) {
|
|
40
|
+
stack.push([rootChildren[index], 1]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
while (stack.length) {
|
|
45
|
+
const [node, depth] = stack.pop()!;
|
|
46
|
+
if (depth > maxDepth) {
|
|
47
|
+
deepestNodeId = node.id;
|
|
48
|
+
maxDepth = depth;
|
|
49
|
+
}
|
|
50
|
+
const children = childrenByParent.get(node.id);
|
|
51
|
+
if (children) {
|
|
52
|
+
for (let index = children.length - 1; index >= 0; index--) {
|
|
53
|
+
stack.push([children[index], depth + 1]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return nodes.find((node) => node.id === deepestNodeId);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function getNodeAncestors<RT extends ReferenceType = ReferenceType>(
|
|
62
|
+
nodes: Array<FloatingNodeType<RT>>,
|
|
63
|
+
id: string | undefined,
|
|
64
|
+
): Array<FloatingNodeType<RT>> {
|
|
65
|
+
let allAncestors: Array<FloatingNodeType<RT>> = [];
|
|
66
|
+
let currentParentId = nodes.find((node) => node.id === id)?.parentId;
|
|
67
|
+
while (currentParentId) {
|
|
68
|
+
const currentNode = nodes.find((node) => node.id === currentParentId);
|
|
69
|
+
currentParentId = currentNode?.parentId;
|
|
70
|
+
if (currentNode) {
|
|
71
|
+
allAncestors = allAncestors.concat(currentNode);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return allAncestors;
|
|
75
|
+
}
|