From b3ced89433fe93b8ed0eeaf3501edd78c603d57d Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Thu, 26 Nov 2026 18:06:11 +0530 Subject: [PATCH 1/7] fix: improve modal keyboard focus navigation --- client/src/pages/Tasks.jsx | 92 +++++++++++++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 5 deletions(-) diff --git a/client/src/pages/Tasks.jsx b/client/src/pages/Tasks.jsx index f2dc76f..4ade002 100644 --- a/client/src/pages/Tasks.jsx +++ b/client/src/pages/Tasks.jsx @@ -10,13 +10,73 @@ import Tooltip from "../components/Shared/Tooltip.jsx"; // ── Modal Component ─────────────────────── const Modal = ({ open, onClose, title, children }) => { const titleId = useId(); + const modalRef = useRef(null); + const previousFocusRef = useRef(null); + const onCloseRef = useRef(onClose); + + // Keep the latest onClose function without restarting the effect. + onCloseRef.current = onClose; useEffect(() => { if (!open) return; + // Remember the button or element that opened this modal. + previousFocusRef.current = document.activeElement; + + const modal = modalRef.current; + + // Focus the element marked for initial focus, or the first focusable element. + const initialElement = + modal?.querySelector("[data-modal-autofocus]") || + modal?.querySelector( + 'button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], [tabindex]:not([tabindex="-1"])' + ); + + initialElement?.focus(); + const handleKeyDown = (event) => { + // Close the modal when Escape is pressed. if (event.key === "Escape") { - onClose(); + onCloseRef.current(); + return; + } + + // Handle Tab and Shift + Tab. + if (event.key !== "Tab") return; + + const focusableElements = Array.from( + modal.querySelectorAll( + 'button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], [tabindex]:not([tabindex="-1"])' + ) + ).filter((element) => element.getClientRects().length > 0); + + if (focusableElements.length === 0) { + event.preventDefault(); + return; + } + + const firstElement = focusableElements[0]; + const lastElement = focusableElements[focusableElements.length - 1]; + + // Shift + Tab: move from the first element to the last. + if (event.shiftKey) { + if ( + document.activeElement === firstElement || + !modal.contains(document.activeElement) + ) { + event.preventDefault(); + lastElement.focus(); + } + } + // Tab: move from the last element to the first. + else { + if ( + document.activeElement === lastElement || + !modal.contains(document.activeElement) + ) { + event.preventDefault(); + firstElement.focus(); + } } }; @@ -24,17 +84,38 @@ const Modal = ({ open, onClose, title, children }) => { return () => { window.removeEventListener("keydown", handleKeyDown); + + // Return focus to the element that opened the modal. + if (previousFocusRef.current?.isConnected) { + previousFocusRef.current.focus(); + } }; - }, [open, onClose]); + }, [open]); if (!open) return null; return (
-
-
+
+ +
-

{title}

+

+ {title} +

+
+
{children}
From 199321a0dbfece7fd88c4d93d80c56ed0df09e44 Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Thu, 26 Nov 2026 19:00:31 +0530 Subject: [PATCH 2/7] second improve modal keyboard focus navigation --- client/package-lock.json | 54 ---------------------------------------- 1 file changed, 54 deletions(-) diff --git a/client/package-lock.json b/client/package-lock.json index f90fa3e..62b5ea9 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -677,9 +677,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -697,9 +694,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -717,9 +711,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -737,9 +728,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -757,9 +745,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -777,9 +762,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1006,9 +988,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -1030,9 +1009,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -1054,9 +1030,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -1078,9 +1051,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -1253,9 +1223,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1273,9 +1240,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1293,9 +1257,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1313,9 +1274,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2658,9 +2616,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -2682,9 +2637,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -2706,9 +2658,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MPL-2.0", "optional": true, "os": [ @@ -2730,9 +2679,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MPL-2.0", "optional": true, "os": [ From 2fba841f59b1859ba92f90c9fe3e4e0134baab5c Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Thu, 26 Nov 2026 20:08:47 +0530 Subject: [PATCH 3/7] fix(client): resolve modal ref lint error --- client/src/pages/Tasks.jsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/client/src/pages/Tasks.jsx b/client/src/pages/Tasks.jsx index 4ade002..10dac49 100644 --- a/client/src/pages/Tasks.jsx +++ b/client/src/pages/Tasks.jsx @@ -14,8 +14,12 @@ const Modal = ({ open, onClose, title, children }) => { const previousFocusRef = useRef(null); const onCloseRef = useRef(onClose); - // Keep the latest onClose function without restarting the effect. + useEffect(() => { onCloseRef.current = onClose; +}, [onClose]); + + // Keep the latest onClose function without restarting the effect. + useEffect(() => { if (!open) return; From 2c7c31e90d27349fc27de71bc57c1bd57473a451 Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Thu, 26 Nov 2026 21:43:21 +0530 Subject: [PATCH 4/7] fix: address modal focus management review feedback --- client/src/pages/Tasks.jsx | 666 ++++++++++++++++++++++++++++++------- 1 file changed, 538 insertions(+), 128 deletions(-) diff --git a/client/src/pages/Tasks.jsx b/client/src/pages/Tasks.jsx index 10dac49..5fcb780 100644 --- a/client/src/pages/Tasks.jsx +++ b/client/src/pages/Tasks.jsx @@ -8,27 +8,22 @@ import { formatDueDate, isOverdue, isDueToday, isPastDate } from "../utils/dates import Tooltip from "../components/Shared/Tooltip.jsx"; // ── Modal Component ─────────────────────── -const Modal = ({ open, onClose, title, children }) => { +const Modal = ({ open, onClose, title, children, fallbackFocusRef }) => { const titleId = useId(); const modalRef = useRef(null); const previousFocusRef = useRef(null); const onCloseRef = useRef(onClose); + // Keep the latest onClose function without restarting the focus effect. useEffect(() => { - onCloseRef.current = onClose; -}, [onClose]); - - // Keep the latest onClose function without restarting the effect. - + onCloseRef.current = onClose; + }, [onClose]); useEffect(() => { if (!open) return; - // Remember the button or element that opened this modal. previousFocusRef.current = document.activeElement; - const modal = modalRef.current; - // Focus the element marked for initial focus, or the first focusable element. const initialElement = modal?.querySelector("[data-modal-autofocus]") || @@ -71,7 +66,7 @@ const Modal = ({ open, onClose, title, children }) => { event.preventDefault(); lastElement.focus(); } - } + } // Tab: move from the last element to the first. else { if ( @@ -83,18 +78,18 @@ const Modal = ({ open, onClose, title, children }) => { } } }; - window.addEventListener("keydown", handleKeyDown); - return () => { window.removeEventListener("keydown", handleKeyDown); - - // Return focus to the element that opened the modal. + // Restore focus to the element that opened the modal. if (previousFocusRef.current?.isConnected) { previousFocusRef.current.focus(); + } else { + // If the original button was removed, focus the persistent New Task button. + fallbackFocusRef?.current?.focus(); } }; - }, [open]); + }, [open, fallbackFocusRef]); if (!open) return null; @@ -148,6 +143,7 @@ const TaskForm = ({ initial = {}, onSave, onClose }) => { dueDate: initial.dueDate ? initial.dueDate.slice(0, 10) : "", tags: Array.isArray(initial.tags) ? initial.tags : [], }); + const [error, setError] = useState(""); const [tagInput, setTagInput] = useState(""); const [loading, setLoading] = useState(false); @@ -157,41 +153,78 @@ const TaskForm = ({ initial = {}, onSave, onClose }) => { setError(""); }; - // Hint for a past due date. When editing, only if the date was changed, - // so opening an overdue task to edit its title shows no hint. + // Hint for a past due date. When editing, only if the date was changed. const originalDueDate = initial.dueDate ? initial.dueDate.slice(0, 10) : ""; + const showPastDateHint = Boolean(form.dueDate) && isPastDate(form.dueDate) && (!initial._id || form.dueDate !== originalDueDate); - // Same rules as the API: trimmed, lowercased, no duplicates, up to 5 tags of 1–20 characters + // Same rules as the API: trimmed, lowercased, no duplicates, up to 5 tags of 1–20 characters. const addTag = () => { const tag = tagInput.trim().toLowerCase(); + if (!tag) return; - if (tag.length > 20) { setError("Each tag must be 1–20 characters long."); return; } - if (form.tags.includes(tag)) { setTagInput(""); setError(""); return; } - if (form.tags.length >= 5) { setError("You can add a maximum of 5 tags."); return; } + + if (tag.length > 20) { + setError("Each tag must be 1–20 characters long."); + return; + } + + if (form.tags.includes(tag)) { + setTagInput(""); + setError(""); + return; + } + + if (form.tags.length >= 5) { + setError("You can add a maximum of 5 tags."); + return; + } + setForm((prev) => ({ ...prev, tags: [...prev.tags, tag] })); setTagInput(""); setError(""); }; const removeTag = (tag) => { - setForm((prev) => ({ ...prev, tags: prev.tags.filter((item) => item !== tag) })); + setForm((prev) => ({ + ...prev, + tags: prev.tags.filter((item) => item !== tag), + })); + setError(""); }; const handleSubmit = async (e) => { e.preventDefault(); - if (!form.title.trim()) { setError("Title is required."); return; } - // Text still in the tag box is saved as a tag too, so nothing typed is lost + + if (!form.title.trim()) { + setError("Title is required."); + return; + } + + // Text still in the tag box is saved as a tag too. const pendingTag = tagInput.trim().toLowerCase(); - if (pendingTag.length > 20) { setError("Each tag must be 1–20 characters long."); return; } - const tags = [...new Set([...form.tags, ...(pendingTag ? [pendingTag] : [])])]; - if (tags.length > 5) { setError("You can add a maximum of 5 tags."); return; } + + if (pendingTag.length > 20) { + setError("Each tag must be 1–20 characters long."); + return; + } + + const tags = [ + ...new Set([...form.tags, ...(pendingTag ? [pendingTag] : [])]), + ]; + + if (tags.length > 5) { + setError("You can add a maximum of 5 tags."); + return; + } + setLoading(true); setError(""); + try { await onSave({ ...form, tags, dueDate: form.dueDate || null }); } catch (err) { @@ -203,38 +236,101 @@ const TaskForm = ({ initial = {}, onSave, onClose }) => { return (
- {error &&

{error}

} + {error && ( +

+ {error} +

+ )}
- - + + +
- -