From 4c34b978ce6560eb569f7f82838651e62b1ac9c1 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Mon, 19 May 2025 17:13:11 -0700 Subject: frontend: adjust mobile dropdown menu position styling --- site/src/components/TitleBar.tsx | 69 ++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 9 deletions(-) (limited to 'site/src/components') diff --git a/site/src/components/TitleBar.tsx b/site/src/components/TitleBar.tsx index 8016a77..651b402 100644 --- a/site/src/components/TitleBar.tsx +++ b/site/src/components/TitleBar.tsx @@ -15,11 +15,16 @@ const TitleBar: React.FC = () => { const [otherDropdownOpen, setOtherDropdownOpen] = useState(false); const dropdownRef = useRef(null); const otherDropdownRef = useRef(null); + const rhythmGamesBtnRef = useRef(null); + const otherGamesBtnRef = useRef(null); const [searchParams] = useSearchParams(); const navigate = useNavigate(); const location = useLocation(); const isMoe = searchParams.has("moe"); + const [rhythmDropdownStyle, setRhythmDropdownStyle] = useState({}); + const [otherDropdownStyle, setOtherDropdownStyle] = useState({}); + const toggleTheme = () => { const params = new URLSearchParams(searchParams); @@ -106,6 +111,48 @@ const TitleBar: React.FC = () => { }, ]; + const calculateDropdownPosition = (buttonRef: React.RefObject) => { + if (!buttonRef.current) return {}; + + const rect = buttonRef.current.getBoundingClientRect(); + const dropdownWidth = 320; // sm:w-80 equivalent in px + const spaceOnRight = window.innerWidth - rect.right; + if (spaceOnRight < dropdownWidth) { + return { right: 'auto', left: '0' }; + } + return { right: '0', left: 'auto' }; + }; + const toggleRhythmDropdown = () => { + const newState = !dropdownOpen; + if (newState) { + setRhythmDropdownStyle(calculateDropdownPosition(rhythmGamesBtnRef)); + } + setDropdownOpen(newState); + }; + + const toggleOtherDropdown = () => { + const newState = !otherDropdownOpen; + if (newState) { + setOtherDropdownStyle(calculateDropdownPosition(otherGamesBtnRef)); + } + setOtherDropdownOpen(newState); + }; + + // Handle window resize + useEffect(() => { + const handleResize = () => { + if (dropdownOpen) { + setRhythmDropdownStyle(calculateDropdownPosition(rhythmGamesBtnRef)); + } + if (otherDropdownOpen) { + setOtherDropdownStyle(calculateDropdownPosition(otherGamesBtnRef)); + } + }; + + window.addEventListener('resize', handleResize); + return () => window.removeEventListener('resize', handleResize); + }, [dropdownOpen, otherDropdownOpen]); + useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if ( @@ -158,18 +205,19 @@ const TitleBar: React.FC = () => { -
+
All Games