diff --git a/src/components/TopNavClient.tsx b/src/components/TopNavClient.tsx index 68c71629df9548..1e59f0de6ba27f 100644 --- a/src/components/TopNavClient.tsx +++ b/src/components/TopNavClient.tsx @@ -11,9 +11,16 @@ import platformSelectorStyles from './platformSelector/style.module.scss'; const mainSections = mainSectionsWithDropdowns; -// Add a helper hook for portal dropdown positioning +// Helper hook for dropdown positioning +// Returns both absolute (for portals) and fixed (for inline) coordinates function useDropdownPosition(triggerRef, open) { - const [position, setPosition] = useState({top: 0, left: 0, width: 0}); + const [position, setPosition] = useState<{ + top: number; + left: number; + width: number; + fixedTop: number; + fixedLeft: number; + } | null>(null); useEffect(() => { function updatePosition() { if (triggerRef.current && open) { @@ -22,6 +29,8 @@ function useDropdownPosition(triggerRef, open) { top: rect.bottom + window.scrollY, left: rect.left + window.scrollX, width: rect.width, + fixedTop: rect.bottom, + fixedLeft: rect.left, }); } } @@ -139,8 +148,29 @@ export default function TopNavClient({platforms}: {platforms: Platform[]}) { } } } + function handleKeyDown(e: KeyboardEvent) { + if (e.key === 'Escape') { + if (conceptsDropdownOpen) { + setConceptsDropdownOpen(false); + conceptsBtnRef.current?.focus(); + } + if (moreDropdownOpen) { + setMoreDropdownOpen(false); + moreBtnRef.current?.focus(); + } + if (platformDropdownOpen) { + setPlatformDropdownOpen(false); + setPlatformDropdownByClick(false); + platformBtnRef.current?.focus(); + } + } + } document.addEventListener('mousedown', handleClick); - return () => document.removeEventListener('mousedown', handleClick); + document.addEventListener('keydown', handleKeyDown); + return () => { + document.removeEventListener('mousedown', handleClick); + document.removeEventListener('keydown', handleKeyDown); + }; }, [ platformDropdownOpen, platformDropdownByClick, @@ -298,6 +328,39 @@ export default function TopNavClient({platforms}: {platforms: Platform[]}) { /> + {conceptsDropdownOpen && conceptsPosition && ( +