@navikt/ds-react 7.32.1 → 7.32.3

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 (221) hide show
  1. package/cjs/copybutton/CopyButton.js +4 -9
  2. package/cjs/copybutton/CopyButton.js.map +1 -1
  3. package/cjs/form/combobox/Combobox.js +1 -3
  4. package/cjs/form/combobox/Combobox.js.map +1 -1
  5. package/cjs/form/combobox/ComboboxWrapper.d.ts +1 -2
  6. package/cjs/form/combobox/ComboboxWrapper.js +1 -2
  7. package/cjs/form/combobox/ComboboxWrapper.js.map +1 -1
  8. package/cjs/form/combobox/FilteredOptions/FilteredOptions.js +28 -19
  9. package/cjs/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
  10. package/cjs/form/combobox/FilteredOptions/FilteredOptionsItem.js +4 -0
  11. package/cjs/form/combobox/FilteredOptions/FilteredOptionsItem.js.map +1 -1
  12. package/cjs/form/combobox/FilteredOptions/useVirtualFocus.js.map +1 -1
  13. package/cjs/form/combobox/Input/Input.context.d.ts +2 -0
  14. package/cjs/form/combobox/Input/Input.context.js +4 -1
  15. package/cjs/form/combobox/Input/Input.context.js.map +1 -1
  16. package/cjs/form/combobox/Input/InputController.js +2 -2
  17. package/cjs/form/combobox/Input/InputController.js.map +1 -1
  18. package/cjs/form/switch/Switch.js +3 -3
  19. package/cjs/form/switch/Switch.js.map +1 -1
  20. package/cjs/help-text/HelpText.js +3 -3
  21. package/cjs/help-text/HelpText.js.map +1 -1
  22. package/cjs/help-text/HelpTextIcon.d.ts +1 -2
  23. package/cjs/help-text/HelpTextIcon.js +3 -7
  24. package/cjs/help-text/HelpTextIcon.js.map +1 -1
  25. package/cjs/layout/page/parts/PageBlock.d.ts +9 -6
  26. package/cjs/layout/page/parts/PageBlock.js.map +1 -1
  27. package/cjs/modal/ModalUtils.js +6 -4
  28. package/cjs/modal/ModalUtils.js.map +1 -1
  29. package/cjs/overlays/dismissablelayer/DismissableLayer.js +9 -19
  30. package/cjs/overlays/dismissablelayer/DismissableLayer.js.map +1 -1
  31. package/cjs/overlays/dismissablelayer/util/usePointerDownOutside.js +5 -4
  32. package/cjs/overlays/dismissablelayer/util/usePointerDownOutside.js.map +1 -1
  33. package/cjs/overlays/floating-menu/Menu.d.ts +4 -4
  34. package/cjs/overlays/floating-menu/Menu.js +7 -4
  35. package/cjs/overlays/floating-menu/Menu.js.map +1 -1
  36. package/cjs/overlays/floating-menu/parts/RovingFocus.js +3 -3
  37. package/cjs/overlays/floating-menu/parts/RovingFocus.js.map +1 -1
  38. package/cjs/overlays/overlay/hooks/useAnimationsFinished.js +1 -1
  39. package/cjs/overlays/overlay/hooks/useAnimationsFinished.js.map +1 -1
  40. package/cjs/overlays/overlay/hooks/useOpenChangeAnimationComplete.js +2 -2
  41. package/cjs/overlays/overlay/hooks/useOpenChangeAnimationComplete.js.map +1 -1
  42. package/cjs/popover/Popover.js +1 -1
  43. package/cjs/popover/Popover.js.map +1 -1
  44. package/cjs/progress-bar/ProgressBar.js +9 -6
  45. package/cjs/progress-bar/ProgressBar.js.map +1 -1
  46. package/cjs/table/AnimateHeight.js +12 -13
  47. package/cjs/table/AnimateHeight.js.map +1 -1
  48. package/cjs/tabs/parts/tablist/useScrollButtons.d.ts +1 -1
  49. package/cjs/tabs/parts/tablist/useScrollButtons.js +4 -4
  50. package/cjs/tabs/parts/tablist/useScrollButtons.js.map +1 -1
  51. package/cjs/util/TextareaAutoSize.js +3 -10
  52. package/cjs/util/TextareaAutoSize.js.map +1 -1
  53. package/cjs/util/create-context.d.ts +0 -1
  54. package/cjs/util/create-context.js.map +1 -1
  55. package/cjs/util/debounce.d.ts +1 -1
  56. package/cjs/util/debounce.js +5 -8
  57. package/cjs/util/debounce.js.map +1 -1
  58. package/cjs/util/detectBrowser.d.ts +2 -0
  59. package/cjs/util/detectBrowser.js +7 -0
  60. package/cjs/util/detectBrowser.js.map +1 -0
  61. package/cjs/util/focus-boundary/FocusBoundary.d.ts +44 -0
  62. package/cjs/util/focus-boundary/FocusBoundary.js +365 -0
  63. package/cjs/util/focus-boundary/FocusBoundary.js.map +1 -0
  64. package/cjs/util/focus-guards/FocusGuards.d.ts +8 -0
  65. package/cjs/util/focus-guards/FocusGuards.js +36 -0
  66. package/cjs/util/focus-guards/FocusGuards.js.map +1 -0
  67. package/cjs/util/hooks/descendants/useDescendant.js +3 -0
  68. package/cjs/util/hooks/descendants/useDescendant.js.map +1 -1
  69. package/cjs/util/hooks/useEventCallback.js.map +1 -0
  70. package/cjs/{overlays/overlay → util}/hooks/useLatestRef.js +3 -2
  71. package/cjs/util/hooks/useLatestRef.js.map +1 -0
  72. package/cjs/util/hooks/useRefWithInit.js.map +1 -0
  73. package/cjs/util/hooks/useTimeout.d.ts +16 -0
  74. package/cjs/util/hooks/useTimeout.js +49 -0
  75. package/cjs/util/hooks/useTimeout.js.map +1 -0
  76. package/cjs/util/link-anchor/LinkAnchor.js +6 -7
  77. package/cjs/util/link-anchor/LinkAnchor.js.map +1 -1
  78. package/cjs/util/owner.d.ts +29 -0
  79. package/cjs/util/owner.js +38 -0
  80. package/cjs/util/owner.js.map +1 -0
  81. package/esm/copybutton/CopyButton.js +5 -10
  82. package/esm/copybutton/CopyButton.js.map +1 -1
  83. package/esm/form/combobox/Combobox.js +1 -3
  84. package/esm/form/combobox/Combobox.js.map +1 -1
  85. package/esm/form/combobox/ComboboxWrapper.d.ts +1 -2
  86. package/esm/form/combobox/ComboboxWrapper.js +1 -2
  87. package/esm/form/combobox/ComboboxWrapper.js.map +1 -1
  88. package/esm/form/combobox/FilteredOptions/FilteredOptions.js +29 -20
  89. package/esm/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
  90. package/esm/form/combobox/FilteredOptions/FilteredOptionsItem.js +4 -0
  91. package/esm/form/combobox/FilteredOptions/FilteredOptionsItem.js.map +1 -1
  92. package/esm/form/combobox/FilteredOptions/useVirtualFocus.js.map +1 -1
  93. package/esm/form/combobox/Input/Input.context.d.ts +2 -0
  94. package/esm/form/combobox/Input/Input.context.js +4 -1
  95. package/esm/form/combobox/Input/Input.context.js.map +1 -1
  96. package/esm/form/combobox/Input/InputController.js +2 -2
  97. package/esm/form/combobox/Input/InputController.js.map +1 -1
  98. package/esm/form/switch/Switch.js +3 -3
  99. package/esm/form/switch/Switch.js.map +1 -1
  100. package/esm/help-text/HelpText.js +3 -3
  101. package/esm/help-text/HelpText.js.map +1 -1
  102. package/esm/help-text/HelpTextIcon.d.ts +1 -2
  103. package/esm/help-text/HelpTextIcon.js +3 -7
  104. package/esm/help-text/HelpTextIcon.js.map +1 -1
  105. package/esm/layout/page/parts/PageBlock.d.ts +9 -6
  106. package/esm/layout/page/parts/PageBlock.js.map +1 -1
  107. package/esm/modal/ModalUtils.js +6 -4
  108. package/esm/modal/ModalUtils.js.map +1 -1
  109. package/esm/overlays/dismissablelayer/DismissableLayer.js +9 -19
  110. package/esm/overlays/dismissablelayer/DismissableLayer.js.map +1 -1
  111. package/esm/overlays/dismissablelayer/util/usePointerDownOutside.js +5 -4
  112. package/esm/overlays/dismissablelayer/util/usePointerDownOutside.js.map +1 -1
  113. package/esm/overlays/floating-menu/Menu.d.ts +4 -4
  114. package/esm/overlays/floating-menu/Menu.js +7 -4
  115. package/esm/overlays/floating-menu/Menu.js.map +1 -1
  116. package/esm/overlays/floating-menu/parts/RovingFocus.js +3 -3
  117. package/esm/overlays/floating-menu/parts/RovingFocus.js.map +1 -1
  118. package/esm/overlays/overlay/hooks/useAnimationsFinished.js +1 -1
  119. package/esm/overlays/overlay/hooks/useAnimationsFinished.js.map +1 -1
  120. package/esm/overlays/overlay/hooks/useOpenChangeAnimationComplete.js +2 -2
  121. package/esm/overlays/overlay/hooks/useOpenChangeAnimationComplete.js.map +1 -1
  122. package/esm/popover/Popover.js +1 -1
  123. package/esm/popover/Popover.js.map +1 -1
  124. package/esm/progress-bar/ProgressBar.js +10 -7
  125. package/esm/progress-bar/ProgressBar.js.map +1 -1
  126. package/esm/table/AnimateHeight.js +12 -13
  127. package/esm/table/AnimateHeight.js.map +1 -1
  128. package/esm/tabs/parts/tablist/useScrollButtons.d.ts +1 -1
  129. package/esm/tabs/parts/tablist/useScrollButtons.js +4 -4
  130. package/esm/tabs/parts/tablist/useScrollButtons.js.map +1 -1
  131. package/esm/util/TextareaAutoSize.js +1 -8
  132. package/esm/util/TextareaAutoSize.js.map +1 -1
  133. package/esm/util/create-context.d.ts +0 -1
  134. package/esm/util/create-context.js.map +1 -1
  135. package/esm/util/debounce.d.ts +1 -1
  136. package/esm/util/debounce.js +5 -8
  137. package/esm/util/debounce.js.map +1 -1
  138. package/esm/util/detectBrowser.d.ts +2 -0
  139. package/esm/util/detectBrowser.js +4 -0
  140. package/esm/util/detectBrowser.js.map +1 -0
  141. package/esm/util/focus-boundary/FocusBoundary.d.ts +44 -0
  142. package/esm/util/focus-boundary/FocusBoundary.js +329 -0
  143. package/esm/util/focus-boundary/FocusBoundary.js.map +1 -0
  144. package/esm/util/focus-guards/FocusGuards.d.ts +8 -0
  145. package/esm/util/focus-guards/FocusGuards.js +31 -0
  146. package/esm/util/focus-guards/FocusGuards.js.map +1 -0
  147. package/esm/util/hooks/descendants/useDescendant.js +3 -0
  148. package/esm/util/hooks/descendants/useDescendant.js.map +1 -1
  149. package/esm/util/hooks/useEventCallback.js.map +1 -0
  150. package/esm/{overlays/overlay → util}/hooks/useLatestRef.js +2 -1
  151. package/esm/util/hooks/useLatestRef.js.map +1 -0
  152. package/esm/util/hooks/useRefWithInit.js.map +1 -0
  153. package/esm/util/hooks/useTimeout.d.ts +16 -0
  154. package/esm/util/hooks/useTimeout.js +45 -0
  155. package/esm/util/hooks/useTimeout.js.map +1 -0
  156. package/esm/util/link-anchor/LinkAnchor.js +6 -7
  157. package/esm/util/link-anchor/LinkAnchor.js.map +1 -1
  158. package/esm/util/owner.d.ts +29 -0
  159. package/esm/util/owner.js +35 -0
  160. package/esm/util/owner.js.map +1 -0
  161. package/package.json +8 -8
  162. package/src/copybutton/CopyButton.tsx +5 -17
  163. package/src/form/combobox/Combobox.tsx +0 -4
  164. package/src/form/combobox/ComboboxWrapper.tsx +0 -3
  165. package/src/form/combobox/FilteredOptions/FilteredOptions.tsx +65 -45
  166. package/src/form/combobox/FilteredOptions/FilteredOptionsItem.tsx +4 -0
  167. package/src/form/combobox/FilteredOptions/useVirtualFocus.ts +1 -0
  168. package/src/form/combobox/Input/Input.context.tsx +5 -0
  169. package/src/form/combobox/Input/InputController.tsx +2 -1
  170. package/src/form/file-upload/parts/item/utils/format-file-size.test.ts +2 -2
  171. package/src/form/switch/Switch.tsx +4 -4
  172. package/src/help-text/HelpText.tsx +3 -2
  173. package/src/help-text/HelpTextIcon.tsx +2 -12
  174. package/src/layout/page/parts/PageBlock.tsx +9 -6
  175. package/src/modal/ModalUtils.ts +7 -4
  176. package/src/overlays/dismissablelayer/DismissableLayer.tsx +9 -18
  177. package/src/overlays/dismissablelayer/util/usePointerDownOutside.ts +5 -4
  178. package/src/overlays/floating-menu/Menu.tsx +13 -9
  179. package/src/overlays/floating-menu/parts/RovingFocus.tsx +3 -3
  180. package/src/overlays/overlay/hooks/useAnimationsFinished.ts +1 -1
  181. package/src/overlays/overlay/hooks/useOpenChangeAnimationComplete.ts +2 -2
  182. package/src/popover/Popover.tsx +1 -1
  183. package/src/progress-bar/ProgressBar.tsx +12 -10
  184. package/src/table/AnimateHeight.tsx +12 -15
  185. package/src/tabs/parts/tablist/useScrollButtons.ts +4 -3
  186. package/src/util/TextareaAutoSize.tsx +1 -9
  187. package/src/util/create-context.tsx +0 -1
  188. package/src/util/debounce.ts +7 -8
  189. package/src/util/detectBrowser.ts +5 -0
  190. package/src/util/focus-boundary/FocusBoundary.tsx +453 -0
  191. package/src/util/focus-guards/FocusGuards.tsx +56 -0
  192. package/src/util/hooks/descendants/useDescendant.tsx +3 -0
  193. package/src/{overlays/overlay → util}/hooks/useLatestRef.ts +2 -1
  194. package/src/util/hooks/useTimeout.ts +54 -0
  195. package/src/util/link-anchor/LinkAnchor.tsx +7 -6
  196. package/src/util/owner.ts +35 -0
  197. package/cjs/overlays/floating-menu/parts/FocusScope.d.ts +0 -22
  198. package/cjs/overlays/floating-menu/parts/FocusScope.js +0 -98
  199. package/cjs/overlays/floating-menu/parts/FocusScope.js.map +0 -1
  200. package/cjs/overlays/overlay/hooks/useEventCallback.js.map +0 -1
  201. package/cjs/overlays/overlay/hooks/useLatestRef.js.map +0 -1
  202. package/cjs/overlays/overlay/hooks/useRefWithInit.js.map +0 -1
  203. package/esm/overlays/floating-menu/parts/FocusScope.d.ts +0 -22
  204. package/esm/overlays/floating-menu/parts/FocusScope.js +0 -62
  205. package/esm/overlays/floating-menu/parts/FocusScope.js.map +0 -1
  206. package/esm/overlays/overlay/hooks/useEventCallback.js.map +0 -1
  207. package/esm/overlays/overlay/hooks/useLatestRef.js.map +0 -1
  208. package/esm/overlays/overlay/hooks/useRefWithInit.js.map +0 -1
  209. package/src/overlays/floating-menu/parts/FocusScope.tsx +0 -83
  210. /package/cjs/{overlays/overlay → util}/hooks/useEventCallback.d.ts +0 -0
  211. /package/cjs/{overlays/overlay → util}/hooks/useEventCallback.js +0 -0
  212. /package/cjs/{overlays/overlay → util}/hooks/useLatestRef.d.ts +0 -0
  213. /package/cjs/{overlays/overlay → util}/hooks/useRefWithInit.d.ts +0 -0
  214. /package/cjs/{overlays/overlay → util}/hooks/useRefWithInit.js +0 -0
  215. /package/esm/{overlays/overlay → util}/hooks/useEventCallback.d.ts +0 -0
  216. /package/esm/{overlays/overlay → util}/hooks/useEventCallback.js +0 -0
  217. /package/esm/{overlays/overlay → util}/hooks/useLatestRef.d.ts +0 -0
  218. /package/esm/{overlays/overlay → util}/hooks/useRefWithInit.d.ts +0 -0
  219. /package/esm/{overlays/overlay → util}/hooks/useRefWithInit.js +0 -0
  220. /package/src/{overlays/overlay → util}/hooks/useEventCallback.ts +0 -0
  221. /package/src/{overlays/overlay → util}/hooks/useRefWithInit.ts +0 -0
@@ -0,0 +1,453 @@
1
+ import React, {
2
+ forwardRef,
3
+ useCallback,
4
+ useEffect,
5
+ useRef,
6
+ useState,
7
+ } from "react";
8
+ import { Slot } from "../../slot/Slot";
9
+ import { useMergeRefs } from "../../util/hooks";
10
+ import { useEventCallback } from "../hooks/useEventCallback";
11
+
12
+ const AUTOFOCUS_ON_MOUNT = "focusBoundary.autoFocusOnMount";
13
+ const AUTOFOCUS_ON_UNMOUNT = "focusBoundary.autoFocusOnUnmount";
14
+ const EVENT_OPTIONS = { bubbles: false, cancelable: true };
15
+
16
+ /* TODO: Regular story */
17
+ /**
18
+ * TODO:
19
+ * - owner(container) for corrent document
20
+ */
21
+
22
+ /* -------------------------------------------------------------------------- */
23
+ /* FocusBoundary */
24
+ /* -------------------------------------------------------------------------- */
25
+ interface FocusBoundaryProps extends React.HTMLAttributes<HTMLDivElement> {
26
+ /**
27
+ * FocusBoundary expects a single child element since its a slotted component.
28
+ */
29
+ children: React.ReactElement;
30
+ /**
31
+ * When `true`, tabbing from last item will focus first tabbable
32
+ * and shift+tab from first item will focus last tabbable element.
33
+ * This does not "trap" focus inside the boundary, it only loops it when
34
+ * tabbing. If focus is moved outside the boundary programmatically or by
35
+ * pointer, it will not be moved back.
36
+ *
37
+ * - Links (`<a>` elements), are not considered tabbable for the purpose of looping.
38
+ * - Hidden inputs (i.e. `<input type="hidden">`) are not considered tabbable.
39
+ * - Elements that are `display: none` or `visibility: hidden` are not considered tabbable.
40
+ * - Elements with `tabIndex < 0` are not considered tabbable.
41
+ * @defaultValue false
42
+ */
43
+ loop?: boolean;
44
+ /**
45
+ * When `true`, focus cannot escape the focus boundary via keyboard,
46
+ * pointer, or a programmatic focus.
47
+ * @defaultValue false
48
+ */
49
+ trapped?: boolean;
50
+ /**
51
+ * Event handler called when auto-focusing on mount.
52
+ * Can be prevented.
53
+ */
54
+ onMountAutoFocus?: (event: Event) => void;
55
+ /**
56
+ * Event handler called when auto-focusing on unmount.
57
+ * Can be prevented.
58
+ */
59
+ onUnmountAutoFocus?: (event: Event) => void;
60
+ }
61
+
62
+ const FocusBoundary = forwardRef<HTMLDivElement, FocusBoundaryProps>(
63
+ (
64
+ {
65
+ loop = false,
66
+ trapped = false,
67
+ onMountAutoFocus: onMountAutoFocusProp,
68
+ onUnmountAutoFocus: onUnmountAutoFocusProp,
69
+ ...restProps
70
+ }: FocusBoundaryProps,
71
+ forwardedRef,
72
+ ) => {
73
+ const onMountAutoFocus = useEventCallback(onMountAutoFocusProp);
74
+ const onUnmountAutoFocus = useEventCallback(onUnmountAutoFocusProp);
75
+
76
+ const lastFocusedElementRef = useRef<HTMLElement | null>(null);
77
+ const [container, setContainer] = useState<HTMLElement | null>(null);
78
+ const mergedRefs = useMergeRefs(forwardedRef, setContainer);
79
+
80
+ const focusBoundary = useRef<FocusBoundaryAPI>({
81
+ paused: false,
82
+ pause() {
83
+ this.paused = true;
84
+ },
85
+ resume() {
86
+ this.paused = false;
87
+ },
88
+ }).current;
89
+
90
+ /* Handles trapped state */
91
+ useEffect(() => {
92
+ if (!trapped || !container) {
93
+ return;
94
+ }
95
+
96
+ function handleFocusIn(event: FocusEvent) {
97
+ if (focusBoundary.paused || container === null) {
98
+ return;
99
+ }
100
+
101
+ const target = event.target as HTMLElement | null;
102
+ if (container.contains(target)) {
103
+ lastFocusedElementRef.current = target;
104
+ } else {
105
+ focus(lastFocusedElementRef.current, { select: true });
106
+ }
107
+ }
108
+
109
+ function handleFocusOut(event: FocusEvent) {
110
+ if (focusBoundary.paused || container === null) {
111
+ return;
112
+ }
113
+
114
+ const relatedTarget = event.relatedTarget as HTMLElement | null;
115
+
116
+ /*
117
+ * `focusout` event with a `null` `relatedTarget` will happen in a few known cases:
118
+ * 1. When the user switches app/tabs/windows/the browser itself loses focus.
119
+ * 2. In Google Chrome, when the focused element is removed from the DOM.
120
+ * 3. When clicking on an element that cannot receive focus.
121
+ *
122
+ * We let the browser do its thing here because:
123
+ * 1. The browser already keeps a memory of what's focused for when the page gets refocused.
124
+ * 2. In Google Chrome, if we try to focus the deleted focused element (as per below), it
125
+ * throws the CPU to 100%, so we avoid doing anything for this reason here too.
126
+ */
127
+ if (relatedTarget === null) {
128
+ return;
129
+ }
130
+
131
+ /*
132
+ * If the focus has moved to an element outside the container, we move focus to the last valid focused element inside.
133
+ * This makes sure to "trap" focus inside the container.
134
+ * We handle focus on focusout instead of focusin to avoid elements recieving focusin events
135
+ * when they are not supposed to (like when clicking on elements outside the container
136
+ */
137
+ if (!container.contains(relatedTarget)) {
138
+ focus(lastFocusedElementRef.current, { select: true });
139
+ }
140
+ }
141
+
142
+ /**
143
+ * When the currently focused element is removed from the DOM, browsers move focus
144
+ * to the document.body. In this case, we move focus to the container
145
+ * to keep focus trapped correctly instead.
146
+ */
147
+ const handleMutations = (mutations: MutationRecord[]) => {
148
+ if (document.activeElement !== document.body) {
149
+ return;
150
+ }
151
+
152
+ if (mutations.some((mutation) => mutation.removedNodes.length > 0)) {
153
+ focus(container);
154
+ }
155
+ };
156
+
157
+ document.addEventListener("focusin", handleFocusIn);
158
+ document.addEventListener("focusout", handleFocusOut);
159
+ const observer = new MutationObserver(handleMutations);
160
+ observer.observe(container, { childList: true, subtree: true });
161
+
162
+ return () => {
163
+ document.removeEventListener("focusin", handleFocusIn);
164
+ document.removeEventListener("focusout", handleFocusOut);
165
+ observer.disconnect();
166
+ };
167
+ }, [trapped, container, focusBoundary.paused]);
168
+
169
+ /* Handles autofocus on mount and unmount */
170
+ useEffect(() => {
171
+ if (!container) {
172
+ return;
173
+ }
174
+
175
+ focusBoundarysStack.add(focusBoundary);
176
+ const initialFocusedElement =
177
+ document.activeElement as HTMLElement | null;
178
+ const containsActiveElement =
179
+ initialFocusedElement && container.contains(initialFocusedElement);
180
+
181
+ /*
182
+ * We only autofocus on mount if container does not contain active element.
183
+ * If container has an element with `autoFocus` attribute, browser will
184
+ * have already moved focus there before this effect runs.
185
+ */
186
+ if (!containsActiveElement) {
187
+ const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
188
+ container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
189
+ container.dispatchEvent(mountEvent);
190
+
191
+ /* If consumer does not manually prevent event and handle focus themselves */
192
+ if (!mountEvent.defaultPrevented) {
193
+ /**
194
+ * Attempts focusing the first element in a list of candidates.
195
+ * Stops when focus has actually moved.
196
+ */
197
+ const candidates = removeLinks(getTabbableCandidates(container));
198
+ const previouslyFocusedElement = document.activeElement;
199
+ for (const candidate of candidates) {
200
+ focus(candidate, { select: true });
201
+ if (document.activeElement !== previouslyFocusedElement) {
202
+ break;
203
+ }
204
+ }
205
+
206
+ /* focusFirst might not find any candidates, so we fall back to focusing container */
207
+ if (document.activeElement === initialFocusedElement) {
208
+ focus(container);
209
+ }
210
+ }
211
+ }
212
+
213
+ return () => {
214
+ container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
215
+
216
+ /**
217
+ * https://github.com/facebook/react/issues/17894
218
+ * We delay to next tick to avoid issues with React's event system
219
+ * where calling `focus` inside a effect cleanup causes React to not call onFocus handlers.
220
+ */
221
+ setTimeout(() => {
222
+ const unmountEvent = new CustomEvent(
223
+ AUTOFOCUS_ON_UNMOUNT,
224
+ EVENT_OPTIONS,
225
+ );
226
+ container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
227
+ container.dispatchEvent(unmountEvent);
228
+
229
+ /* If consumer does not manually prevent event and handle focus themselves */
230
+ if (!unmountEvent.defaultPrevented) {
231
+ /* To avoid CPU-spikes on Chrome, we make sure element is still connected to the DOM. */
232
+ focus(
233
+ initialFocusedElement?.isConnected
234
+ ? initialFocusedElement
235
+ : document.body,
236
+ {
237
+ select: true,
238
+ },
239
+ );
240
+ }
241
+ /* Since this is inside a cleanup, we need to instantly remove the listener ourselves */
242
+ container.removeEventListener(
243
+ AUTOFOCUS_ON_UNMOUNT,
244
+ onUnmountAutoFocus,
245
+ );
246
+
247
+ focusBoundarysStack.remove(focusBoundary);
248
+ }, 0);
249
+ };
250
+ }, [container, onMountAutoFocus, onUnmountAutoFocus, focusBoundary]);
251
+
252
+ /* Takes care of looping focus */
253
+ const handleKeyDown = useCallback(
254
+ (event: React.KeyboardEvent) => {
255
+ if ((!loop && !trapped) || focusBoundary.paused) {
256
+ return;
257
+ }
258
+
259
+ const isTabKey =
260
+ event.key === "Tab" &&
261
+ !event.altKey &&
262
+ !event.ctrlKey &&
263
+ !event.metaKey;
264
+
265
+ const focusedElement = document.activeElement;
266
+
267
+ if (isTabKey && focusedElement) {
268
+ const containerTarget = event.currentTarget as HTMLElement;
269
+ const [first, last] = getTabbableEdges(containerTarget);
270
+
271
+ /* We can only wrap focus if we have tabbable edges */
272
+ if (!(first && last)) {
273
+ /*
274
+ * No need to do anything if active element is the expected focus-target
275
+ * Case: No tabbable elements, focus should stay on container. If we don't preventDefault, the container will lose focus
276
+ * and potentially lose controll of focus to browser (like focusing address bar).
277
+ */
278
+ if (focusedElement === containerTarget) {
279
+ event.preventDefault();
280
+ }
281
+ return;
282
+ }
283
+
284
+ /**
285
+ * Since we are either trapped + looping, or one of them we will do nothing when trapped and focus first element when looping.
286
+ */
287
+ if (!event.shiftKey && focusedElement === last) {
288
+ event.preventDefault();
289
+ if (loop) {
290
+ focus(first, { select: true });
291
+ }
292
+ } else if (event.shiftKey && focusedElement === first) {
293
+ event.preventDefault();
294
+ if (loop) {
295
+ focus(last, { select: true });
296
+ }
297
+ }
298
+ }
299
+ },
300
+ [loop, trapped, focusBoundary.paused],
301
+ );
302
+
303
+ return (
304
+ <Slot
305
+ tabIndex={-1}
306
+ {...restProps}
307
+ ref={mergedRefs}
308
+ onKeyDown={handleKeyDown}
309
+ />
310
+ );
311
+ },
312
+ );
313
+
314
+ /* ---------------------------- FocusBoundary utils ---------------------------- */
315
+ /**
316
+ * Returns the first and last tabbable elements inside a container as a tuple.
317
+ */
318
+ function getTabbableEdges(container: HTMLElement) {
319
+ const candidates = getTabbableCandidates(container);
320
+ return [
321
+ findFirstVisible(candidates, container),
322
+ findFirstVisible(candidates.reverse(), container),
323
+ ] as const;
324
+ }
325
+
326
+ /**
327
+ * Returns a list of potential tabbable candidates.
328
+ * We do not take into account tabindex values.
329
+ *
330
+ * See: https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker
331
+ * Credit: https://github.com/discord/focus-layers/blob/master/src/util/wrapFocus.tsx#L1
332
+ */
333
+ function getTabbableCandidates(container: HTMLElement) {
334
+ const nodes: HTMLElement[] = [];
335
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
336
+ acceptNode: (node: any) => {
337
+ const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
338
+ if (node.disabled || node.hidden || isHiddenInput) {
339
+ return NodeFilter.FILTER_SKIP;
340
+ }
341
+
342
+ /**
343
+ * `.tabIndex` is not the same as the `tabindex` attribute. It works on the
344
+ * runtime's understanding of tabbability, so this automatically accounts
345
+ * for any kind of element that could be tabbed to.
346
+ */
347
+ return node.tabIndex >= 0
348
+ ? NodeFilter.FILTER_ACCEPT
349
+ : NodeFilter.FILTER_SKIP;
350
+ },
351
+ });
352
+
353
+ while (walker.nextNode()) {
354
+ nodes.push(walker.currentNode as HTMLElement);
355
+ }
356
+
357
+ return nodes;
358
+ }
359
+
360
+ /**
361
+ * Returns the first visible element in a list.
362
+ * NOTE: Only checks visibility up to the `container`.
363
+ */
364
+ function findFirstVisible(elements: HTMLElement[], container: HTMLElement) {
365
+ for (const element of elements) {
366
+ if (!isHidden(element, { upTo: container })) {
367
+ return element;
368
+ }
369
+ }
370
+ }
371
+
372
+ function isHidden(node: HTMLElement, { upTo }: { upTo?: HTMLElement }) {
373
+ if (getComputedStyle(node).visibility === "hidden") {
374
+ return true;
375
+ }
376
+
377
+ while (node) {
378
+ /* we stop at `upTo` */
379
+ if (upTo !== undefined && node === upTo) {
380
+ return false;
381
+ }
382
+ if (getComputedStyle(node).display === "none") {
383
+ return true;
384
+ }
385
+ node = node.parentElement as HTMLElement;
386
+ }
387
+ return false;
388
+ }
389
+
390
+ function focus(element?: HTMLElement | null, { select = false } = {}) {
391
+ if (!element?.focus) {
392
+ return;
393
+ }
394
+
395
+ const previouslyFocusedElement = document.activeElement;
396
+ /* Prevent scrolling on focus, to minimize jarring transitions */
397
+ element.focus({ preventScroll: true });
398
+
399
+ if (!select) {
400
+ return;
401
+ }
402
+
403
+ /* By default, inputs that gets focus should select its contents */
404
+ if (
405
+ element !== previouslyFocusedElement &&
406
+ element instanceof HTMLInputElement &&
407
+ "select" in element
408
+ )
409
+ element.select();
410
+ }
411
+
412
+ /* ---------------------------- FocusBoundary stack ---------------------------- */
413
+ type FocusBoundaryAPI = { paused: boolean; pause(): void; resume(): void };
414
+ const focusBoundarysStack = createFocusBoundarysStack();
415
+
416
+ function createFocusBoundarysStack() {
417
+ /* A stack of focus-boundaries, with the active one at the top */
418
+ let stack: FocusBoundaryAPI[] = [];
419
+
420
+ return {
421
+ add(focusBoundary: FocusBoundaryAPI) {
422
+ /* Pause the currently active focus-boundary (at the top of the stack) */
423
+ const activeFocusBoundary = stack[0];
424
+ if (focusBoundary !== activeFocusBoundary) {
425
+ activeFocusBoundary?.pause();
426
+ }
427
+ /* remove in case it already exists (because we'll re-add it at the top of the stack) */
428
+ stack = arrayRemove(stack, focusBoundary);
429
+ stack.unshift(focusBoundary);
430
+ },
431
+
432
+ remove(focusBoundary: FocusBoundaryAPI) {
433
+ stack = arrayRemove(stack, focusBoundary);
434
+ stack[0]?.resume();
435
+ },
436
+ };
437
+ }
438
+
439
+ function arrayRemove<T>(array: T[], item: T) {
440
+ const updatedArray = [...array];
441
+ const index = updatedArray.indexOf(item);
442
+ if (index !== -1) {
443
+ updatedArray.splice(index, 1);
444
+ }
445
+ return updatedArray;
446
+ }
447
+
448
+ function removeLinks(items: HTMLElement[]) {
449
+ return items.filter((item) => item.tagName !== "A");
450
+ }
451
+
452
+ export { FocusBoundary };
453
+ export type { FocusBoundaryProps };
@@ -0,0 +1,56 @@
1
+ import React from "react";
2
+ import { useMergeRefs } from "../hooks";
3
+
4
+ const visuallyHidden: React.CSSProperties = {
5
+ clip: "rect(0 0 0 0)",
6
+ overflow: "hidden",
7
+ whiteSpace: "nowrap",
8
+ position: "fixed",
9
+ top: 0,
10
+ left: 0,
11
+ border: 0,
12
+ padding: 0,
13
+ width: 1,
14
+ height: 1,
15
+ margin: -1,
16
+ };
17
+
18
+ type FocusGuardsProps = {
19
+ children: React.ReactNode;
20
+ startRef?: React.RefObject<HTMLSpanElement>;
21
+ endRef?: React.RefObject<HTMLSpanElement>;
22
+ };
23
+
24
+ function FocusGuards({
25
+ children,
26
+ startRef: forwardedStartRef,
27
+ endRef: forwardedEndRef,
28
+ }: FocusGuardsProps) {
29
+ const startRef = React.useRef<HTMLSpanElement | null>(null);
30
+ const endRef = React.useRef<HTMLSpanElement | null>(null);
31
+
32
+ const startRefCombined = useMergeRefs(startRef, forwardedStartRef);
33
+ const endRefCombined = useMergeRefs(endRef, forwardedEndRef);
34
+
35
+ return (
36
+ <React.Fragment>
37
+ <span
38
+ ref={startRefCombined}
39
+ // eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
40
+ tabIndex={0}
41
+ style={visuallyHidden}
42
+ data-aksel-focus-guard=""
43
+ />
44
+ {children}
45
+ <span
46
+ ref={endRefCombined}
47
+ // eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
48
+ tabIndex={0}
49
+ style={visuallyHidden}
50
+ data-aksel-focus-guard=""
51
+ />
52
+ </React.Fragment>
53
+ );
54
+ }
55
+
56
+ export { FocusGuards };
@@ -46,10 +46,13 @@ export function createDescendantContext<
46
46
  useClientLayoutEffect(() => {
47
47
  return () => {
48
48
  if (!ref.current) return;
49
+ // eslint-disable-next-line react-hooks/exhaustive-deps
49
50
  descendants.unregister(ref.current);
50
51
  };
52
+ // eslint-disable-next-line react-hooks/exhaustive-deps
51
53
  }, []);
52
54
 
55
+ // eslint-disable-next-line react-hooks/exhaustive-deps
53
56
  useClientLayoutEffect(() => {
54
57
  if (!ref.current) return;
55
58
  const dataIndex = Number(ref.current.dataset.index);
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
 
3
- import { useClientLayoutEffect } from "../../../util";
3
+ import { useClientLayoutEffect } from "./useClientLayoutEffect";
4
4
  import { useRefWithInit } from "./useRefWithInit";
5
5
 
6
6
  export function useLatestRef<T>(value: T) {
@@ -8,6 +8,7 @@ export function useLatestRef<T>(value: T) {
8
8
 
9
9
  latest.next = value;
10
10
 
11
+ // eslint-disable-next-line react-hooks/exhaustive-deps
11
12
  useClientLayoutEffect(latest.effect);
12
13
 
13
14
  return latest;
@@ -0,0 +1,54 @@
1
+ "use client";
2
+
3
+ import { useEffect } from "react";
4
+ import { useRefWithInit } from "./useRefWithInit";
5
+
6
+ const EMPTY = 0;
7
+
8
+ class Timeout {
9
+ static create() {
10
+ return new Timeout();
11
+ }
12
+
13
+ currentId: number = EMPTY;
14
+
15
+ /**
16
+ * Executes `fn` after `delay`, clearing any previously scheduled call.
17
+ */
18
+ start(delay: number, fn: () => void) {
19
+ this.clear();
20
+ this.currentId = setTimeout(() => {
21
+ this.currentId = EMPTY;
22
+ fn();
23
+ }, delay) as unknown as number; /* Node.js types are enabled in development */
24
+ }
25
+
26
+ isStarted() {
27
+ return this.currentId !== EMPTY;
28
+ }
29
+
30
+ clear = () => {
31
+ if (this.currentId !== EMPTY) {
32
+ clearTimeout(this.currentId);
33
+ this.currentId = EMPTY;
34
+ }
35
+ };
36
+
37
+ disposeEffect = () => {
38
+ return this.clear;
39
+ };
40
+ }
41
+
42
+ /**
43
+ * A `setTimeout` with automatic cleanup and guard.
44
+ */
45
+ function useTimeout() {
46
+ const timeout = useRefWithInit(Timeout.create).current!;
47
+
48
+ // eslint-disable-next-line react-hooks/exhaustive-deps
49
+ useEffect(timeout.disposeEffect, []);
50
+
51
+ return timeout;
52
+ }
53
+
54
+ export { Timeout, useTimeout };
@@ -11,6 +11,7 @@ import { useRenameCSS } from "../../theme/Theme";
11
11
  import { composeEventHandlers } from "../composeEventHandlers";
12
12
  import { createContext } from "../create-context";
13
13
  import { useMergeRefs } from "../hooks/useMergeRefs";
14
+ import { ownerWindow } from "../owner";
14
15
  import { AsChildProps } from "../types";
15
16
 
16
17
  type LinkAnchorOverlayContextProps = {
@@ -47,7 +48,10 @@ const LinkAnchorOverlay = forwardRef<HTMLDivElement, LinkAnchorOverlayProps>(
47
48
  {...restProps}
48
49
  className={cn("navds-link-anchor__overlay", className)}
49
50
  onClick={composeEventHandlers(onClick, (e) => {
50
- if (e.target === anchorRef.current || isTextSelected()) {
51
+ if (
52
+ e.target === anchorRef.current ||
53
+ isTextSelected(anchorRef.current)
54
+ ) {
51
55
  return;
52
56
  }
53
57
 
@@ -142,11 +146,8 @@ const LinkAnchorArrow = forwardRef<SVGSVGElement, LinkAnchorArrowProps>(
142
146
  );
143
147
 
144
148
  /* -------------------------- LinkAnchor Utilities -------------------------- */
145
- function isTextSelected(): boolean {
146
- if (typeof window === "undefined") {
147
- return false;
148
- }
149
- return !!window.getSelection()?.toString();
149
+ function isTextSelected(refElement: HTMLAnchorElement | null): boolean {
150
+ return !!ownerWindow(refElement)?.getSelection()?.toString();
150
151
  }
151
152
 
152
153
  export { LinkAnchor, LinkAnchorArrow, LinkAnchorOverlay };
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Returns the owner document of a given element.
3
+ *
4
+ * Use this when the node might live in a different browsing context than the code
5
+ * invoking the utility (portals, iframes, custom documents).
6
+ *
7
+ * Examples:
8
+ * - Focus guards for portaled menus: pass the menu root so guards are created in the portal document.
9
+ * - Components rendered inside an iframe preview: scope listeners to the iframe-document.
10
+ * - Element opened with `window.open`: scope listeners to the new window-document.
11
+ *
12
+ * Scenarios:
13
+ * - Modal content rendered to parent `document.body` via a portal while running inside an iframe.
14
+ * - Tooltips or popovers that live outside the component't immediate DOM tree.
15
+ *
16
+ * https://github.com/radix-ui/primitives/issues/1676
17
+ * https://github.com/radix-ui/primitives/issues/1721
18
+ * https://github.com/radix-ui/primitives/discussions/1715
19
+ */
20
+ function ownerDocument(node: Element | null) {
21
+ return node?.ownerDocument || globalThis?.document;
22
+ }
23
+
24
+ /**
25
+ * Returns the owner window of a given element.
26
+ *
27
+ * Examples:
28
+ * - Keyboard listeners for portaled overlays.
29
+ * - Resize/scroll observers applied to iframe widgets.
30
+ */
31
+ function ownerWindow(node: Document | Element | null): typeof window {
32
+ return node?.ownerDocument?.defaultView || window;
33
+ }
34
+
35
+ export { ownerDocument, ownerWindow };
@@ -1,22 +0,0 @@
1
- import React from "react";
2
- interface FocusScopeProps extends React.HTMLAttributes<HTMLDivElement> {
3
- /**
4
- * Event handler called on mount, unless the component already has focus. Used for auto-focusing.
5
- * Can be prevented.
6
- */
7
- onMountHandler?: (event: Event) => void;
8
- /**
9
- * Event handler called on unmount. Used for auto-focusing.
10
- * Can be prevented.
11
- */
12
- onUnmountHandler?: (event: Event) => void;
13
- }
14
- /**
15
- * FocusScope manages focus on mount and unmount of container.
16
- * This is used to better handle autofocus of elements when mounted and unmounted.
17
- * Example usage:
18
- * - Focus first item in a list when mounted
19
- * - Focus a button when unmounted
20
- */
21
- declare const FocusScope: React.ForwardRefExoticComponent<FocusScopeProps & React.RefAttributes<HTMLDivElement>>;
22
- export { FocusScope, type FocusScopeProps };