@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/dist/cjs/useFloating.cjs
CHANGED
|
@@ -60,16 +60,31 @@ function usePositionFloating(args) {
|
|
|
60
60
|
},
|
|
61
61
|
(0, import_internal.subSlot)(slot, "data")
|
|
62
62
|
);
|
|
63
|
-
const
|
|
64
|
-
if (!(0, import_utils.deepEqual)(
|
|
65
|
-
|
|
63
|
+
const latestMiddlewareRef = (0, import_octane.useRef)({ value: middleware, revision: 0 }, (0, import_internal.subSlot)(slot, "mw"));
|
|
64
|
+
if (!(0, import_utils.deepEqual)(latestMiddlewareRef.current.value, middleware)) {
|
|
65
|
+
latestMiddlewareRef.current.value = middleware;
|
|
66
|
+
latestMiddlewareRef.current.revision += 1;
|
|
66
67
|
}
|
|
68
|
+
const middlewareRevision = latestMiddlewareRef.current.revision;
|
|
67
69
|
const [_reference, _setReference] = (0, import_octane.useState)(null, (0, import_internal.subSlot)(slot, "ref"));
|
|
68
70
|
const [_floating, _setFloating] = (0, import_octane.useState)(null, (0, import_internal.subSlot)(slot, "flo"));
|
|
69
71
|
const referenceRef = (0, import_octane.useRef)(null, (0, import_internal.subSlot)(slot, "rref"));
|
|
70
72
|
const floatingRef = (0, import_octane.useRef)(null, (0, import_internal.subSlot)(slot, "rflo"));
|
|
73
|
+
const referenceDetachRef = (0, import_octane.useRef)(0, (0, import_internal.subSlot)(slot, "rdetach"));
|
|
74
|
+
const floatingDetachRef = (0, import_octane.useRef)(0, (0, import_internal.subSlot)(slot, "fdetach"));
|
|
71
75
|
const setReference = (0, import_octane.useCallback)(
|
|
72
76
|
(node) => {
|
|
77
|
+
const detach = ++referenceDetachRef.current;
|
|
78
|
+
if (node === null) {
|
|
79
|
+
queueMicrotask(() => {
|
|
80
|
+
const current = referenceRef.current;
|
|
81
|
+
if (referenceDetachRef.current === detach && current !== null && (!(current instanceof Element) || !current.isConnected)) {
|
|
82
|
+
referenceRef.current = null;
|
|
83
|
+
_setReference(null);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
73
88
|
if (node !== referenceRef.current) {
|
|
74
89
|
referenceRef.current = node;
|
|
75
90
|
_setReference(node);
|
|
@@ -80,6 +95,17 @@ function usePositionFloating(args) {
|
|
|
80
95
|
);
|
|
81
96
|
const setFloating = (0, import_octane.useCallback)(
|
|
82
97
|
(node) => {
|
|
98
|
+
const detach = ++floatingDetachRef.current;
|
|
99
|
+
if (node === null) {
|
|
100
|
+
queueMicrotask(() => {
|
|
101
|
+
const current = floatingRef.current;
|
|
102
|
+
if (floatingDetachRef.current === detach && current !== null && !current.isConnected) {
|
|
103
|
+
floatingRef.current = null;
|
|
104
|
+
_setFloating(null);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
83
109
|
if (node !== floatingRef.current) {
|
|
84
110
|
floatingRef.current = node;
|
|
85
111
|
_setFloating(node);
|
|
@@ -104,7 +130,7 @@ function usePositionFloating(args) {
|
|
|
104
130
|
const config = {
|
|
105
131
|
placement,
|
|
106
132
|
strategy,
|
|
107
|
-
middleware:
|
|
133
|
+
middleware: latestMiddlewareRef.current.value
|
|
108
134
|
};
|
|
109
135
|
if (platformRef.current) {
|
|
110
136
|
config.platform = platformRef.current;
|
|
@@ -122,7 +148,7 @@ function usePositionFloating(args) {
|
|
|
122
148
|
}
|
|
123
149
|
});
|
|
124
150
|
},
|
|
125
|
-
[
|
|
151
|
+
[middlewareRevision, placement, strategy, platformRef, openRef],
|
|
126
152
|
(0, import_internal.subSlot)(slot, "upd")
|
|
127
153
|
);
|
|
128
154
|
(0, import_octane.useLayoutEffect)(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octanejs/floating-ui",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.47",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -33,7 +33,11 @@
|
|
|
33
33
|
"files": [
|
|
34
34
|
"src",
|
|
35
35
|
"dist/cjs",
|
|
36
|
-
"README.md"
|
|
36
|
+
"README.md",
|
|
37
|
+
"UPSTREAM.md",
|
|
38
|
+
"LICENSE",
|
|
39
|
+
"LICENSE.upstream",
|
|
40
|
+
"LICENSE.react.upstream"
|
|
37
41
|
],
|
|
38
42
|
"exports": {
|
|
39
43
|
".": {
|
|
@@ -49,18 +53,45 @@
|
|
|
49
53
|
"tabbable": "^6.5.0"
|
|
50
54
|
},
|
|
51
55
|
"peerDependencies": {
|
|
52
|
-
"octane": "0.1.
|
|
56
|
+
"octane": "0.1.48"
|
|
53
57
|
},
|
|
54
58
|
"devDependencies": {
|
|
59
|
+
"@radix-ui/react-checkbox": "1.0.4",
|
|
60
|
+
"@radix-ui/react-icons": "1.3.0",
|
|
61
|
+
"@testing-library/jest-dom": "6.6.3",
|
|
62
|
+
"@testing-library/react": "16.2.0",
|
|
63
|
+
"@testing-library/user-event": "14.6.1",
|
|
64
|
+
"@types/react": "18.3.19",
|
|
65
|
+
"@types/react-dom": "18.3.1",
|
|
66
|
+
"@tsrx/react": "^0.2.61",
|
|
67
|
+
"@vitest/browser": "4.1.10",
|
|
55
68
|
"@floating-ui/react": "0.27.19",
|
|
69
|
+
"@floating-ui/react-dom": "2.1.8",
|
|
56
70
|
"esbuild": "^0.28.1",
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
71
|
+
"clsx": "1.2.1",
|
|
72
|
+
"playwright": "^1.61.1",
|
|
73
|
+
"react": "18.2.0",
|
|
74
|
+
"react-dom": "18.2.0",
|
|
75
|
+
"react-responsive": "9.0.2",
|
|
76
|
+
"resize-observer-polyfill": "1.5.1",
|
|
77
|
+
"tabbable-pristine": "npm:tabbable@6.2.0",
|
|
78
|
+
"typescript": "5.4.2",
|
|
79
|
+
"use-isomorphic-layout-effect": "1.2.1",
|
|
80
|
+
"vite": "^8.1.5",
|
|
81
|
+
"vitest": "3.0.9",
|
|
82
|
+
"vitest-browser-react": "0.1.1",
|
|
83
|
+
"@octanejs/testing-library": "0.1.45",
|
|
84
|
+
"octane": "0.1.48"
|
|
61
85
|
},
|
|
62
86
|
"scripts": {
|
|
63
87
|
"build:cjs": "node ../../scripts/build-package-commonjs.mjs",
|
|
64
|
-
"test": "vitest run"
|
|
88
|
+
"test": "pnpm upstream:verify && node ../../scripts/react-parity/harness.mjs run-required --manifest packages/floating-ui/audit/react-parity.json && ../../node_modules/.bin/vitest run --root ../.. --project floating-ui",
|
|
89
|
+
"test:upstream": "node ../../scripts/react-parity/run-pristine.mjs floating-ui",
|
|
90
|
+
"test:adapted-upstream": "vitest run --config tests/adapted-vitest.config.ts",
|
|
91
|
+
"parity:evidence:generate": "node scripts/generate-parity-evidence.mjs",
|
|
92
|
+
"parity:evidence:check": "node scripts/generate-parity-evidence.mjs --check",
|
|
93
|
+
"typecheck": "tsrx-tsc --noEmit -p tsconfig.json && tsrx-tsc --noEmit -p typetests/tsconfig.adapted.json",
|
|
94
|
+
"typecheck:pristine": "tsc --noEmit -p typetests/tsconfig.pristine.json",
|
|
95
|
+
"upstream:verify": "node ../../scripts/react-parity/verify-provenance.mjs"
|
|
65
96
|
}
|
|
66
97
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// are returned as an ARRAY (octane renders it like a React fragment).
|
|
7
7
|
import { getNodeName, isHTMLElement, isShadowRoot } from '@floating-ui/utils/dom';
|
|
8
8
|
import { focusable, isTabbable, tabbable } from 'tabbable';
|
|
9
|
-
import { createElement, useEffect, useMemo, useRef } from 'octane';
|
|
9
|
+
import { createElement, Fragment, useEffect, useMemo, useRef } from 'octane';
|
|
10
10
|
import type { OctaneNode } from 'octane';
|
|
11
11
|
|
|
12
12
|
import { FocusGuard, usePortalContext } from './FloatingPortal';
|
|
@@ -171,7 +171,7 @@ function applyAttributeToOthers(
|
|
|
171
171
|
}
|
|
172
172
|
};
|
|
173
173
|
}
|
|
174
|
-
function markOthers(avoidElements: any[], ariaHidden = false, inert = false): () => void {
|
|
174
|
+
export function markOthers(avoidElements: any[], ariaHidden = false, inert = false): () => void {
|
|
175
175
|
const body = getDocument(avoidElements[0]).body;
|
|
176
176
|
return applyAttributeToOthers(
|
|
177
177
|
avoidElements.concat(Array.from(body.querySelectorAll('[aria-live],[role="status"],output'))),
|
|
@@ -849,6 +849,7 @@ export function FloatingFocusManager(props: FloatingFocusManagerProps): OctaneNo
|
|
|
849
849
|
return null;
|
|
850
850
|
}
|
|
851
851
|
return createElement(VisuallyHiddenDismiss, {
|
|
852
|
+
key: `dismiss-${location}`,
|
|
852
853
|
ref: location === 'start' ? startDismissButtonRef : endDismissButtonRef,
|
|
853
854
|
onClick: (event: MouseEvent) => onOpenChange(false, event),
|
|
854
855
|
children: typeof visuallyHiddenDismiss === 'string' ? visuallyHiddenDismiss : 'Dismiss',
|
|
@@ -861,54 +862,60 @@ export function FloatingFocusManager(props: FloatingFocusManagerProps): OctaneNo
|
|
|
861
862
|
(modal ? !isUntrappedTypeableCombobox : true) &&
|
|
862
863
|
(isInsidePortal || modal);
|
|
863
864
|
|
|
865
|
+
// Octane reconciles array children as keyed lists. Keep every conditional slot
|
|
866
|
+
// stable so changing guard/dismiss visibility cannot remount consumer content.
|
|
864
867
|
return [
|
|
865
|
-
shouldRenderGuards
|
|
866
|
-
createElement(FocusGuard, {
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
868
|
+
shouldRenderGuards
|
|
869
|
+
? createElement(FocusGuard, {
|
|
870
|
+
key: 'guard-inside-before',
|
|
871
|
+
'data-type': 'inside',
|
|
872
|
+
ref: mergedBeforeGuardRef,
|
|
873
|
+
onFocus: (event: FocusEvent) => {
|
|
874
|
+
if (modal) {
|
|
875
|
+
const els = getTabbableElements();
|
|
876
|
+
enqueueFocus(order[0] === 'reference' ? els[0] : els[els.length - 1]);
|
|
877
|
+
} else if (
|
|
878
|
+
portalContext != null &&
|
|
879
|
+
portalContext.preserveTabOrder &&
|
|
880
|
+
portalContext.portalNode
|
|
881
|
+
) {
|
|
882
|
+
preventReturnFocusRef.current = false;
|
|
883
|
+
if (isOutsideEvent(event, portalContext.portalNode)) {
|
|
884
|
+
getNextTabbable(domReference)?.focus();
|
|
885
|
+
} else {
|
|
886
|
+
portalContext.beforeOutsideRef.current?.focus();
|
|
887
|
+
}
|
|
883
888
|
}
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
|
|
889
|
+
},
|
|
890
|
+
})
|
|
891
|
+
: null,
|
|
887
892
|
!isUntrappedTypeableCombobox && renderDismissButton('start'),
|
|
888
|
-
children,
|
|
893
|
+
createElement(Fragment, { key: 'children', children }),
|
|
889
894
|
renderDismissButton('end'),
|
|
890
|
-
shouldRenderGuards
|
|
891
|
-
createElement(FocusGuard, {
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
895
|
+
shouldRenderGuards
|
|
896
|
+
? createElement(FocusGuard, {
|
|
897
|
+
key: 'guard-inside-after',
|
|
898
|
+
'data-type': 'inside',
|
|
899
|
+
ref: mergedAfterGuardRef,
|
|
900
|
+
onFocus: (event: FocusEvent) => {
|
|
901
|
+
if (modal) {
|
|
902
|
+
enqueueFocus(getTabbableElements()[0]);
|
|
903
|
+
} else if (
|
|
904
|
+
portalContext != null &&
|
|
905
|
+
portalContext.preserveTabOrder &&
|
|
906
|
+
portalContext.portalNode
|
|
907
|
+
) {
|
|
908
|
+
if (closeOnFocusOut) {
|
|
909
|
+
preventReturnFocusRef.current = true;
|
|
910
|
+
}
|
|
911
|
+
if (isOutsideEvent(event, portalContext.portalNode)) {
|
|
912
|
+
getPreviousTabbable(domReference)?.focus();
|
|
913
|
+
} else {
|
|
914
|
+
portalContext.afterOutsideRef.current?.focus();
|
|
915
|
+
}
|
|
904
916
|
}
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
portalContext.afterOutsideRef.current?.focus();
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
},
|
|
912
|
-
}),
|
|
917
|
+
},
|
|
918
|
+
})
|
|
919
|
+
: null,
|
|
913
920
|
];
|
|
914
921
|
}
|
package/src/FloatingPortal.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
createContext,
|
|
9
9
|
createElement,
|
|
10
10
|
createPortal,
|
|
11
|
+
Fragment,
|
|
11
12
|
useContext,
|
|
12
13
|
useEffect,
|
|
13
14
|
useMemo,
|
|
@@ -51,6 +52,9 @@ export function FocusGuard(
|
|
|
51
52
|
},
|
|
52
53
|
): OctaneNode {
|
|
53
54
|
const [role, setRole] = useState<'button' | undefined>(undefined, S('FocusGuard:role'));
|
|
55
|
+
const elementRef = useRef<HTMLSpanElement | null>(null, S('FocusGuard:element'));
|
|
56
|
+
const forwardedRef = props.ref;
|
|
57
|
+
const onFocus = props.onFocus;
|
|
54
58
|
useModernLayoutEffect(
|
|
55
59
|
() => {
|
|
56
60
|
if (isSafari()) {
|
|
@@ -60,8 +64,28 @@ export function FocusGuard(
|
|
|
60
64
|
[],
|
|
61
65
|
S('FocusGuard:eff'),
|
|
62
66
|
);
|
|
67
|
+
useModernLayoutEffect(
|
|
68
|
+
() => {
|
|
69
|
+
const element = elementRef.current;
|
|
70
|
+
if (!element || !onFocus) return;
|
|
71
|
+
const handleFocus = (event: FocusEvent) => onFocus(event as any);
|
|
72
|
+
element.addEventListener('focusin', handleFocus);
|
|
73
|
+
return () => element.removeEventListener('focusin', handleFocus);
|
|
74
|
+
},
|
|
75
|
+
[onFocus],
|
|
76
|
+
S('FocusGuard:focus'),
|
|
77
|
+
);
|
|
78
|
+
const { ref: _ref, onFocus: _onFocus, ...elementProps } = props;
|
|
63
79
|
return createElement('span', {
|
|
64
|
-
...
|
|
80
|
+
...elementProps,
|
|
81
|
+
ref: (element: HTMLSpanElement | null) => {
|
|
82
|
+
elementRef.current = element;
|
|
83
|
+
if (typeof forwardedRef === 'function') {
|
|
84
|
+
forwardedRef(element);
|
|
85
|
+
} else if (forwardedRef) {
|
|
86
|
+
forwardedRef.current = element;
|
|
87
|
+
}
|
|
88
|
+
},
|
|
65
89
|
tabIndex: 0,
|
|
66
90
|
role,
|
|
67
91
|
'aria-hidden': role ? undefined : true,
|
|
@@ -291,40 +315,51 @@ export function FloatingPortal(props: FloatingPortalProps): OctaneNode {
|
|
|
291
315
|
return createElement(PortalContext.Provider, {
|
|
292
316
|
value,
|
|
293
317
|
children: [
|
|
294
|
-
shouldRenderGuards &&
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
createElement('span', {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
318
|
+
shouldRenderGuards && portalNode
|
|
319
|
+
? createElement(FocusGuard, {
|
|
320
|
+
key: 'guard-outside-before',
|
|
321
|
+
'data-type': 'outside',
|
|
322
|
+
ref: beforeOutsideRef,
|
|
323
|
+
onFocus: (event: FocusEvent) => {
|
|
324
|
+
if (isOutsideEvent(event, portalNode)) {
|
|
325
|
+
beforeInsideRef.current?.focus();
|
|
326
|
+
} else {
|
|
327
|
+
const domReference = focusManagerState ? focusManagerState.domReference : null;
|
|
328
|
+
getPreviousTabbable(domReference)?.focus();
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
})
|
|
332
|
+
: null,
|
|
333
|
+
shouldRenderGuards && portalNode
|
|
334
|
+
? createElement('span', {
|
|
335
|
+
key: 'aria-owns',
|
|
336
|
+
'aria-owns': portalNode.id,
|
|
337
|
+
style: HIDDEN_OWNER_STYLES,
|
|
338
|
+
})
|
|
339
|
+
: null,
|
|
340
|
+
portalNode
|
|
341
|
+
? createElement(Fragment, {
|
|
342
|
+
key: 'portal-children',
|
|
343
|
+
children: createPortal(children, portalNode),
|
|
344
|
+
})
|
|
345
|
+
: null,
|
|
346
|
+
shouldRenderGuards && portalNode
|
|
347
|
+
? createElement(FocusGuard, {
|
|
348
|
+
key: 'guard-outside-after',
|
|
349
|
+
'data-type': 'outside',
|
|
350
|
+
ref: afterOutsideRef,
|
|
351
|
+
onFocus: (event: FocusEvent) => {
|
|
352
|
+
if (isOutsideEvent(event, portalNode)) {
|
|
353
|
+
afterInsideRef.current?.focus();
|
|
354
|
+
} else {
|
|
355
|
+
const domReference = focusManagerState ? focusManagerState.domReference : null;
|
|
356
|
+
getNextTabbable(domReference)?.focus();
|
|
357
|
+
focusManagerState?.closeOnFocusOut &&
|
|
358
|
+
focusManagerState?.onOpenChange(false, event, 'focus-out');
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
})
|
|
362
|
+
: null,
|
|
328
363
|
],
|
|
329
364
|
});
|
|
330
365
|
}
|
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';
|