@octanejs/floating-ui 0.1.45 → 0.1.47
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/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/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
|
|