@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.
- package/cjs/copybutton/CopyButton.js +4 -9
- package/cjs/copybutton/CopyButton.js.map +1 -1
- package/cjs/form/combobox/Combobox.js +1 -3
- package/cjs/form/combobox/Combobox.js.map +1 -1
- package/cjs/form/combobox/ComboboxWrapper.d.ts +1 -2
- package/cjs/form/combobox/ComboboxWrapper.js +1 -2
- package/cjs/form/combobox/ComboboxWrapper.js.map +1 -1
- package/cjs/form/combobox/FilteredOptions/FilteredOptions.js +28 -19
- package/cjs/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
- package/cjs/form/combobox/FilteredOptions/FilteredOptionsItem.js +4 -0
- package/cjs/form/combobox/FilteredOptions/FilteredOptionsItem.js.map +1 -1
- package/cjs/form/combobox/FilteredOptions/useVirtualFocus.js.map +1 -1
- package/cjs/form/combobox/Input/Input.context.d.ts +2 -0
- package/cjs/form/combobox/Input/Input.context.js +4 -1
- package/cjs/form/combobox/Input/Input.context.js.map +1 -1
- package/cjs/form/combobox/Input/InputController.js +2 -2
- package/cjs/form/combobox/Input/InputController.js.map +1 -1
- package/cjs/form/switch/Switch.js +3 -3
- package/cjs/form/switch/Switch.js.map +1 -1
- package/cjs/help-text/HelpText.js +3 -3
- package/cjs/help-text/HelpText.js.map +1 -1
- package/cjs/help-text/HelpTextIcon.d.ts +1 -2
- package/cjs/help-text/HelpTextIcon.js +3 -7
- package/cjs/help-text/HelpTextIcon.js.map +1 -1
- package/cjs/layout/page/parts/PageBlock.d.ts +9 -6
- package/cjs/layout/page/parts/PageBlock.js.map +1 -1
- package/cjs/modal/ModalUtils.js +6 -4
- package/cjs/modal/ModalUtils.js.map +1 -1
- package/cjs/overlays/dismissablelayer/DismissableLayer.js +9 -19
- package/cjs/overlays/dismissablelayer/DismissableLayer.js.map +1 -1
- package/cjs/overlays/dismissablelayer/util/usePointerDownOutside.js +5 -4
- package/cjs/overlays/dismissablelayer/util/usePointerDownOutside.js.map +1 -1
- package/cjs/overlays/floating-menu/Menu.d.ts +4 -4
- package/cjs/overlays/floating-menu/Menu.js +7 -4
- package/cjs/overlays/floating-menu/Menu.js.map +1 -1
- package/cjs/overlays/floating-menu/parts/RovingFocus.js +3 -3
- package/cjs/overlays/floating-menu/parts/RovingFocus.js.map +1 -1
- package/cjs/overlays/overlay/hooks/useAnimationsFinished.js +1 -1
- package/cjs/overlays/overlay/hooks/useAnimationsFinished.js.map +1 -1
- package/cjs/overlays/overlay/hooks/useOpenChangeAnimationComplete.js +2 -2
- package/cjs/overlays/overlay/hooks/useOpenChangeAnimationComplete.js.map +1 -1
- package/cjs/popover/Popover.js +1 -1
- package/cjs/popover/Popover.js.map +1 -1
- package/cjs/progress-bar/ProgressBar.js +9 -6
- package/cjs/progress-bar/ProgressBar.js.map +1 -1
- package/cjs/table/AnimateHeight.js +12 -13
- package/cjs/table/AnimateHeight.js.map +1 -1
- package/cjs/tabs/parts/tablist/useScrollButtons.d.ts +1 -1
- package/cjs/tabs/parts/tablist/useScrollButtons.js +4 -4
- package/cjs/tabs/parts/tablist/useScrollButtons.js.map +1 -1
- package/cjs/util/TextareaAutoSize.js +3 -10
- package/cjs/util/TextareaAutoSize.js.map +1 -1
- package/cjs/util/create-context.d.ts +0 -1
- package/cjs/util/create-context.js.map +1 -1
- package/cjs/util/debounce.d.ts +1 -1
- package/cjs/util/debounce.js +5 -8
- package/cjs/util/debounce.js.map +1 -1
- package/cjs/util/detectBrowser.d.ts +2 -0
- package/cjs/util/detectBrowser.js +7 -0
- package/cjs/util/detectBrowser.js.map +1 -0
- package/cjs/util/focus-boundary/FocusBoundary.d.ts +44 -0
- package/cjs/util/focus-boundary/FocusBoundary.js +365 -0
- package/cjs/util/focus-boundary/FocusBoundary.js.map +1 -0
- package/cjs/util/focus-guards/FocusGuards.d.ts +8 -0
- package/cjs/util/focus-guards/FocusGuards.js +36 -0
- package/cjs/util/focus-guards/FocusGuards.js.map +1 -0
- package/cjs/util/hooks/descendants/useDescendant.js +3 -0
- package/cjs/util/hooks/descendants/useDescendant.js.map +1 -1
- package/cjs/util/hooks/useEventCallback.js.map +1 -0
- package/cjs/{overlays/overlay → util}/hooks/useLatestRef.js +3 -2
- package/cjs/util/hooks/useLatestRef.js.map +1 -0
- package/cjs/util/hooks/useRefWithInit.js.map +1 -0
- package/cjs/util/hooks/useTimeout.d.ts +16 -0
- package/cjs/util/hooks/useTimeout.js +49 -0
- package/cjs/util/hooks/useTimeout.js.map +1 -0
- package/cjs/util/link-anchor/LinkAnchor.js +6 -7
- package/cjs/util/link-anchor/LinkAnchor.js.map +1 -1
- package/cjs/util/owner.d.ts +29 -0
- package/cjs/util/owner.js +38 -0
- package/cjs/util/owner.js.map +1 -0
- package/esm/copybutton/CopyButton.js +5 -10
- package/esm/copybutton/CopyButton.js.map +1 -1
- package/esm/form/combobox/Combobox.js +1 -3
- package/esm/form/combobox/Combobox.js.map +1 -1
- package/esm/form/combobox/ComboboxWrapper.d.ts +1 -2
- package/esm/form/combobox/ComboboxWrapper.js +1 -2
- package/esm/form/combobox/ComboboxWrapper.js.map +1 -1
- package/esm/form/combobox/FilteredOptions/FilteredOptions.js +29 -20
- package/esm/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
- package/esm/form/combobox/FilteredOptions/FilteredOptionsItem.js +4 -0
- package/esm/form/combobox/FilteredOptions/FilteredOptionsItem.js.map +1 -1
- package/esm/form/combobox/FilteredOptions/useVirtualFocus.js.map +1 -1
- package/esm/form/combobox/Input/Input.context.d.ts +2 -0
- package/esm/form/combobox/Input/Input.context.js +4 -1
- package/esm/form/combobox/Input/Input.context.js.map +1 -1
- package/esm/form/combobox/Input/InputController.js +2 -2
- package/esm/form/combobox/Input/InputController.js.map +1 -1
- package/esm/form/switch/Switch.js +3 -3
- package/esm/form/switch/Switch.js.map +1 -1
- package/esm/help-text/HelpText.js +3 -3
- package/esm/help-text/HelpText.js.map +1 -1
- package/esm/help-text/HelpTextIcon.d.ts +1 -2
- package/esm/help-text/HelpTextIcon.js +3 -7
- package/esm/help-text/HelpTextIcon.js.map +1 -1
- package/esm/layout/page/parts/PageBlock.d.ts +9 -6
- package/esm/layout/page/parts/PageBlock.js.map +1 -1
- package/esm/modal/ModalUtils.js +6 -4
- package/esm/modal/ModalUtils.js.map +1 -1
- package/esm/overlays/dismissablelayer/DismissableLayer.js +9 -19
- package/esm/overlays/dismissablelayer/DismissableLayer.js.map +1 -1
- package/esm/overlays/dismissablelayer/util/usePointerDownOutside.js +5 -4
- package/esm/overlays/dismissablelayer/util/usePointerDownOutside.js.map +1 -1
- package/esm/overlays/floating-menu/Menu.d.ts +4 -4
- package/esm/overlays/floating-menu/Menu.js +7 -4
- package/esm/overlays/floating-menu/Menu.js.map +1 -1
- package/esm/overlays/floating-menu/parts/RovingFocus.js +3 -3
- package/esm/overlays/floating-menu/parts/RovingFocus.js.map +1 -1
- package/esm/overlays/overlay/hooks/useAnimationsFinished.js +1 -1
- package/esm/overlays/overlay/hooks/useAnimationsFinished.js.map +1 -1
- package/esm/overlays/overlay/hooks/useOpenChangeAnimationComplete.js +2 -2
- package/esm/overlays/overlay/hooks/useOpenChangeAnimationComplete.js.map +1 -1
- package/esm/popover/Popover.js +1 -1
- package/esm/popover/Popover.js.map +1 -1
- package/esm/progress-bar/ProgressBar.js +10 -7
- package/esm/progress-bar/ProgressBar.js.map +1 -1
- package/esm/table/AnimateHeight.js +12 -13
- package/esm/table/AnimateHeight.js.map +1 -1
- package/esm/tabs/parts/tablist/useScrollButtons.d.ts +1 -1
- package/esm/tabs/parts/tablist/useScrollButtons.js +4 -4
- package/esm/tabs/parts/tablist/useScrollButtons.js.map +1 -1
- package/esm/util/TextareaAutoSize.js +1 -8
- package/esm/util/TextareaAutoSize.js.map +1 -1
- package/esm/util/create-context.d.ts +0 -1
- package/esm/util/create-context.js.map +1 -1
- package/esm/util/debounce.d.ts +1 -1
- package/esm/util/debounce.js +5 -8
- package/esm/util/debounce.js.map +1 -1
- package/esm/util/detectBrowser.d.ts +2 -0
- package/esm/util/detectBrowser.js +4 -0
- package/esm/util/detectBrowser.js.map +1 -0
- package/esm/util/focus-boundary/FocusBoundary.d.ts +44 -0
- package/esm/util/focus-boundary/FocusBoundary.js +329 -0
- package/esm/util/focus-boundary/FocusBoundary.js.map +1 -0
- package/esm/util/focus-guards/FocusGuards.d.ts +8 -0
- package/esm/util/focus-guards/FocusGuards.js +31 -0
- package/esm/util/focus-guards/FocusGuards.js.map +1 -0
- package/esm/util/hooks/descendants/useDescendant.js +3 -0
- package/esm/util/hooks/descendants/useDescendant.js.map +1 -1
- package/esm/util/hooks/useEventCallback.js.map +1 -0
- package/esm/{overlays/overlay → util}/hooks/useLatestRef.js +2 -1
- package/esm/util/hooks/useLatestRef.js.map +1 -0
- package/esm/util/hooks/useRefWithInit.js.map +1 -0
- package/esm/util/hooks/useTimeout.d.ts +16 -0
- package/esm/util/hooks/useTimeout.js +45 -0
- package/esm/util/hooks/useTimeout.js.map +1 -0
- package/esm/util/link-anchor/LinkAnchor.js +6 -7
- package/esm/util/link-anchor/LinkAnchor.js.map +1 -1
- package/esm/util/owner.d.ts +29 -0
- package/esm/util/owner.js +35 -0
- package/esm/util/owner.js.map +1 -0
- package/package.json +8 -8
- package/src/copybutton/CopyButton.tsx +5 -17
- package/src/form/combobox/Combobox.tsx +0 -4
- package/src/form/combobox/ComboboxWrapper.tsx +0 -3
- package/src/form/combobox/FilteredOptions/FilteredOptions.tsx +65 -45
- package/src/form/combobox/FilteredOptions/FilteredOptionsItem.tsx +4 -0
- package/src/form/combobox/FilteredOptions/useVirtualFocus.ts +1 -0
- package/src/form/combobox/Input/Input.context.tsx +5 -0
- package/src/form/combobox/Input/InputController.tsx +2 -1
- package/src/form/file-upload/parts/item/utils/format-file-size.test.ts +2 -2
- package/src/form/switch/Switch.tsx +4 -4
- package/src/help-text/HelpText.tsx +3 -2
- package/src/help-text/HelpTextIcon.tsx +2 -12
- package/src/layout/page/parts/PageBlock.tsx +9 -6
- package/src/modal/ModalUtils.ts +7 -4
- package/src/overlays/dismissablelayer/DismissableLayer.tsx +9 -18
- package/src/overlays/dismissablelayer/util/usePointerDownOutside.ts +5 -4
- package/src/overlays/floating-menu/Menu.tsx +13 -9
- package/src/overlays/floating-menu/parts/RovingFocus.tsx +3 -3
- package/src/overlays/overlay/hooks/useAnimationsFinished.ts +1 -1
- package/src/overlays/overlay/hooks/useOpenChangeAnimationComplete.ts +2 -2
- package/src/popover/Popover.tsx +1 -1
- package/src/progress-bar/ProgressBar.tsx +12 -10
- package/src/table/AnimateHeight.tsx +12 -15
- package/src/tabs/parts/tablist/useScrollButtons.ts +4 -3
- package/src/util/TextareaAutoSize.tsx +1 -9
- package/src/util/create-context.tsx +0 -1
- package/src/util/debounce.ts +7 -8
- package/src/util/detectBrowser.ts +5 -0
- package/src/util/focus-boundary/FocusBoundary.tsx +453 -0
- package/src/util/focus-guards/FocusGuards.tsx +56 -0
- package/src/util/hooks/descendants/useDescendant.tsx +3 -0
- package/src/{overlays/overlay → util}/hooks/useLatestRef.ts +2 -1
- package/src/util/hooks/useTimeout.ts +54 -0
- package/src/util/link-anchor/LinkAnchor.tsx +7 -6
- package/src/util/owner.ts +35 -0
- package/cjs/overlays/floating-menu/parts/FocusScope.d.ts +0 -22
- package/cjs/overlays/floating-menu/parts/FocusScope.js +0 -98
- package/cjs/overlays/floating-menu/parts/FocusScope.js.map +0 -1
- package/cjs/overlays/overlay/hooks/useEventCallback.js.map +0 -1
- package/cjs/overlays/overlay/hooks/useLatestRef.js.map +0 -1
- package/cjs/overlays/overlay/hooks/useRefWithInit.js.map +0 -1
- package/esm/overlays/floating-menu/parts/FocusScope.d.ts +0 -22
- package/esm/overlays/floating-menu/parts/FocusScope.js +0 -62
- package/esm/overlays/floating-menu/parts/FocusScope.js.map +0 -1
- package/esm/overlays/overlay/hooks/useEventCallback.js.map +0 -1
- package/esm/overlays/overlay/hooks/useLatestRef.js.map +0 -1
- package/esm/overlays/overlay/hooks/useRefWithInit.js.map +0 -1
- package/src/overlays/floating-menu/parts/FocusScope.tsx +0 -83
- /package/cjs/{overlays/overlay → util}/hooks/useEventCallback.d.ts +0 -0
- /package/cjs/{overlays/overlay → util}/hooks/useEventCallback.js +0 -0
- /package/cjs/{overlays/overlay → util}/hooks/useLatestRef.d.ts +0 -0
- /package/cjs/{overlays/overlay → util}/hooks/useRefWithInit.d.ts +0 -0
- /package/cjs/{overlays/overlay → util}/hooks/useRefWithInit.js +0 -0
- /package/esm/{overlays/overlay → util}/hooks/useEventCallback.d.ts +0 -0
- /package/esm/{overlays/overlay → util}/hooks/useEventCallback.js +0 -0
- /package/esm/{overlays/overlay → util}/hooks/useLatestRef.d.ts +0 -0
- /package/esm/{overlays/overlay → util}/hooks/useRefWithInit.d.ts +0 -0
- /package/esm/{overlays/overlay → util}/hooks/useRefWithInit.js +0 -0
- /package/src/{overlays/overlay → util}/hooks/useEventCallback.ts +0 -0
- /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 "
|
|
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 (
|
|
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
|
-
|
|
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 };
|