From c0047269c1c865e3e2a819a05b01c2320f60d645 Mon Sep 17 00:00:00 2001 From: Aakansha Doshi Date: Thu, 15 Apr 2021 20:29:00 +0530 Subject: [PATCH] fix: focus on last active element when dialog closes (#3447) * fix: focus on last active element when dialog closes * useState instead of ref --- src/components/Dialog.tsx | 13 ++++++++++--- src/components/ErrorDialog.tsx | 7 +++++-- src/components/ExportDialog.tsx | 3 --- src/components/LayerUI.tsx | 4 ---- src/excalidraw-app/collab/CollabWrapper.tsx | 2 -- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/components/Dialog.tsx b/src/components/Dialog.tsx index f2c917db..ada1bb5a 100644 --- a/src/components/Dialog.tsx +++ b/src/components/Dialog.tsx @@ -1,5 +1,5 @@ import clsx from "clsx"; -import React, { useEffect } from "react"; +import React, { useEffect, useState } from "react"; import { useCallbackRefState } from "../hooks/useCallbackRefState"; import { t } from "../i18n"; import { useIsMobile } from "../components/App"; @@ -20,6 +20,8 @@ export const Dialog = (props: { theme?: AppState["theme"]; }) => { const [islandNode, setIslandNode] = useCallbackRefState(); + const [lastActiveElement] = useState(document.activeElement); + useEffect(() => { if (!islandNode) { return; @@ -66,12 +68,17 @@ export const Dialog = (props: { return focusableElements ? Array.from(focusableElements) : []; }; + const onClose = () => { + (lastActiveElement as HTMLElement).focus(); + props.onCloseRequest(); + }; + return ( @@ -79,7 +86,7 @@ export const Dialog = (props: { {props.title}