mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-21 01:23:40 +00:00
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.
This commit is contained in:
1 parent
2035f9f476
commit
462f1db833
5 files changed
+35
-2
No files matched your search
@@ -553,6 +553,11 @@
|
||||
padding: 0 !important;
|
||||
}
|
||||
|
||||
/* The dialog itself takes focus on open (useDialogFocus); its fields show the ring. */
|
||||
.editor-dialog:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.editor-dialog > div:first-child {
|
||||
margin-bottom: 0 !important;
|
||||
flex-shrink: 0 !important;
|
||||
|
||||
@@ -345,6 +345,7 @@ function close(): void {
|
||||
|
||||
// A click on the backdrop must not discard what has been typed so far.
|
||||
const { isBackdropClose } = useBackdropGuard(() => props.open, ".editor-dialog");
|
||||
useDialogFocus(() => props.open, ".editor-dialog");
|
||||
|
||||
function onOpenChange(open: boolean): void {
|
||||
if (!open && !isBackdropClose()) close();
|
||||
|
||||
@@ -260,6 +260,7 @@ function close(): void {
|
||||
|
||||
// A click on the backdrop must not discard what has been typed so far.
|
||||
const { isBackdropClose } = useBackdropGuard(() => props.open, ".editor-dialog");
|
||||
useDialogFocus(() => props.open, ".editor-dialog");
|
||||
|
||||
function onOpenChange(open: boolean): void {
|
||||
if (!open && !isBackdropClose()) close();
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
Reference in new issue
Block a user