Skip to content

Commit 910e585

Browse files
authored
[BE][HUD] Improve dropdown behavior on touch devices (#6942)
### Summary - Fixed dropdown menus (Metrics ▾, Benchmarks ▾, Dev Infra ▾) that were navigating to pages instead of expanding on mobile - Resolved z-index stacking issues causing dropdowns to appear behind other menu items ### Changes 1. Mobile touch handling: Added touch device detection to prevent navigation and enable proper dropdown toggling on mobile devices 2. Z-index fix: Removed z-index: 9999 from navbar <li> elements to prevent stacking context conflicts ### Technical details - Touch devices now use onClick to toggle dropdowns with preventDefault() to stop navigation - Non-touch devices continue using hover behavior - Removing z-index from parent elements allows dropdown z-index to work correctly in the document's root stacking context --- Before: https://github.com/user-attachments/assets/f9810299-75b4-47cb-8bb0-f51d0c9b051e After: https://github.com/user-attachments/assets/b4ed1f34-612a-45de-a216-cd22e6392287
1 parent ad1bb73 commit 910e585

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

torchci/components/layout/NavBar.module.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
width: 100%;
88
min-height: 60px;
99
align-items: center;
10+
position: relative;
11+
z-index: 1000;
12+
backdrop-filter: blur(1px);
13+
-webkit-backdrop-filter: blur(1px);
1014
}
1115

1216
.navbar li {
1317
padding: 0.9rem 1rem;
1418
position: relative;
15-
z-index: 9999;
1619
display: flex;
1720
align-items: center;
1821
}

torchci/components/layout/NavBar.tsx

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import styles from "components/layout/NavBar.module.css";
22
import Link from "next/link";
3-
import { useState } from "react";
3+
import React, { useState } from "react";
44
import { AiFillGithub } from "react-icons/ai";
55
import ThemeModePicker from "../common/ThemeModePicker";
66
import LoginSection from "./LoginSection";
@@ -16,16 +16,58 @@ const NavBarDropdown = ({
1616
const dropdownStyle = dropdown ? { display: "block" } : {};
1717
const firstItemHref = items.length > 0 ? items[0].href : "#";
1818

19+
// Check if device is touch-enabled
20+
const isTouchDevice = React.useMemo(
21+
() =>
22+
typeof window !== "undefined" &&
23+
("ontouchstart" in window || navigator.maxTouchPoints > 0),
24+
[]
25+
);
26+
27+
// Set dropdown state only on non-touch devices
28+
const setDropdownIfNotTouch = (value: boolean) => {
29+
if (!isTouchDevice) {
30+
setDropdown(value);
31+
}
32+
};
33+
34+
// Close dropdown when clicking outside
35+
React.useEffect(() => {
36+
const handleClickOutside = (event: MouseEvent) => {
37+
const target = event.target as HTMLElement;
38+
if (!target.closest(`.${styles.dropdownContainer}`)) {
39+
setDropdown(false);
40+
}
41+
};
42+
43+
if (dropdown) {
44+
document.addEventListener("click", handleClickOutside);
45+
return () => {
46+
document.removeEventListener("click", handleClickOutside);
47+
};
48+
}
49+
}, [dropdown]);
50+
1951
return (
2052
<li
21-
onMouseEnter={() => setDropdown(true)}
22-
onMouseLeave={() => setDropdown(false)}
53+
onMouseEnter={() => setDropdownIfNotTouch(true)}
54+
onMouseLeave={() => setDropdownIfNotTouch(false)}
2355
style={{ padding: 0 }}
56+
className={`${styles.dropdownContainer} ${
57+
dropdown ? styles.dropdownOpen : ""
58+
}`}
2459
>
2560
<Link
2661
href={firstItemHref}
2762
prefetch={false}
2863
className={styles.dropdowntitle}
64+
onClick={(e) => {
65+
if (isTouchDevice) {
66+
// otherwise the menu will close immediately on touch devices
67+
e.preventDefault();
68+
}
69+
setDropdown(!dropdown);
70+
}}
2971
>
3072
{title}
3173
</Link>

torchci/styles/globals.css

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
--background-color: #ffffff;
33
--text-color: #212529;
44
--link-color: #0064cf;
5-
--navbar-bg: linear-gradient(326deg, rgb(255 246 246), rgb(255 247 236));
5+
--navbar-bg: linear-gradient(
6+
326deg,
7+
rgba(255, 246, 246, 0.5),
8+
rgba(255, 247, 236, 0.5)
9+
);
610
--navbar-shadow: 0 -4px 20px 0px #c2c2c2;
711
--dropdown-bg: white;
812
--code-bg: aliceblue;
@@ -74,7 +78,11 @@
7478
--background-color: #1e1e1e;
7579
--text-color: #e0e0e0;
7680
--link-color: #4a90e2;
77-
--navbar-bg: linear-gradient(326deg, #2a2a2a, #2d2d2d);
81+
--navbar-bg: linear-gradient(
82+
326deg,
83+
rgba(42, 42, 42, 0.8),
84+
rgba(45, 45, 45, 0.8)
85+
);
7886
--navbar-shadow: 0 -4px 20px 0px rgba(0, 0, 0, 0.4);
7987
--dropdown-bg: #2a2a2a;
8088
--code-bg: #2a2a2a;

0 commit comments

Comments
 (0)