@vuu-ui/vuu-utils 2.0.0 → 2.1.0-alpha.2

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.
Files changed (171) hide show
  1. package/cjs/node_modules/@dnd-kit/react/hooks.js +3 -3
  2. package/cjs/node_modules/@floating-ui/dom/dist/floating-ui.dom.js +492 -0
  3. package/cjs/node_modules/@floating-ui/dom/dist/floating-ui.dom.js.map +1 -0
  4. package/cjs/node_modules/@floating-ui/react/dist/floating-ui.react.js +987 -0
  5. package/cjs/node_modules/@floating-ui/react/dist/floating-ui.react.js.map +1 -0
  6. package/cjs/node_modules/@floating-ui/react/dist/floating-ui.react.utils.js +130 -0
  7. package/cjs/node_modules/@floating-ui/react/dist/floating-ui.react.utils.js.map +1 -0
  8. package/cjs/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js +183 -0
  9. package/cjs/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js.map +1 -0
  10. package/cjs/node_modules/@floating-ui/utils/dist/floating-ui.utils.js +39 -0
  11. package/cjs/node_modules/@floating-ui/utils/dist/floating-ui.utils.js.map +1 -0
  12. package/cjs/node_modules/@salt-ds/core/dist-es/aria-announcer/AriaAnnouncerContext.js +10 -0
  13. package/cjs/node_modules/@salt-ds/core/dist-es/aria-announcer/AriaAnnouncerContext.js.map +1 -0
  14. package/cjs/node_modules/@salt-ds/core/dist-es/aria-announcer/AriaAnnouncerProvider.js +76 -0
  15. package/cjs/node_modules/@salt-ds/core/dist-es/aria-announcer/AriaAnnouncerProvider.js.map +1 -0
  16. package/cjs/node_modules/@salt-ds/core/dist-es/aria-announcer/useAriaAnnouncer.js +52 -0
  17. package/cjs/node_modules/@salt-ds/core/dist-es/aria-announcer/useAriaAnnouncer.js.map +1 -0
  18. package/cjs/node_modules/@salt-ds/core/dist-es/breakpoints/BreakpointProvider.js +59 -0
  19. package/cjs/node_modules/@salt-ds/core/dist-es/breakpoints/BreakpointProvider.js.map +1 -0
  20. package/cjs/node_modules/@salt-ds/core/dist-es/breakpoints/Breakpoints.js +12 -0
  21. package/cjs/node_modules/@salt-ds/core/dist-es/breakpoints/Breakpoints.js.map +1 -0
  22. package/cjs/node_modules/@salt-ds/core/dist-es/button/Button.css.js +6 -0
  23. package/cjs/node_modules/@salt-ds/core/dist-es/button/Button.css.js.map +1 -0
  24. package/cjs/node_modules/@salt-ds/core/dist-es/button/Button.js +96 -0
  25. package/cjs/node_modules/@salt-ds/core/dist-es/button/Button.js.map +1 -0
  26. package/cjs/node_modules/@salt-ds/core/dist-es/button/useButton.js +65 -0
  27. package/cjs/node_modules/@salt-ds/core/dist-es/button/useButton.js.map +1 -0
  28. package/cjs/node_modules/@salt-ds/core/dist-es/salt-provider/ProviderContext.js +11 -0
  29. package/cjs/node_modules/@salt-ds/core/dist-es/salt-provider/ProviderContext.js.map +1 -0
  30. package/cjs/node_modules/@salt-ds/core/dist-es/salt-provider/SaltProvider.css.js +6 -0
  31. package/cjs/node_modules/@salt-ds/core/dist-es/salt-provider/SaltProvider.css.js.map +1 -0
  32. package/cjs/node_modules/@salt-ds/core/dist-es/salt-provider/SaltProvider.js +160 -0
  33. package/cjs/node_modules/@salt-ds/core/dist-es/salt-provider/SaltProvider.js.map +1 -0
  34. package/cjs/node_modules/@salt-ds/core/dist-es/salt-provider/ThemeApplicator.js +115 -0
  35. package/cjs/node_modules/@salt-ds/core/dist-es/salt-provider/ThemeApplicator.js.map +1 -0
  36. package/cjs/node_modules/@salt-ds/core/dist-es/spinner/Spinner.css.js +6 -0
  37. package/cjs/node_modules/@salt-ds/core/dist-es/spinner/Spinner.css.js.map +1 -0
  38. package/cjs/node_modules/@salt-ds/core/dist-es/spinner/Spinner.js +86 -0
  39. package/cjs/node_modules/@salt-ds/core/dist-es/spinner/Spinner.js.map +1 -0
  40. package/cjs/node_modules/@salt-ds/core/dist-es/spinner/svgSpinners/SpinnerSVG.js +93 -0
  41. package/cjs/node_modules/@salt-ds/core/dist-es/spinner/svgSpinners/SpinnerSVG.js.map +1 -0
  42. package/cjs/node_modules/@salt-ds/core/dist-es/utils/createChainedFunction.js +20 -0
  43. package/cjs/node_modules/@salt-ds/core/dist-es/utils/createChainedFunction.js.map +1 -0
  44. package/cjs/node_modules/@salt-ds/core/dist-es/utils/createContext.js +14 -0
  45. package/cjs/node_modules/@salt-ds/core/dist-es/utils/createContext.js.map +1 -0
  46. package/cjs/node_modules/@salt-ds/core/dist-es/utils/debounce.js +22 -0
  47. package/cjs/node_modules/@salt-ds/core/dist-es/utils/debounce.js.map +1 -0
  48. package/cjs/node_modules/@salt-ds/core/dist-es/utils/makePrefixer.js +6 -0
  49. package/cjs/node_modules/@salt-ds/core/dist-es/utils/makePrefixer.js.map +1 -0
  50. package/cjs/node_modules/@salt-ds/core/dist-es/utils/useFloatingUI/useFloatingUI.js +52 -0
  51. package/cjs/node_modules/@salt-ds/core/dist-es/utils/useFloatingUI/useFloatingUI.js.map +1 -0
  52. package/cjs/node_modules/@salt-ds/core/dist-es/utils/useId.js +45 -0
  53. package/cjs/node_modules/@salt-ds/core/dist-es/utils/useId.js.map +1 -0
  54. package/cjs/node_modules/@salt-ds/core/dist-es/utils/useIsomorphicLayoutEffect.js +8 -0
  55. package/cjs/node_modules/@salt-ds/core/dist-es/utils/useIsomorphicLayoutEffect.js.map +1 -0
  56. package/cjs/node_modules/@salt-ds/core/dist-es/utils/usePreventScroll.js +233 -0
  57. package/cjs/node_modules/@salt-ds/core/dist-es/utils/usePreventScroll.js.map +1 -0
  58. package/cjs/node_modules/@salt-ds/core/dist-es/viewport/ViewportProvider.js +35 -0
  59. package/cjs/node_modules/@salt-ds/core/dist-es/viewport/ViewportProvider.js.map +1 -0
  60. package/cjs/node_modules/@salt-ds/styles/dist-es/style-injection-provider/index.js +22 -0
  61. package/cjs/node_modules/@salt-ds/styles/dist-es/style-injection-provider/index.js.map +1 -0
  62. package/cjs/node_modules/@salt-ds/styles/dist-es/use-style-injection/InsertionPointProvider.js +16 -0
  63. package/cjs/node_modules/@salt-ds/styles/dist-es/use-style-injection/InsertionPointProvider.js.map +1 -0
  64. package/cjs/node_modules/@salt-ds/styles/dist-es/use-style-injection/useStyleInjection.js +75 -0
  65. package/cjs/node_modules/@salt-ds/styles/dist-es/use-style-injection/useStyleInjection.js.map +1 -0
  66. package/cjs/node_modules/@salt-ds/window/dist-es/WindowProvider.js +17 -0
  67. package/cjs/node_modules/@salt-ds/window/dist-es/WindowProvider.js.map +1 -0
  68. package/cjs/node_modules/tabbable/dist/index.esm.js +550 -0
  69. package/cjs/node_modules/tabbable/dist/index.esm.js.map +1 -0
  70. package/cjs/packages/vuu-utils/src/ThemeProvider.js +2 -2
  71. package/cjs/packages/vuu-utils/src/ThemeProvider.js.map +1 -1
  72. package/cjs/packages/vuu-utils/src/data-editing/DataEditingProvider.js +25 -0
  73. package/cjs/packages/vuu-utils/src/data-editing/DataEditingProvider.js.map +1 -0
  74. package/cjs/packages/vuu-utils/src/data-editing/EditButtons.js +23 -0
  75. package/cjs/packages/vuu-utils/src/data-editing/EditButtons.js.map +1 -0
  76. package/cjs/packages/vuu-utils/src/data-editing/EditTracker.js +154 -0
  77. package/cjs/packages/vuu-utils/src/data-editing/EditTracker.js.map +1 -0
  78. package/cjs/packages/vuu-utils/src/data-editing/useEditableTable.js +69 -0
  79. package/cjs/packages/vuu-utils/src/data-editing/useEditableTable.js.map +1 -0
  80. package/cjs/packages/vuu-utils/src/index.js +9 -0
  81. package/cjs/packages/vuu-utils/src/index.js.map +1 -1
  82. package/esm/node_modules/@floating-ui/dom/dist/floating-ui.dom.js +489 -0
  83. package/esm/node_modules/@floating-ui/dom/dist/floating-ui.dom.js.map +1 -0
  84. package/esm/node_modules/@floating-ui/react/dist/floating-ui.react.js +962 -0
  85. package/esm/node_modules/@floating-ui/react/dist/floating-ui.react.js.map +1 -0
  86. package/esm/node_modules/@floating-ui/react/dist/floating-ui.react.utils.js +114 -0
  87. package/esm/node_modules/@floating-ui/react/dist/floating-ui.react.utils.js.map +1 -0
  88. package/esm/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js +162 -0
  89. package/esm/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js.map +1 -0
  90. package/esm/node_modules/@floating-ui/utils/dist/floating-ui.utils.js +33 -0
  91. package/esm/node_modules/@floating-ui/utils/dist/floating-ui.utils.js.map +1 -0
  92. package/esm/node_modules/@salt-ds/core/dist-es/aria-announcer/AriaAnnouncerContext.js +8 -0
  93. package/esm/node_modules/@salt-ds/core/dist-es/aria-announcer/AriaAnnouncerContext.js.map +1 -0
  94. package/esm/node_modules/@salt-ds/core/dist-es/aria-announcer/AriaAnnouncerProvider.js +73 -0
  95. package/esm/node_modules/@salt-ds/core/dist-es/aria-announcer/AriaAnnouncerProvider.js.map +1 -0
  96. package/esm/node_modules/@salt-ds/core/dist-es/aria-announcer/useAriaAnnouncer.js +50 -0
  97. package/esm/node_modules/@salt-ds/core/dist-es/aria-announcer/useAriaAnnouncer.js.map +1 -0
  98. package/esm/node_modules/@salt-ds/core/dist-es/breakpoints/BreakpointProvider.js +56 -0
  99. package/esm/node_modules/@salt-ds/core/dist-es/breakpoints/BreakpointProvider.js.map +1 -0
  100. package/esm/node_modules/@salt-ds/core/dist-es/breakpoints/Breakpoints.js +10 -0
  101. package/esm/node_modules/@salt-ds/core/dist-es/breakpoints/Breakpoints.js.map +1 -0
  102. package/esm/node_modules/@salt-ds/core/dist-es/button/Button.css.js +4 -0
  103. package/esm/node_modules/@salt-ds/core/dist-es/button/Button.css.js.map +1 -0
  104. package/esm/node_modules/@salt-ds/core/dist-es/button/Button.js +94 -0
  105. package/esm/node_modules/@salt-ds/core/dist-es/button/Button.js.map +1 -0
  106. package/esm/node_modules/@salt-ds/core/dist-es/button/useButton.js +63 -0
  107. package/esm/node_modules/@salt-ds/core/dist-es/button/useButton.js.map +1 -0
  108. package/esm/node_modules/@salt-ds/core/dist-es/salt-provider/ProviderContext.js +9 -0
  109. package/esm/node_modules/@salt-ds/core/dist-es/salt-provider/ProviderContext.js.map +1 -0
  110. package/esm/node_modules/@salt-ds/core/dist-es/salt-provider/SaltProvider.css.js +4 -0
  111. package/esm/node_modules/@salt-ds/core/dist-es/salt-provider/SaltProvider.css.js.map +1 -0
  112. package/esm/node_modules/@salt-ds/core/dist-es/salt-provider/SaltProvider.js +151 -0
  113. package/esm/node_modules/@salt-ds/core/dist-es/salt-provider/SaltProvider.js.map +1 -0
  114. package/esm/node_modules/@salt-ds/core/dist-es/salt-provider/ThemeApplicator.js +113 -0
  115. package/esm/node_modules/@salt-ds/core/dist-es/salt-provider/ThemeApplicator.js.map +1 -0
  116. package/esm/node_modules/@salt-ds/core/dist-es/spinner/Spinner.css.js +4 -0
  117. package/esm/node_modules/@salt-ds/core/dist-es/spinner/Spinner.css.js.map +1 -0
  118. package/esm/node_modules/@salt-ds/core/dist-es/spinner/Spinner.js +84 -0
  119. package/esm/node_modules/@salt-ds/core/dist-es/spinner/Spinner.js.map +1 -0
  120. package/esm/node_modules/@salt-ds/core/dist-es/spinner/svgSpinners/SpinnerSVG.js +91 -0
  121. package/esm/node_modules/@salt-ds/core/dist-es/spinner/svgSpinners/SpinnerSVG.js.map +1 -0
  122. package/esm/node_modules/@salt-ds/core/dist-es/utils/createChainedFunction.js +18 -0
  123. package/esm/node_modules/@salt-ds/core/dist-es/utils/createChainedFunction.js.map +1 -0
  124. package/esm/node_modules/@salt-ds/core/dist-es/utils/createContext.js +12 -0
  125. package/esm/node_modules/@salt-ds/core/dist-es/utils/createContext.js.map +1 -0
  126. package/esm/node_modules/@salt-ds/core/dist-es/utils/debounce.js +20 -0
  127. package/esm/node_modules/@salt-ds/core/dist-es/utils/debounce.js.map +1 -0
  128. package/esm/node_modules/@salt-ds/core/dist-es/utils/makePrefixer.js +4 -0
  129. package/esm/node_modules/@salt-ds/core/dist-es/utils/makePrefixer.js.map +1 -0
  130. package/esm/node_modules/@salt-ds/core/dist-es/utils/useFloatingUI/useFloatingUI.js +50 -0
  131. package/esm/node_modules/@salt-ds/core/dist-es/utils/useFloatingUI/useFloatingUI.js.map +1 -0
  132. package/esm/node_modules/@salt-ds/core/dist-es/utils/useId.js +24 -0
  133. package/esm/node_modules/@salt-ds/core/dist-es/utils/useId.js.map +1 -0
  134. package/esm/node_modules/@salt-ds/core/dist-es/utils/useIsomorphicLayoutEffect.js +6 -0
  135. package/esm/node_modules/@salt-ds/core/dist-es/utils/useIsomorphicLayoutEffect.js.map +1 -0
  136. package/esm/node_modules/@salt-ds/core/dist-es/utils/usePreventScroll.js +231 -0
  137. package/esm/node_modules/@salt-ds/core/dist-es/utils/usePreventScroll.js.map +1 -0
  138. package/esm/node_modules/@salt-ds/core/dist-es/viewport/ViewportProvider.js +32 -0
  139. package/esm/node_modules/@salt-ds/core/dist-es/viewport/ViewportProvider.js.map +1 -0
  140. package/esm/node_modules/@salt-ds/styles/dist-es/style-injection-provider/index.js +19 -0
  141. package/esm/node_modules/@salt-ds/styles/dist-es/style-injection-provider/index.js.map +1 -0
  142. package/esm/node_modules/@salt-ds/styles/dist-es/use-style-injection/InsertionPointProvider.js +14 -0
  143. package/esm/node_modules/@salt-ds/styles/dist-es/use-style-injection/InsertionPointProvider.js.map +1 -0
  144. package/esm/node_modules/@salt-ds/styles/dist-es/use-style-injection/useStyleInjection.js +54 -0
  145. package/esm/node_modules/@salt-ds/styles/dist-es/use-style-injection/useStyleInjection.js.map +1 -0
  146. package/esm/node_modules/@salt-ds/window/dist-es/WindowProvider.js +15 -0
  147. package/esm/node_modules/@salt-ds/window/dist-es/WindowProvider.js.map +1 -0
  148. package/esm/node_modules/tabbable/dist/index.esm.js +546 -0
  149. package/esm/node_modules/tabbable/dist/index.esm.js.map +1 -0
  150. package/esm/packages/vuu-utils/src/ThemeProvider.js +2 -2
  151. package/esm/packages/vuu-utils/src/ThemeProvider.js.map +1 -1
  152. package/esm/packages/vuu-utils/src/context-definitions/WorkspaceContext.js +2 -2
  153. package/esm/packages/vuu-utils/src/context-definitions/WorkspaceContext.js.map +1 -1
  154. package/esm/packages/vuu-utils/src/data-editing/DataEditingProvider.js +22 -0
  155. package/esm/packages/vuu-utils/src/data-editing/DataEditingProvider.js.map +1 -0
  156. package/esm/packages/vuu-utils/src/data-editing/EditButtons.js +21 -0
  157. package/esm/packages/vuu-utils/src/data-editing/EditButtons.js.map +1 -0
  158. package/esm/packages/vuu-utils/src/data-editing/EditTracker.js +152 -0
  159. package/esm/packages/vuu-utils/src/data-editing/EditTracker.js.map +1 -0
  160. package/esm/packages/vuu-utils/src/data-editing/useEditableTable.js +67 -0
  161. package/esm/packages/vuu-utils/src/data-editing/useEditableTable.js.map +1 -0
  162. package/esm/packages/vuu-utils/src/feature-utils.js +2 -2
  163. package/esm/packages/vuu-utils/src/feature-utils.js.map +1 -1
  164. package/esm/packages/vuu-utils/src/index.js +4 -0
  165. package/esm/packages/vuu-utils/src/index.js.map +1 -1
  166. package/package.json +6 -6
  167. package/types/data-editing/DataEditingProvider.d.ts +7 -0
  168. package/types/data-editing/EditButtons.d.ts +7 -0
  169. package/types/data-editing/EditTracker.d.ts +24 -0
  170. package/types/data-editing/useEditableTable.d.ts +15 -0
  171. package/types/index.d.ts +5 -1
@@ -0,0 +1,962 @@
1
+ import * as React from 'react';
2
+ import { useLayoutEffect, useEffect, useRef } from 'react';
3
+ import { isTypeableCombobox, getDocument, activeElement, contains, isVirtualClick, isVirtualPointerEvent, isSafari, stopEvent, getTarget } from './floating-ui.react.utils.js';
4
+ import { isHTMLElement, isElement, getNodeName } from '../../utils/dist/floating-ui.utils.dom.js';
5
+ export { getOverflowAncestors } from '../../utils/dist/floating-ui.utils.dom.js';
6
+ import { isTabbable, tabbable } from '../../../tabbable/dist/index.esm.js';
7
+ import * as ReactDOM from 'react-dom';
8
+
9
+ // https://github.com/mui/material-ui/issues/41190#issuecomment-2040873379
10
+ const SafeReact = {
11
+ ...React
12
+ };
13
+
14
+ const useInsertionEffect = SafeReact.useInsertionEffect;
15
+ const useSafeInsertionEffect = useInsertionEffect || (fn => fn());
16
+ function useEffectEvent(callback) {
17
+ const ref = React.useRef(() => {
18
+ if (process.env.NODE_ENV !== "production") {
19
+ throw new Error('Cannot call an event handler while rendering.');
20
+ }
21
+ });
22
+ useSafeInsertionEffect(() => {
23
+ ref.current = callback;
24
+ });
25
+ return React.useCallback(function () {
26
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
27
+ args[_key] = arguments[_key];
28
+ }
29
+ return ref.current == null ? void 0 : ref.current(...args);
30
+ }, []);
31
+ }
32
+
33
+ var index = typeof document !== 'undefined' ? useLayoutEffect : useEffect;
34
+
35
+ function _extends() {
36
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
37
+ for (var i = 1; i < arguments.length; i++) {
38
+ var source = arguments[i];
39
+ for (var key in source) {
40
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
41
+ target[key] = source[key];
42
+ }
43
+ }
44
+ }
45
+ return target;
46
+ };
47
+ return _extends.apply(this, arguments);
48
+ }
49
+
50
+ let serverHandoffComplete = false;
51
+ let count = 0;
52
+ const genId = () => // Ensure the id is unique with multiple independent versions of Floating UI
53
+ // on <React 18
54
+ "floating-ui-" + Math.random().toString(36).slice(2, 6) + count++;
55
+ function useFloatingId() {
56
+ const [id, setId] = React.useState(() => serverHandoffComplete ? genId() : undefined);
57
+ index(() => {
58
+ if (id == null) {
59
+ setId(genId());
60
+ }
61
+ // eslint-disable-next-line react-hooks/exhaustive-deps
62
+ }, []);
63
+ React.useEffect(() => {
64
+ serverHandoffComplete = true;
65
+ }, []);
66
+ return id;
67
+ }
68
+ const useReactId = SafeReact.useId;
69
+
70
+ /**
71
+ * Uses React 18's built-in `useId()` when available, or falls back to a
72
+ * slightly less performant (requiring a double render) implementation for
73
+ * earlier React versions.
74
+ * @see https://floating-ui.com/docs/react-utils#useid
75
+ */
76
+ const useId = useReactId || useFloatingId;
77
+ if (process.env.NODE_ENV !== "production") ;
78
+ const FloatingTreeContext = /*#__PURE__*/React.createContext(null);
79
+
80
+ /**
81
+ * Returns the nearest floating tree context, if available.
82
+ */
83
+ const useFloatingTree = () => React.useContext(FloatingTreeContext);
84
+
85
+ function createAttribute(name) {
86
+ return "data-floating-ui-" + name;
87
+ }
88
+
89
+ function useLatestRef(value) {
90
+ const ref = useRef(value);
91
+ index(() => {
92
+ ref.current = value;
93
+ });
94
+ return ref;
95
+ }
96
+
97
+ let rafId = 0;
98
+ function enqueueFocus(el, options) {
99
+ if (options === void 0) {
100
+ options = {};
101
+ }
102
+ const {
103
+ preventScroll = false,
104
+ cancelPrevious = true,
105
+ sync = false
106
+ } = options;
107
+ cancelPrevious && cancelAnimationFrame(rafId);
108
+ const exec = () => el == null ? void 0 : el.focus({
109
+ preventScroll
110
+ });
111
+ if (sync) {
112
+ exec();
113
+ } else {
114
+ rafId = requestAnimationFrame(exec);
115
+ }
116
+ }
117
+
118
+ function getAncestors(nodes, id) {
119
+ var _nodes$find;
120
+ let allAncestors = [];
121
+ let currentParentId = (_nodes$find = nodes.find(node => node.id === id)) == null ? void 0 : _nodes$find.parentId;
122
+ while (currentParentId) {
123
+ const currentNode = nodes.find(node => node.id === currentParentId);
124
+ currentParentId = currentNode == null ? void 0 : currentNode.parentId;
125
+ if (currentNode) {
126
+ allAncestors = allAncestors.concat(currentNode);
127
+ }
128
+ }
129
+ return allAncestors;
130
+ }
131
+
132
+ function getChildren(nodes, id) {
133
+ let allChildren = nodes.filter(node => {
134
+ var _node$context;
135
+ return node.parentId === id && ((_node$context = node.context) == null ? void 0 : _node$context.open);
136
+ });
137
+ let currentChildren = allChildren;
138
+ while (currentChildren.length) {
139
+ currentChildren = nodes.filter(node => {
140
+ var _currentChildren;
141
+ return (_currentChildren = currentChildren) == null ? void 0 : _currentChildren.some(n => {
142
+ var _node$context2;
143
+ return node.parentId === n.id && ((_node$context2 = node.context) == null ? void 0 : _node$context2.open);
144
+ });
145
+ });
146
+ allChildren = allChildren.concat(currentChildren);
147
+ }
148
+ return allChildren;
149
+ }
150
+
151
+ // Modified to add conditional `aria-hidden` support:
152
+ // https://github.com/theKashey/aria-hidden/blob/9220c8f4a4fd35f63bee5510a9f41a37264382d4/src/index.ts
153
+ let counterMap = /*#__PURE__*/new WeakMap();
154
+ let uncontrolledElementsSet = /*#__PURE__*/new WeakSet();
155
+ let markerMap = {};
156
+ let lockCount$1 = 0;
157
+ const supportsInert = () => typeof HTMLElement !== 'undefined' && 'inert' in HTMLElement.prototype;
158
+ const unwrapHost = node => node && (node.host || unwrapHost(node.parentNode));
159
+ const correctElements = (parent, targets) => targets.map(target => {
160
+ if (parent.contains(target)) {
161
+ return target;
162
+ }
163
+ const correctedTarget = unwrapHost(target);
164
+ if (parent.contains(correctedTarget)) {
165
+ return correctedTarget;
166
+ }
167
+ return null;
168
+ }).filter(x => x != null);
169
+ function applyAttributeToOthers(uncorrectedAvoidElements, body, ariaHidden, inert) {
170
+ const markerName = 'data-floating-ui-inert';
171
+ const controlAttribute = inert ? 'inert' : ariaHidden ? 'aria-hidden' : null;
172
+ const avoidElements = correctElements(body, uncorrectedAvoidElements);
173
+ const elementsToKeep = new Set();
174
+ const elementsToStop = new Set(avoidElements);
175
+ const hiddenElements = [];
176
+ if (!markerMap[markerName]) {
177
+ markerMap[markerName] = new WeakMap();
178
+ }
179
+ const markerCounter = markerMap[markerName];
180
+ avoidElements.forEach(keep);
181
+ deep(body);
182
+ elementsToKeep.clear();
183
+ function keep(el) {
184
+ if (!el || elementsToKeep.has(el)) {
185
+ return;
186
+ }
187
+ elementsToKeep.add(el);
188
+ el.parentNode && keep(el.parentNode);
189
+ }
190
+ function deep(parent) {
191
+ if (!parent || elementsToStop.has(parent)) {
192
+ return;
193
+ }
194
+ [].forEach.call(parent.children, node => {
195
+ if (getNodeName(node) === 'script') return;
196
+ if (elementsToKeep.has(node)) {
197
+ deep(node);
198
+ } else {
199
+ const attr = controlAttribute ? node.getAttribute(controlAttribute) : null;
200
+ const alreadyHidden = attr !== null && attr !== 'false';
201
+ const counterValue = (counterMap.get(node) || 0) + 1;
202
+ const markerValue = (markerCounter.get(node) || 0) + 1;
203
+ counterMap.set(node, counterValue);
204
+ markerCounter.set(node, markerValue);
205
+ hiddenElements.push(node);
206
+ if (counterValue === 1 && alreadyHidden) {
207
+ uncontrolledElementsSet.add(node);
208
+ }
209
+ if (markerValue === 1) {
210
+ node.setAttribute(markerName, '');
211
+ }
212
+ if (!alreadyHidden && controlAttribute) {
213
+ node.setAttribute(controlAttribute, 'true');
214
+ }
215
+ }
216
+ });
217
+ }
218
+ lockCount$1++;
219
+ return () => {
220
+ hiddenElements.forEach(element => {
221
+ const counterValue = (counterMap.get(element) || 0) - 1;
222
+ const markerValue = (markerCounter.get(element) || 0) - 1;
223
+ counterMap.set(element, counterValue);
224
+ markerCounter.set(element, markerValue);
225
+ if (!counterValue) {
226
+ if (!uncontrolledElementsSet.has(element) && controlAttribute) {
227
+ element.removeAttribute(controlAttribute);
228
+ }
229
+ uncontrolledElementsSet.delete(element);
230
+ }
231
+ if (!markerValue) {
232
+ element.removeAttribute(markerName);
233
+ }
234
+ });
235
+ lockCount$1--;
236
+ if (!lockCount$1) {
237
+ counterMap = new WeakMap();
238
+ counterMap = new WeakMap();
239
+ uncontrolledElementsSet = new WeakSet();
240
+ markerMap = {};
241
+ }
242
+ };
243
+ }
244
+ function markOthers(avoidElements, ariaHidden, inert) {
245
+ if (ariaHidden === void 0) {
246
+ ariaHidden = false;
247
+ }
248
+ if (inert === void 0) {
249
+ inert = false;
250
+ }
251
+ const body = getDocument(avoidElements[0]).body;
252
+ return applyAttributeToOthers(avoidElements.concat(Array.from(body.querySelectorAll('[aria-live]'))), body, ariaHidden, inert);
253
+ }
254
+
255
+ const getTabbableOptions = () => ({
256
+ getShadowRoot: true,
257
+ displayCheck:
258
+ // JSDOM does not support the `tabbable` library. To solve this we can
259
+ // check if `ResizeObserver` is a real function (not polyfilled), which
260
+ // determines if the current environment is JSDOM-like.
261
+ typeof ResizeObserver === 'function' && ResizeObserver.toString().includes('[native code]') ? 'full' : 'none'
262
+ });
263
+ function getTabbableIn(container, direction) {
264
+ const allTabbable = tabbable(container, getTabbableOptions());
265
+ if (direction === 'prev') {
266
+ allTabbable.reverse();
267
+ }
268
+ const activeIndex = allTabbable.indexOf(activeElement(getDocument(container)));
269
+ const nextTabbableElements = allTabbable.slice(activeIndex + 1);
270
+ return nextTabbableElements[0];
271
+ }
272
+ function getNextTabbable() {
273
+ return getTabbableIn(document.body, 'next');
274
+ }
275
+ function getPreviousTabbable() {
276
+ return getTabbableIn(document.body, 'prev');
277
+ }
278
+ function isOutsideEvent(event, container) {
279
+ const containerElement = container || event.currentTarget;
280
+ const relatedTarget = event.relatedTarget;
281
+ return !relatedTarget || !contains(containerElement, relatedTarget);
282
+ }
283
+ function disableFocusInside(container) {
284
+ const tabbableElements = tabbable(container, getTabbableOptions());
285
+ tabbableElements.forEach(element => {
286
+ element.dataset.tabindex = element.getAttribute('tabindex') || '';
287
+ element.setAttribute('tabindex', '-1');
288
+ });
289
+ }
290
+ function enableFocusInside(container) {
291
+ const elements = container.querySelectorAll('[data-tabindex]');
292
+ elements.forEach(element => {
293
+ const tabindex = element.dataset.tabindex;
294
+ delete element.dataset.tabindex;
295
+ if (tabindex) {
296
+ element.setAttribute('tabindex', tabindex);
297
+ } else {
298
+ element.removeAttribute('tabindex');
299
+ }
300
+ });
301
+ }
302
+
303
+ // See Diego Haz's Sandbox for making this logic work well on Safari/iOS:
304
+ // https://codesandbox.io/s/tabbable-portal-f4tng?file=/src/FocusTrap.tsx
305
+
306
+ const HIDDEN_STYLES = {
307
+ border: 0,
308
+ clip: 'rect(0 0 0 0)',
309
+ height: '1px',
310
+ margin: '-1px',
311
+ overflow: 'hidden',
312
+ padding: 0,
313
+ position: 'fixed',
314
+ whiteSpace: 'nowrap',
315
+ width: '1px',
316
+ top: 0,
317
+ left: 0
318
+ };
319
+ let timeoutId;
320
+ function setActiveElementOnTab(event) {
321
+ if (event.key === 'Tab') {
322
+ event.target;
323
+ clearTimeout(timeoutId);
324
+ }
325
+ }
326
+ const FocusGuard = /*#__PURE__*/React.forwardRef(function FocusGuard(props, ref) {
327
+ const [role, setRole] = React.useState();
328
+ index(() => {
329
+ if (isSafari()) {
330
+ // Unlike other screen readers such as NVDA and JAWS, the virtual cursor
331
+ // on VoiceOver does trigger the onFocus event, so we can use the focus
332
+ // trap element. On Safari, only buttons trigger the onFocus event.
333
+ // NB: "group" role in the Sandbox no longer appears to work, must be a
334
+ // button role.
335
+ setRole('button');
336
+ }
337
+ document.addEventListener('keydown', setActiveElementOnTab);
338
+ return () => {
339
+ document.removeEventListener('keydown', setActiveElementOnTab);
340
+ };
341
+ }, []);
342
+ const restProps = {
343
+ ref,
344
+ tabIndex: 0,
345
+ // Role is only for VoiceOver
346
+ role,
347
+ 'aria-hidden': role ? undefined : true,
348
+ [createAttribute('focus-guard')]: '',
349
+ style: HIDDEN_STYLES
350
+ };
351
+ return /*#__PURE__*/React.createElement("span", _extends({}, props, restProps));
352
+ });
353
+
354
+ const PortalContext = /*#__PURE__*/React.createContext(null);
355
+ const attr = /*#__PURE__*/createAttribute('portal');
356
+ /**
357
+ * @see https://floating-ui.com/docs/FloatingPortal#usefloatingportalnode
358
+ */
359
+ function useFloatingPortalNode(props) {
360
+ if (props === void 0) {
361
+ props = {};
362
+ }
363
+ const {
364
+ id,
365
+ root
366
+ } = props;
367
+ const uniqueId = useId();
368
+ const portalContext = usePortalContext();
369
+ const [portalNode, setPortalNode] = React.useState(null);
370
+ const portalNodeRef = React.useRef(null);
371
+ index(() => {
372
+ return () => {
373
+ portalNode == null || portalNode.remove();
374
+ // Allow the subsequent layout effects to create a new node on updates.
375
+ // The portal node will still be cleaned up on unmount.
376
+ // https://github.com/floating-ui/floating-ui/issues/2454
377
+ queueMicrotask(() => {
378
+ portalNodeRef.current = null;
379
+ });
380
+ };
381
+ }, [portalNode]);
382
+ index(() => {
383
+ // Wait for the uniqueId to be generated before creating the portal node in
384
+ // React <18 (using `useFloatingId` instead of the native `useId`).
385
+ // https://github.com/floating-ui/floating-ui/issues/2778
386
+ if (!uniqueId) return;
387
+ if (portalNodeRef.current) return;
388
+ const existingIdRoot = id ? document.getElementById(id) : null;
389
+ if (!existingIdRoot) return;
390
+ const subRoot = document.createElement('div');
391
+ subRoot.id = uniqueId;
392
+ subRoot.setAttribute(attr, '');
393
+ existingIdRoot.appendChild(subRoot);
394
+ portalNodeRef.current = subRoot;
395
+ setPortalNode(subRoot);
396
+ }, [id, uniqueId]);
397
+ index(() => {
398
+ // Wait for the root to exist before creating the portal node. The root must
399
+ // be stored in state, not a ref, for this to work reactively.
400
+ if (root === null) return;
401
+ if (!uniqueId) return;
402
+ if (portalNodeRef.current) return;
403
+ let container = root || (portalContext == null ? void 0 : portalContext.portalNode);
404
+ if (container && !isElement(container)) container = container.current;
405
+ container = container || document.body;
406
+ let idWrapper = null;
407
+ if (id) {
408
+ idWrapper = document.createElement('div');
409
+ idWrapper.id = id;
410
+ container.appendChild(idWrapper);
411
+ }
412
+ const subRoot = document.createElement('div');
413
+ subRoot.id = uniqueId;
414
+ subRoot.setAttribute(attr, '');
415
+ container = idWrapper || container;
416
+ container.appendChild(subRoot);
417
+ portalNodeRef.current = subRoot;
418
+ setPortalNode(subRoot);
419
+ }, [id, root, uniqueId, portalContext]);
420
+ return portalNode;
421
+ }
422
+ /**
423
+ * Portals the floating element into a given container element — by default,
424
+ * outside of the app root and into the body.
425
+ * This is necessary to ensure the floating element can appear outside any
426
+ * potential parent containers that cause clipping (such as `overflow: hidden`),
427
+ * while retaining its location in the React tree.
428
+ * @see https://floating-ui.com/docs/FloatingPortal
429
+ */
430
+ function FloatingPortal(props) {
431
+ const {
432
+ children,
433
+ id,
434
+ root,
435
+ preserveTabOrder = true
436
+ } = props;
437
+ const portalNode = useFloatingPortalNode({
438
+ id,
439
+ root
440
+ });
441
+ const [focusManagerState, setFocusManagerState] = React.useState(null);
442
+ const beforeOutsideRef = React.useRef(null);
443
+ const afterOutsideRef = React.useRef(null);
444
+ const beforeInsideRef = React.useRef(null);
445
+ const afterInsideRef = React.useRef(null);
446
+ const modal = focusManagerState == null ? void 0 : focusManagerState.modal;
447
+ const open = focusManagerState == null ? void 0 : focusManagerState.open;
448
+ const shouldRenderGuards =
449
+ // The FocusManager and therefore floating element are currently open/
450
+ // rendered.
451
+ !!focusManagerState &&
452
+ // Guards are only for non-modal focus management.
453
+ !focusManagerState.modal &&
454
+ // Don't render if unmount is transitioning.
455
+ focusManagerState.open && preserveTabOrder && !!(root || portalNode);
456
+
457
+ // https://codesandbox.io/s/tabbable-portal-f4tng?file=/src/TabbablePortal.tsx
458
+ React.useEffect(() => {
459
+ if (!portalNode || !preserveTabOrder || modal) {
460
+ return;
461
+ }
462
+
463
+ // Make sure elements inside the portal element are tabbable only when the
464
+ // portal has already been focused, either by tabbing into a focus trap
465
+ // element outside or using the mouse.
466
+ function onFocus(event) {
467
+ if (portalNode && isOutsideEvent(event)) {
468
+ const focusing = event.type === 'focusin';
469
+ const manageFocus = focusing ? enableFocusInside : disableFocusInside;
470
+ manageFocus(portalNode);
471
+ }
472
+ }
473
+ // Listen to the event on the capture phase so they run before the focus
474
+ // trap elements onFocus prop is called.
475
+ portalNode.addEventListener('focusin', onFocus, true);
476
+ portalNode.addEventListener('focusout', onFocus, true);
477
+ return () => {
478
+ portalNode.removeEventListener('focusin', onFocus, true);
479
+ portalNode.removeEventListener('focusout', onFocus, true);
480
+ };
481
+ }, [portalNode, preserveTabOrder, modal]);
482
+ React.useEffect(() => {
483
+ if (!portalNode) return;
484
+ if (open) return;
485
+ enableFocusInside(portalNode);
486
+ }, [open, portalNode]);
487
+ return /*#__PURE__*/React.createElement(PortalContext.Provider, {
488
+ value: React.useMemo(() => ({
489
+ preserveTabOrder,
490
+ beforeOutsideRef,
491
+ afterOutsideRef,
492
+ beforeInsideRef,
493
+ afterInsideRef,
494
+ portalNode,
495
+ setFocusManagerState
496
+ }), [preserveTabOrder, portalNode])
497
+ }, shouldRenderGuards && portalNode && /*#__PURE__*/React.createElement(FocusGuard, {
498
+ "data-type": "outside",
499
+ ref: beforeOutsideRef,
500
+ onFocus: event => {
501
+ if (isOutsideEvent(event, portalNode)) {
502
+ var _beforeInsideRef$curr;
503
+ (_beforeInsideRef$curr = beforeInsideRef.current) == null || _beforeInsideRef$curr.focus();
504
+ } else {
505
+ const prevTabbable = getPreviousTabbable() || (focusManagerState == null ? void 0 : focusManagerState.refs.domReference.current);
506
+ prevTabbable == null || prevTabbable.focus();
507
+ }
508
+ }
509
+ }), shouldRenderGuards && portalNode && /*#__PURE__*/React.createElement("span", {
510
+ "aria-owns": portalNode.id,
511
+ style: HIDDEN_STYLES
512
+ }), portalNode && /*#__PURE__*/ReactDOM.createPortal(children, portalNode), shouldRenderGuards && portalNode && /*#__PURE__*/React.createElement(FocusGuard, {
513
+ "data-type": "outside",
514
+ ref: afterOutsideRef,
515
+ onFocus: event => {
516
+ if (isOutsideEvent(event, portalNode)) {
517
+ var _afterInsideRef$curre;
518
+ (_afterInsideRef$curre = afterInsideRef.current) == null || _afterInsideRef$curre.focus();
519
+ } else {
520
+ const nextTabbable = getNextTabbable() || (focusManagerState == null ? void 0 : focusManagerState.refs.domReference.current);
521
+ nextTabbable == null || nextTabbable.focus();
522
+ (focusManagerState == null ? void 0 : focusManagerState.closeOnFocusOut) && (focusManagerState == null ? void 0 : focusManagerState.onOpenChange(false, event.nativeEvent, 'focus-out'));
523
+ }
524
+ }
525
+ }));
526
+ }
527
+ const usePortalContext = () => React.useContext(PortalContext);
528
+
529
+ const FOCUSABLE_ATTRIBUTE = 'data-floating-ui-focusable';
530
+ function getFloatingFocusElement(floatingElement) {
531
+ if (!floatingElement) {
532
+ return null;
533
+ }
534
+ // Try to find the element that has `{...getFloatingProps()}` spread on it.
535
+ // This indicates the floating element is acting as a positioning wrapper, and
536
+ // so focus should be managed on the child element with the event handlers and
537
+ // aria props.
538
+ return floatingElement.hasAttribute(FOCUSABLE_ATTRIBUTE) ? floatingElement : floatingElement.querySelector("[" + FOCUSABLE_ATTRIBUTE + "]") || floatingElement;
539
+ }
540
+
541
+ const LIST_LIMIT = 20;
542
+ let previouslyFocusedElements = [];
543
+ function addPreviouslyFocusedElement(element) {
544
+ previouslyFocusedElements = previouslyFocusedElements.filter(el => el.isConnected);
545
+ let tabbableEl = element;
546
+ if (!tabbableEl || getNodeName(tabbableEl) === 'body') return;
547
+ if (!isTabbable(tabbableEl, getTabbableOptions())) {
548
+ const tabbableChild = tabbable(tabbableEl, getTabbableOptions())[0];
549
+ if (tabbableChild) {
550
+ tabbableEl = tabbableChild;
551
+ }
552
+ }
553
+ previouslyFocusedElements.push(tabbableEl);
554
+ if (previouslyFocusedElements.length > LIST_LIMIT) {
555
+ previouslyFocusedElements = previouslyFocusedElements.slice(-LIST_LIMIT);
556
+ }
557
+ }
558
+ function getPreviouslyFocusedElement() {
559
+ return previouslyFocusedElements.slice().reverse().find(el => el.isConnected);
560
+ }
561
+ const VisuallyHiddenDismiss = /*#__PURE__*/React.forwardRef(function VisuallyHiddenDismiss(props, ref) {
562
+ return /*#__PURE__*/React.createElement("button", _extends({}, props, {
563
+ type: "button",
564
+ ref: ref,
565
+ tabIndex: -1,
566
+ style: HIDDEN_STYLES
567
+ }));
568
+ });
569
+ /**
570
+ * Provides focus management for the floating element.
571
+ * @see https://floating-ui.com/docs/FloatingFocusManager
572
+ */
573
+ function FloatingFocusManager(props) {
574
+ const {
575
+ context,
576
+ children,
577
+ disabled = false,
578
+ order = ['content'],
579
+ guards: _guards = true,
580
+ initialFocus = 0,
581
+ returnFocus = true,
582
+ restoreFocus = false,
583
+ modal = true,
584
+ visuallyHiddenDismiss = false,
585
+ closeOnFocusOut = true
586
+ } = props;
587
+ const {
588
+ open,
589
+ refs,
590
+ nodeId,
591
+ onOpenChange,
592
+ events,
593
+ dataRef,
594
+ floatingId,
595
+ elements: {
596
+ domReference,
597
+ floating
598
+ }
599
+ } = context;
600
+ const ignoreInitialFocus = typeof initialFocus === 'number' && initialFocus < 0;
601
+ // If the reference is a combobox and is typeable (e.g. input/textarea),
602
+ // there are different focus semantics. The guards should not be rendered, but
603
+ // aria-hidden should be applied to all nodes still. Further, the visually
604
+ // hidden dismiss button should only appear at the end of the list, not the
605
+ // start.
606
+ const isUntrappedTypeableCombobox = isTypeableCombobox(domReference) && ignoreInitialFocus;
607
+
608
+ // Force the guards to be rendered if the `inert` attribute is not supported.
609
+ const guards = supportsInert() ? _guards : true;
610
+ const orderRef = useLatestRef(order);
611
+ const initialFocusRef = useLatestRef(initialFocus);
612
+ const returnFocusRef = useLatestRef(returnFocus);
613
+ const tree = useFloatingTree();
614
+ const portalContext = usePortalContext();
615
+ const startDismissButtonRef = React.useRef(null);
616
+ const endDismissButtonRef = React.useRef(null);
617
+ const preventReturnFocusRef = React.useRef(false);
618
+ const isPointerDownRef = React.useRef(false);
619
+ const tabbableIndexRef = React.useRef(-1);
620
+ const isInsidePortal = portalContext != null;
621
+ const floatingFocusElement = getFloatingFocusElement(floating);
622
+ const getTabbableContent = useEffectEvent(function (container) {
623
+ if (container === void 0) {
624
+ container = floatingFocusElement;
625
+ }
626
+ return container ? tabbable(container, getTabbableOptions()) : [];
627
+ });
628
+ const getTabbableElements = useEffectEvent(container => {
629
+ const content = getTabbableContent(container);
630
+ return orderRef.current.map(type => {
631
+ if (domReference && type === 'reference') {
632
+ return domReference;
633
+ }
634
+ if (floatingFocusElement && type === 'floating') {
635
+ return floatingFocusElement;
636
+ }
637
+ return content;
638
+ }).filter(Boolean).flat();
639
+ });
640
+ React.useEffect(() => {
641
+ if (disabled) return;
642
+ if (!modal) return;
643
+ function onKeyDown(event) {
644
+ if (event.key === 'Tab') {
645
+ // The focus guards have nothing to focus, so we need to stop the event.
646
+ if (contains(floatingFocusElement, activeElement(getDocument(floatingFocusElement))) && getTabbableContent().length === 0 && !isUntrappedTypeableCombobox) {
647
+ stopEvent(event);
648
+ }
649
+ const els = getTabbableElements();
650
+ const target = getTarget(event);
651
+ if (orderRef.current[0] === 'reference' && target === domReference) {
652
+ stopEvent(event);
653
+ if (event.shiftKey) {
654
+ enqueueFocus(els[els.length - 1]);
655
+ } else {
656
+ enqueueFocus(els[1]);
657
+ }
658
+ }
659
+ if (orderRef.current[1] === 'floating' && target === floatingFocusElement && event.shiftKey) {
660
+ stopEvent(event);
661
+ enqueueFocus(els[0]);
662
+ }
663
+ }
664
+ }
665
+ const doc = getDocument(floatingFocusElement);
666
+ doc.addEventListener('keydown', onKeyDown);
667
+ return () => {
668
+ doc.removeEventListener('keydown', onKeyDown);
669
+ };
670
+ }, [disabled, domReference, floatingFocusElement, modal, orderRef, isUntrappedTypeableCombobox, getTabbableContent, getTabbableElements]);
671
+ React.useEffect(() => {
672
+ if (disabled) return;
673
+ if (!floating) return;
674
+ function handleFocusIn(event) {
675
+ const target = getTarget(event);
676
+ const tabbableContent = getTabbableContent();
677
+ const tabbableIndex = tabbableContent.indexOf(target);
678
+ if (tabbableIndex !== -1) {
679
+ tabbableIndexRef.current = tabbableIndex;
680
+ }
681
+ }
682
+ floating.addEventListener('focusin', handleFocusIn);
683
+ return () => {
684
+ floating.removeEventListener('focusin', handleFocusIn);
685
+ };
686
+ }, [disabled, floating, getTabbableContent]);
687
+ React.useEffect(() => {
688
+ if (disabled) return;
689
+ if (!closeOnFocusOut) return;
690
+
691
+ // In Safari, buttons lose focus when pressing them.
692
+ function handlePointerDown() {
693
+ isPointerDownRef.current = true;
694
+ setTimeout(() => {
695
+ isPointerDownRef.current = false;
696
+ });
697
+ }
698
+ function handleFocusOutside(event) {
699
+ const relatedTarget = event.relatedTarget;
700
+ queueMicrotask(() => {
701
+ const movedToUnrelatedNode = !(contains(domReference, relatedTarget) || contains(floating, relatedTarget) || contains(relatedTarget, floating) || contains(portalContext == null ? void 0 : portalContext.portalNode, relatedTarget) || relatedTarget != null && relatedTarget.hasAttribute(createAttribute('focus-guard')) || tree && (getChildren(tree.nodesRef.current, nodeId).find(node => {
702
+ var _node$context, _node$context2;
703
+ return contains((_node$context = node.context) == null ? void 0 : _node$context.elements.floating, relatedTarget) || contains((_node$context2 = node.context) == null ? void 0 : _node$context2.elements.domReference, relatedTarget);
704
+ }) || getAncestors(tree.nodesRef.current, nodeId).find(node => {
705
+ var _node$context3, _node$context4;
706
+ return ((_node$context3 = node.context) == null ? void 0 : _node$context3.elements.floating) === relatedTarget || ((_node$context4 = node.context) == null ? void 0 : _node$context4.elements.domReference) === relatedTarget;
707
+ })));
708
+
709
+ // Restore focus to the previous tabbable element index to prevent
710
+ // focus from being lost outside the floating tree.
711
+ if (restoreFocus && movedToUnrelatedNode && activeElement(getDocument(floatingFocusElement)) === getDocument(floatingFocusElement).body) {
712
+ // Let `FloatingPortal` effect knows that focus is still inside the
713
+ // floating tree.
714
+ if (isHTMLElement(floatingFocusElement)) {
715
+ floatingFocusElement.focus();
716
+ }
717
+ const prevTabbableIndex = tabbableIndexRef.current;
718
+ const tabbableContent = getTabbableContent();
719
+ const nodeToFocus = tabbableContent[prevTabbableIndex] || tabbableContent[tabbableContent.length - 1] || floatingFocusElement;
720
+ if (isHTMLElement(nodeToFocus)) {
721
+ nodeToFocus.focus();
722
+ }
723
+ }
724
+
725
+ // Focus did not move inside the floating tree, and there are no tabbable
726
+ // portal guards to handle closing.
727
+ if ((isUntrappedTypeableCombobox ? true : !modal) && relatedTarget && movedToUnrelatedNode && !isPointerDownRef.current &&
728
+ // Fix React 18 Strict Mode returnFocus due to double rendering.
729
+ relatedTarget !== getPreviouslyFocusedElement()) {
730
+ preventReturnFocusRef.current = true;
731
+ onOpenChange(false, event, 'focus-out');
732
+ }
733
+ });
734
+ }
735
+ if (floating && isHTMLElement(domReference)) {
736
+ domReference.addEventListener('focusout', handleFocusOutside);
737
+ domReference.addEventListener('pointerdown', handlePointerDown);
738
+ floating.addEventListener('focusout', handleFocusOutside);
739
+ return () => {
740
+ domReference.removeEventListener('focusout', handleFocusOutside);
741
+ domReference.removeEventListener('pointerdown', handlePointerDown);
742
+ floating.removeEventListener('focusout', handleFocusOutside);
743
+ };
744
+ }
745
+ }, [disabled, domReference, floating, floatingFocusElement, modal, nodeId, tree, portalContext, onOpenChange, closeOnFocusOut, restoreFocus, getTabbableContent, isUntrappedTypeableCombobox]);
746
+ React.useEffect(() => {
747
+ var _portalContext$portal;
748
+ if (disabled) return;
749
+
750
+ // Don't hide portals nested within the parent portal.
751
+ const portalNodes = Array.from((portalContext == null || (_portalContext$portal = portalContext.portalNode) == null ? void 0 : _portalContext$portal.querySelectorAll("[" + createAttribute('portal') + "]")) || []);
752
+ if (floating) {
753
+ const insideElements = [floating, ...portalNodes, startDismissButtonRef.current, endDismissButtonRef.current, orderRef.current.includes('reference') || isUntrappedTypeableCombobox ? domReference : null].filter(x => x != null);
754
+ const cleanup = modal || isUntrappedTypeableCombobox ? markOthers(insideElements, guards, !guards) : markOthers(insideElements);
755
+ return () => {
756
+ cleanup();
757
+ };
758
+ }
759
+ }, [disabled, domReference, floating, modal, orderRef, portalContext, isUntrappedTypeableCombobox, guards]);
760
+ index(() => {
761
+ if (disabled || !isHTMLElement(floatingFocusElement)) return;
762
+ const doc = getDocument(floatingFocusElement);
763
+ const previouslyFocusedElement = activeElement(doc);
764
+
765
+ // Wait for any layout effect state setters to execute to set `tabIndex`.
766
+ queueMicrotask(() => {
767
+ const focusableElements = getTabbableElements(floatingFocusElement);
768
+ const initialFocusValue = initialFocusRef.current;
769
+ const elToFocus = (typeof initialFocusValue === 'number' ? focusableElements[initialFocusValue] : initialFocusValue.current) || floatingFocusElement;
770
+ const focusAlreadyInsideFloatingEl = contains(floatingFocusElement, previouslyFocusedElement);
771
+ if (!ignoreInitialFocus && !focusAlreadyInsideFloatingEl && open) {
772
+ enqueueFocus(elToFocus, {
773
+ preventScroll: elToFocus === floatingFocusElement
774
+ });
775
+ }
776
+ });
777
+ }, [disabled, open, floatingFocusElement, ignoreInitialFocus, getTabbableElements, initialFocusRef]);
778
+ index(() => {
779
+ if (disabled || !floatingFocusElement) return;
780
+ let preventReturnFocusScroll = false;
781
+ const doc = getDocument(floatingFocusElement);
782
+ const previouslyFocusedElement = activeElement(doc);
783
+ const contextData = dataRef.current;
784
+ let openEvent = contextData.openEvent;
785
+ addPreviouslyFocusedElement(previouslyFocusedElement);
786
+
787
+ // Dismissing via outside press should always ignore `returnFocus` to
788
+ // prevent unwanted scrolling.
789
+ function onOpenChange(_ref) {
790
+ let {
791
+ open,
792
+ reason,
793
+ event,
794
+ nested
795
+ } = _ref;
796
+ if (open) {
797
+ openEvent = event;
798
+ }
799
+ if (reason === 'escape-key' && refs.domReference.current) {
800
+ addPreviouslyFocusedElement(refs.domReference.current);
801
+ }
802
+ if (reason === 'hover' && event.type === 'mouseleave') {
803
+ preventReturnFocusRef.current = true;
804
+ }
805
+ if (reason !== 'outside-press') return;
806
+ if (nested) {
807
+ preventReturnFocusRef.current = false;
808
+ preventReturnFocusScroll = true;
809
+ } else {
810
+ preventReturnFocusRef.current = !(isVirtualClick(event) || isVirtualPointerEvent(event));
811
+ }
812
+ }
813
+ events.on('openchange', onOpenChange);
814
+ const fallbackEl = doc.createElement('span');
815
+ fallbackEl.setAttribute('tabindex', '-1');
816
+ fallbackEl.setAttribute('aria-hidden', 'true');
817
+ Object.assign(fallbackEl.style, HIDDEN_STYLES);
818
+ if (isInsidePortal && domReference) {
819
+ domReference.insertAdjacentElement('afterend', fallbackEl);
820
+ }
821
+ function getReturnElement() {
822
+ if (typeof returnFocusRef.current === 'boolean') {
823
+ return getPreviouslyFocusedElement() || fallbackEl;
824
+ }
825
+ return returnFocusRef.current.current || fallbackEl;
826
+ }
827
+ return () => {
828
+ events.off('openchange', onOpenChange);
829
+ const activeEl = activeElement(doc);
830
+ const isFocusInsideFloatingTree = contains(floating, activeEl) || tree && getChildren(tree.nodesRef.current, nodeId).some(node => {
831
+ var _node$context5;
832
+ return contains((_node$context5 = node.context) == null ? void 0 : _node$context5.elements.floating, activeEl);
833
+ });
834
+ const shouldFocusReference = isFocusInsideFloatingTree || openEvent && ['click', 'mousedown'].includes(openEvent.type);
835
+ if (shouldFocusReference && refs.domReference.current) {
836
+ addPreviouslyFocusedElement(refs.domReference.current);
837
+ }
838
+ const returnElement = getReturnElement();
839
+ queueMicrotask(() => {
840
+ if (
841
+ // eslint-disable-next-line react-hooks/exhaustive-deps
842
+ returnFocusRef.current && !preventReturnFocusRef.current && isHTMLElement(returnElement) && (
843
+ // If the focus moved somewhere else after mount, avoid returning focus
844
+ // since it likely entered a different element which should be
845
+ // respected: https://github.com/floating-ui/floating-ui/issues/2607
846
+ returnElement !== activeEl && activeEl !== doc.body ? isFocusInsideFloatingTree : true)) {
847
+ returnElement.focus({
848
+ preventScroll: preventReturnFocusScroll
849
+ });
850
+ }
851
+ fallbackEl.remove();
852
+ });
853
+ };
854
+ }, [disabled, floating, floatingFocusElement, returnFocusRef, dataRef, refs, events, tree, nodeId, isInsidePortal, domReference]);
855
+ React.useEffect(() => {
856
+ // The `returnFocus` cleanup behavior is inside a microtask; ensure we
857
+ // wait for it to complete before resetting the flag.
858
+ queueMicrotask(() => {
859
+ preventReturnFocusRef.current = false;
860
+ });
861
+ }, [disabled]);
862
+
863
+ // Synchronize the `context` & `modal` value to the FloatingPortal context.
864
+ // It will decide whether or not it needs to render its own guards.
865
+ index(() => {
866
+ if (disabled) return;
867
+ if (!portalContext) return;
868
+ portalContext.setFocusManagerState({
869
+ modal,
870
+ closeOnFocusOut,
871
+ open,
872
+ onOpenChange,
873
+ refs
874
+ });
875
+ return () => {
876
+ portalContext.setFocusManagerState(null);
877
+ };
878
+ }, [disabled, portalContext, modal, open, onOpenChange, refs, closeOnFocusOut]);
879
+ index(() => {
880
+ if (disabled) return;
881
+ if (!floatingFocusElement) return;
882
+ if (typeof MutationObserver !== 'function') return;
883
+ if (ignoreInitialFocus) return;
884
+ const handleMutation = () => {
885
+ const tabIndex = floatingFocusElement.getAttribute('tabindex');
886
+ const tabbableContent = getTabbableContent();
887
+ const activeEl = activeElement(getDocument(floating));
888
+ const tabbableIndex = tabbableContent.indexOf(activeEl);
889
+ if (tabbableIndex !== -1) {
890
+ tabbableIndexRef.current = tabbableIndex;
891
+ }
892
+ if (orderRef.current.includes('floating') || activeEl !== refs.domReference.current && tabbableContent.length === 0) {
893
+ if (tabIndex !== '0') {
894
+ floatingFocusElement.setAttribute('tabindex', '0');
895
+ }
896
+ } else if (tabIndex !== '-1') {
897
+ floatingFocusElement.setAttribute('tabindex', '-1');
898
+ }
899
+ };
900
+ handleMutation();
901
+ const observer = new MutationObserver(handleMutation);
902
+ observer.observe(floatingFocusElement, {
903
+ childList: true,
904
+ subtree: true,
905
+ attributes: true
906
+ });
907
+ return () => {
908
+ observer.disconnect();
909
+ };
910
+ }, [disabled, floating, floatingFocusElement, refs, orderRef, getTabbableContent, ignoreInitialFocus]);
911
+ function renderDismissButton(location) {
912
+ if (disabled || !visuallyHiddenDismiss || !modal) {
913
+ return null;
914
+ }
915
+ return /*#__PURE__*/React.createElement(VisuallyHiddenDismiss, {
916
+ ref: location === 'start' ? startDismissButtonRef : endDismissButtonRef,
917
+ onClick: event => onOpenChange(false, event.nativeEvent)
918
+ }, typeof visuallyHiddenDismiss === 'string' ? visuallyHiddenDismiss : 'Dismiss');
919
+ }
920
+ const shouldRenderGuards = !disabled && guards && (modal ? !isUntrappedTypeableCombobox : true) && (isInsidePortal || modal);
921
+ return /*#__PURE__*/React.createElement(React.Fragment, null, shouldRenderGuards && /*#__PURE__*/React.createElement(FocusGuard, {
922
+ "data-type": "inside",
923
+ ref: portalContext == null ? void 0 : portalContext.beforeInsideRef,
924
+ onFocus: event => {
925
+ if (modal) {
926
+ const els = getTabbableElements();
927
+ enqueueFocus(order[0] === 'reference' ? els[0] : els[els.length - 1]);
928
+ } else if (portalContext != null && portalContext.preserveTabOrder && portalContext.portalNode) {
929
+ preventReturnFocusRef.current = false;
930
+ if (isOutsideEvent(event, portalContext.portalNode)) {
931
+ const nextTabbable = getNextTabbable() || domReference;
932
+ nextTabbable == null || nextTabbable.focus();
933
+ } else {
934
+ var _portalContext$before;
935
+ (_portalContext$before = portalContext.beforeOutsideRef.current) == null || _portalContext$before.focus();
936
+ }
937
+ }
938
+ }
939
+ }), !isUntrappedTypeableCombobox && renderDismissButton('start'), children, renderDismissButton('end'), shouldRenderGuards && /*#__PURE__*/React.createElement(FocusGuard, {
940
+ "data-type": "inside",
941
+ ref: portalContext == null ? void 0 : portalContext.afterInsideRef,
942
+ onFocus: event => {
943
+ if (modal) {
944
+ enqueueFocus(getTabbableElements()[0]);
945
+ } else if (portalContext != null && portalContext.preserveTabOrder && portalContext.portalNode) {
946
+ if (closeOnFocusOut) {
947
+ preventReturnFocusRef.current = true;
948
+ }
949
+ if (isOutsideEvent(event, portalContext.portalNode)) {
950
+ const prevTabbable = getPreviousTabbable() || domReference;
951
+ prevTabbable == null || prevTabbable.focus();
952
+ } else {
953
+ var _portalContext$afterO;
954
+ (_portalContext$afterO = portalContext.afterOutsideRef.current) == null || _portalContext$afterO.focus();
955
+ }
956
+ }
957
+ }
958
+ }));
959
+ }
960
+
961
+ export { FloatingFocusManager, FloatingPortal, useFloatingPortalNode, useFloatingTree, useId };
962
+ //# sourceMappingURL=floating-ui.react.js.map