Skip to content

Commit 630c3ff

Browse files
committed
refactor(focus-scope): simplify focus management by removing unnecessary event dispatching and timeout logic
1 parent b8f1276 commit 630c3ff

File tree

1 file changed

+2
-22
lines changed

1 file changed

+2
-22
lines changed

headless/src/composables/use-focus-scope.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -146,30 +146,10 @@ export function useFocusScope(elRef: Ref<HTMLElement | undefined>, options?: Use
146146
if (onOpenAutoFocus) {
147147
container.removeEventListener(AUTOFOCUS_ON_OPEN, onOpenAutoFocus);
148148
}
149-
150-
// We hit a react bug (fixed in v17) with focusing in unmount.
151-
// We need to delay the focus a little to get around it for now.
152-
// See: https://github.com/facebook/react/issues/17894
153-
const unmountEvent = new CustomEvent(AUTOFOCUS_ON_CLOSE, AUTOFOCUS_EVENT_OPTIONS);
154149
if (onCloseAutoFocus) {
155-
container.addEventListener(AUTOFOCUS_ON_CLOSE, onCloseAutoFocus);
150+
container.removeEventListener(AUTOFOCUS_ON_CLOSE, onCloseAutoFocus);
156151
}
157-
container.dispatchEvent(unmountEvent);
158-
159-
const timerId = window.setTimeout(() => {
160-
if (!unmountEvent.defaultPrevented) {
161-
focus(previouslyFocusedElement ?? document.body, { select: true });
162-
}
163-
164-
// we need to remove the listener after we `dispatchEvent`
165-
if (onCloseAutoFocus) {
166-
container.removeEventListener(AUTOFOCUS_ON_CLOSE, onCloseAutoFocus);
167-
}
168-
169-
focusScopesStack.remove(focusScope);
170-
171-
window.clearTimeout(timerId);
172-
}, 0);
152+
focusScopesStack.remove(focusScope);
173153
});
174154
});
175155
}

0 commit comments

Comments
 (0)