MCPcopy Create free account
hub / github.com/adobe/react-spectrum / preventFocus

Function preventFocus

packages/react-aria/src/interactions/utils.ts:127–211  ·  view source on GitHub ↗
(target: FocusableElement | null)

Source from the content-addressed store, hash-verified

125 * using a capturing listener on the window and stopping propagation.
126 */
127export function preventFocus(target: FocusableElement | null): (() => void) | undefined {
128 // The browser will focus the nearest focusable ancestor of our target.
129 while (target && !isFocusable(target, {skipVisibilityCheck: true})) {
130 target = getParentElement(target) as FocusableElement | null;
131 }
132
133 let ownerWindow = getOwnerWindow(target);
134 let activeElement = getActiveElement(ownerWindow.document) as FocusableElement | null;
135 if (!activeElement || activeElement === target) {
136 return;
137 }
138
139 // Listen on the target's root (document or shadow root) so we catch focus events inside
140 // shadow DOM; they do not reach the main window.
141 let targetRoot = target?.getRootNode();
142 let root = targetRoot != null && isShadowRoot(targetRoot) ? targetRoot : getOwnerWindow(target);
143
144 // Focus is "moving to target" when it moves to the button or to a descendant of the button
145 // (e.g. SVG icon)
146 let isFocusMovingToTarget = (focusTarget: Element | null) =>
147 focusTarget === target || (isNode(focusTarget) && nodeContains(target, focusTarget));
148 // Blur/focusout events have their target as the element losing focus. Stop propagation when
149 // that is the previously focused element (activeElement) or a descendant (e.g. in shadow DOM).
150 let isBlurFromActiveElement = (eventTarget: Element | null) =>
151 eventTarget === activeElement ||
152 (activeElement != null && isNode(eventTarget) && nodeContains(activeElement, eventTarget));
153
154 ignoreFocusEvent = true;
155 let isRefocusing = false;
156 let onBlur: EventListener = e => {
157 if (isBlurFromActiveElement(getEventTarget(e) as Element) || isRefocusing) {
158 e.stopImmediatePropagation();
159 }
160 };
161
162 let onFocusOut: EventListener = e => {
163 if (isBlurFromActiveElement(getEventTarget(e) as Element) || isRefocusing) {
164 e.stopImmediatePropagation();
165
166 // If there was no focusable ancestor, we don't expect a focus event.
167 // Re-focus the original active element here.
168 if (!target && !isRefocusing) {
169 isRefocusing = true;
170 focusWithoutScrolling(activeElement);
171 cleanup();
172 }
173 }
174 };
175
176 let onFocus: EventListener = e => {
177 if (isFocusMovingToTarget(getEventTarget(e) as Element) || isRefocusing) {
178 e.stopImmediatePropagation();
179 }
180 };
181
182 let onFocusIn: EventListener = e => {
183 if (isFocusMovingToTarget(getEventTarget(e) as Element) || isRefocusing) {
184 e.stopImmediatePropagation();

Callers 1

usePressFunction · 0.90

Calls 6

isFocusableFunction · 0.90
getParentElementFunction · 0.90
getOwnerWindowFunction · 0.90
getActiveElementFunction · 0.90
isShadowRootFunction · 0.90
addEventListenerMethod · 0.80

Tested by

no test coverage detected