From 446ec1bbe5b0e57c125e60343a7c5c084499959b Mon Sep 17 00:00:00 2001 From: Aleksandr Gumroian Date: Mon, 16 Sep 2024 17:59:21 +0200 Subject: [PATCH] Refactor Modal component to use useFocusTrap hook --- src/bootstrap/Modal.js | 15 +++++++++------ src/utils/hooks.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/bootstrap/Modal.js b/src/bootstrap/Modal.js index aee202b..87f1cd6 100644 --- a/src/bootstrap/Modal.js +++ b/src/bootstrap/Modal.js @@ -9,7 +9,7 @@ import React, { useRef, useEffect } from "react"; import PropTypes from "prop-types"; -import { useClickOutside } from "../utils/hooks"; +import { useClickOutside, useFocusTrap } from "../utils/hooks"; import Portal from "../utils/Portal"; import "./Modal.css"; @@ -29,10 +29,11 @@ Modal.propTypes = { }; export function Modal({ shown, setShown, scrollable, size, children }) { - const dialogRef = useRef(); + const modalRef = useRef(); let modalSize = "modal-"; - useClickOutside(dialogRef, () => setShown(false)); + useClickOutside(modalRef, () => setShown(false)); + useFocusTrap(modalRef); useEffect(() => { const handleEsc = (event) => { @@ -45,7 +46,7 @@ export function Modal({ shown, setShown, scrollable, size, children }) { return () => { window.removeEventListener("keydown", handleEsc); }; - }, [setShown]); + }, [shown]); switch (size) { case "sm": @@ -65,11 +66,13 @@ export function Modal({ shown, setShown, scrollable, size, children }) { return (
-
{title}
+

{title}