mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
Cancel the non-bubbling FocusScope mount event in the capture phase and focus the dialog itself. Keep keyboard navigation working without highlighting the name or opening the touch keyboard automatically.
27 lines
1.2 KiB
TypeScript
27 lines
1.2 KiB
TypeScript
/**
|
|
* Opens an editor dialog without jumping into its first field. reka-ui's
|
|
* FocusScope focuses and *selects* the first input on open, which highlights
|
|
* the name as if about to overwrite it and pops up the keyboard on touch
|
|
* screens. Its mount auto-focus is announced with a non-bubbling, cancelable
|
|
* event — RcDialog exposes no `openAutoFocus` hook — so it is caught here in
|
|
* the capture phase and focus goes to the dialog itself instead: Escape and
|
|
* Tab keep working, the fields stay untouched until the user picks one.
|
|
*/
|
|
export function useDialogFocus(isOpen: () => boolean, dialogSelector: string) {
|
|
function onMountAutoFocus(event: Event): void {
|
|
const dialog = event.target;
|
|
if (!(dialog instanceof HTMLElement) || !dialog.matches(dialogSelector)) return;
|
|
event.preventDefault();
|
|
dialog.focus({ preventScroll: true });
|
|
}
|
|
|
|
function listen(on: boolean): void {
|
|
if (typeof document === "undefined") return;
|
|
if (on) document.addEventListener("focusScope.autoFocusOnMount", onMountAutoFocus, true);
|
|
else document.removeEventListener("focusScope.autoFocusOnMount", onMountAutoFocus, true);
|
|
}
|
|
|
|
watch(isOpen, listen, { immediate: true });
|
|
onBeforeUnmount(() => listen(false));
|
|
}
|