diff --git a/src/components/Dialog.tsx b/src/components/Dialog.tsx index 80595b59..98f90ae6 100644 --- a/src/components/Dialog.tsx +++ b/src/components/Dialog.tsx @@ -1,9 +1,10 @@ -import React from "react"; +import React, { useEffect, useRef } from "react"; import { Modal } from "./Modal"; import { Island } from "./Island"; import { t } from "../i18n"; import useIsMobile from "../is-mobile"; import { back, close } from "./icons"; +import { KEYS } from "../keys"; import "./Dialog.scss"; @@ -12,9 +13,59 @@ export function Dialog(props: { className?: string; maxWidth?: number; onCloseRequest(): void; - closeButtonRef?: React.Ref; title: React.ReactNode; }) { + const islandRef = useRef(null); + + useEffect(() => { + const focusableElements = queryFocusableElements(); + + if (focusableElements.length > 0) { + // If there's an element other than close, focus it. + (focusableElements[1] || focusableElements[0]).focus(); + } + }, []); + + useEffect(() => { + if (!islandRef.current) { + return; + } + + function handleKeyDown(event: KeyboardEvent) { + if (event.key === KEYS.TAB) { + const focusableElements = queryFocusableElements(); + const { activeElement } = document; + const currentIndex = focusableElements.findIndex( + (element) => element === activeElement, + ); + + if (currentIndex === 0 && event.shiftKey) { + focusableElements[focusableElements.length - 1].focus(); + event.preventDefault(); + } else if ( + currentIndex === focusableElements.length - 1 && + !event.shiftKey + ) { + focusableElements[0].focus(); + event.preventDefault(); + } + } + } + + const node = islandRef.current; + node.addEventListener("keydown", handleKeyDown); + + return () => node.removeEventListener("keydown", handleKeyDown); + }, []); + + function queryFocusableElements() { + const focusableElements = islandRef.current?.querySelectorAll( + "button, a, input, select, textarea, div[tabindex]", + ); + + return focusableElements ? Array.from(focusableElements) : []; + } + return ( - +

{props.title} diff --git a/src/components/ExportDialog.tsx b/src/components/ExportDialog.tsx index cd917159..1b100e94 100644 --- a/src/components/ExportDialog.tsx +++ b/src/components/ExportDialog.tsx @@ -11,8 +11,6 @@ import { ActionsManagerInterface } from "../actions/types"; import Stack from "./Stack"; import { t } from "../i18n"; -import { KEYS } from "../keys"; - import { probablySupportsClipboardBlob } from "../clipboard"; import { getSelectedElements, isSomeElementSelected } from "../scene"; import useIsMobile from "../is-mobile"; @@ -35,7 +33,6 @@ function ExportModal({ onExportToSvg, onExportToClipboard, onExportToBackend, - closeButton, }: { appState: AppState; elements: readonly ExcalidrawElement[]; @@ -46,15 +43,12 @@ function ExportModal({ onExportToClipboard: ExportCB; onExportToBackend: ExportCB; onCloseRequest: () => void; - closeButton: React.RefObject; }) { const someElementIsSelected = isSomeElementSelected(elements, appState); const [scale, setScale] = useState(defaultScale); const [exportSelected, setExportSelected] = useState(someElementIsSelected); const previewRef = useRef(null); const { exportBackground, viewBackgroundColor } = appState; - const pngButton = useRef(null); - const onlySelectedInput = useRef(null); const exportedElements = exportSelected ? getSelectedElements(elements, appState) @@ -85,33 +79,8 @@ function ExportModal({ scale, ]); - useEffect(() => { - pngButton.current?.focus(); - }, []); - - function handleKeyDown(event: React.KeyboardEvent) { - if (event.key === KEYS.TAB) { - const { activeElement } = document; - if (event.shiftKey) { - if (activeElement === pngButton.current) { - closeButton.current?.focus(); - event.preventDefault(); - } - } else { - if (activeElement === closeButton.current) { - pngButton.current?.focus(); - event.preventDefault(); - } - if (activeElement === onlySelectedInput.current) { - closeButton.current?.focus(); - event.preventDefault(); - } - } - } - } - return ( -
+
@@ -122,7 +91,6 @@ function ExportModal({ title={t("buttons.exportToPng")} aria-label={t("buttons.exportToPng")} onClick={() => onExportToPng(exportedElements, scale)} - ref={pngButton} /> setExportSelected(event.currentTarget.checked) } - ref={onlySelectedInput} />{" "} {t("labels.onlySelected")} @@ -209,7 +176,6 @@ export function ExportDialog({ }) { const [modalIsShown, setModalIsShown] = useState(false); const triggerButton = useRef(null); - const closeButton = useRef(null); const handleClose = React.useCallback(() => { setModalIsShown(false); @@ -232,7 +198,6 @@ export function ExportDialog({ maxWidth={800} onCloseRequest={handleClose} title={t("buttons.export")} - closeButtonRef={closeButton} > )} diff --git a/src/components/Modal.tsx b/src/components/Modal.tsx index 6614924a..813be4c1 100644 --- a/src/components/Modal.tsx +++ b/src/components/Modal.tsx @@ -26,6 +26,7 @@ export function Modal(props: { aria-modal="true" onKeyDown={handleKeydown} aria-labelledby={props.labelledBy} + tabIndex={-1} >

{props.shortcuts.map((shortcut, index) => ( - <> + {shortcut} {props.isOr && index !== props.shortcuts.length - 1 && t("shortcutsDialog.or")} - + ))}