Files
WebHooker/app/composables/useDialogFocus.ts
T
翅膀 462f1db833 fix(editors): focus the dialog without selecting its name field
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.
2026-10-17 13:23:17 +08:00

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));
}