From 3a5f946a144585f1aefa46786d867291d747d6a6 Mon Sep 17 00:00:00 2001 From: Vivek Rajput Date: Sat, 21 Nov 2026 09:59:34 +0530 Subject: [PATCH] fix: add keyboard accessibility to Learn More modal --- src/components/LearnModeModal.jsx | 66 +++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/src/components/LearnModeModal.jsx b/src/components/LearnModeModal.jsx index 77736acf..394ab6c1 100644 --- a/src/components/LearnModeModal.jsx +++ b/src/components/LearnModeModal.jsx @@ -1,13 +1,65 @@ -import React from "react"; +import React, { useEffect, useRef } from "react"; import { FiX } from "react-icons/fi"; +const FOCUSABLE = + 'a[href], button:not([disabled]), input, select, textarea, [tabindex]:not([tabindex="-1"])'; + export default function LearnMoreModal({ open, onClose }) { + const dialogRef = useRef(null); + const onCloseRef = useRef(onClose); + onCloseRef.current = onClose; + + useEffect(() => { + if (!open) return; + + const previouslyFocused = document.activeElement; + const dialog = dialogRef.current; + if (!dialog) return; + + // Move focus inside the dialog + const items = dialog.querySelectorAll(FOCUSABLE); + (items[0] || dialog).focus(); + + const handleKeyDown = (e) => { + if (e.key === "Escape") { + e.preventDefault(); + onCloseRef.current(); + return; + } + if (e.key !== "Tab") return; + + const focusables = dialog.querySelectorAll(FOCUSABLE); + if (focusables.length === 0) { + e.preventDefault(); + return; + } + const first = focusables[0]; + const last = focusables[focusables.length - 1]; + + if (e.shiftKey && document.activeElement === first) { + e.preventDefault(); + last.focus(); + } else if (!e.shiftKey && document.activeElement === last) { + e.preventDefault(); + first.focus(); + } + }; + + document.addEventListener("keydown", handleKeyDown); + return () => { + document.removeEventListener("keydown", handleKeyDown); + // Restore focus to the element that opened the dialog + previouslyFocused?.focus?.(); + }; + }, [open]); + if (!open) return null; return ( <>