diff --git a/packages/taler-auditor-webui/src/components/modal/index.tsx b/packages/taler-auditor-webui/src/components/modal/index.tsx index 152e35d76..01d552062 100644 --- a/packages/taler-auditor-webui/src/components/modal/index.tsx +++ b/packages/taler-auditor-webui/src/components/modal/index.tsx @@ -5,6 +5,7 @@ import { useTranslationContext } from "@gnu-taler/web-util/browser"; import type { ComponentChildren, VNode } from "preact"; +import { useEffect, useRef } from "preact/hooks"; interface Props { active?: boolean; @@ -28,6 +29,17 @@ export function ConfirmModal({ label, }: Props): VNode | null { const { i18n } = useTranslationContext(); + const cancelButton = useRef(null); + useEffect(() => { + if (!active) return; + // Start on the safe choice so that Enter cannot confirm by accident, and + // hand focus back to whatever opened the dialog once it closes. + const opener = document.activeElement; + cancelButton.current?.focus(); + return () => { + if (opener instanceof HTMLElement && opener.isConnected) opener.focus(); + }; + }, [active]); if (!active) return null; return (
{children}