From 3344809a906c77bbebe6d6563e356e4b130ba367 Mon Sep 17 00:00:00 2001 From: Mohammad Shahid Beigh Date: Fri, 10 Jan 2025 13:05:52 +0530 Subject: [PATCH] fix(accessibility): resolve keyboard navigation issues on Actions menu and preview buttons (#4459) --- .../components/code-window/window-actions.tsx | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/apps/docs/components/code-window/window-actions.tsx b/apps/docs/components/code-window/window-actions.tsx index add1f96c93..fac8977186 100644 --- a/apps/docs/components/code-window/window-actions.tsx +++ b/apps/docs/components/code-window/window-actions.tsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, {useEffect, useCallback, useState} from "react"; import {tv} from "tailwind-variants"; import {clsx} from "@nextui-org/shared-utils"; @@ -19,12 +19,33 @@ const windowIconStyles = tv({ }); export const WindowActions: React.FC = ({title, className, ...props}) => { + const [isMenuVisible, setIsMenuVisible] = useState(true); + + const handleKeyDown = useCallback((e: KeyboardEvent) => { + if (e.key === "Escape") { + setIsMenuVisible(false); // Close the menu + } + }, []); + + useEffect(() => { + window.addEventListener("keydown", handleKeyDown); + + return () => { + window.removeEventListener("keydown", handleKeyDown); + }; + }, [handleKeyDown]); + + if (!isMenuVisible) return null; // Hide component if menu is closed + return (
@@ -32,10 +53,14 @@ export const WindowActions: React.FC = ({title, className, .
-
+
{title &&

{title}

}
-
+
+ +
); };