Keep your hover menu open long enough to click it
Here is a bug I have now fixed more than once, most recently on the marketing header of a side project: a desktop “Features” menu that opened on hover and closed the moment you tried to use it. You would point at the trigger, the panel would appear, you would move down toward an item, and the panel would vanish before you got there. Nothing in it was clickable with a mouse.
It is an easy bug to ship because it never shows up in a unit test and it barely shows up when you test by hand. If you move the mouse fast enough, or come at the panel from the side, it works. Real visitors move at a normal speed, straight down, and they hit it every time.
The cause: a gap you can’t see
The header row was 76px tall. The trigger button inside it was 44px tall, and the hover wrapper around the button was only as tall as the button. The panel was positioned with top: 100%, but relative to the header row, not the wrapper. So between the bottom of the wrapper and the top of the panel there was a strip of about 16px that belonged to neither.
The wrapper had an onMouseLeave that closed the menu. The pointer left the wrapper, entered that empty strip, the handler fired, and the panel unmounted before the pointer could reach it. Every part of the code was doing what it said. The layout just didn’t match the mental model of “the trigger and the panel are one hover area.”
The fix has three parts
1. Make the hover area continuous. The wrapper becomes the positioning context and fills the full header height, so the panel hangs directly off the element that owns the hover. I also let the panel overlap the wrapper by a few pixels. Subpixel rounding, borders and zoom levels can all reopen a one pixel gap, and a small overlap is cheaper than chasing them.
2. Add a short close delay. Even with no gap, a pointer moving diagonally toward an item near the edge of the panel can clip outside the hover area for a moment. So leaving does not close the menu right away. It starts a timer of about 200ms, and entering the wrapper or the panel again cancels it. That is long enough to forgive a sloppy path and short enough that the menu doesn’t feel sticky.
3. Don’t make hover the only way in. A menu that only opens on hover is broken for keyboard users and awkward on touch laptops. The trigger also opens on click, Enter, Space and ArrowDown. Opening from the keyboard moves focus to the first item. Escape closes it and returns focus to the trigger, and a click outside closes it too.
The phone version of this menu was a click accordion and never had the bug, which is a good hint about where the real problem was: the hover path.
A compact version in React
function HoverMenu({ label, items }) {
const [open, setOpen] = useState(false);
const closeTimer = useRef(null);
const wrapRef = useRef(null);
const triggerRef = useRef(null);
const firstItemRef = useRef(null);
const cancelClose = () => clearTimeout(closeTimer.current);
const show = () => {
cancelClose();
setOpen(true);
};
const scheduleClose = () => {
cancelClose();
closeTimer.current = setTimeout(() => setOpen(false), 200);
};
useEffect(() => {
if (!open) return;
const onDown = (e) => {
if (!wrapRef.current.contains(e.target)) setOpen(false);
};
document.addEventListener('pointerdown', onDown);
return () => document.removeEventListener('pointerdown', onDown);
}, [open]);
useEffect(() => cancelClose, []);
const onTriggerKey = (e) => {
if (['Enter', ' ', 'ArrowDown'].includes(e.key)) {
e.preventDefault();
show();
requestAnimationFrame(() => firstItemRef.current?.focus());
}
};
const onMenuKey = (e) => {
if (e.key === 'Escape') {
setOpen(false);
triggerRef.current.focus();
}
};
return (
<div className="menu-wrap" ref={wrapRef} onMouseEnter={show} onMouseLeave={scheduleClose}>
<button
ref={triggerRef}
aria-expanded={open}
aria-haspopup="true"
onClick={() => (open ? setOpen(false) : show())}
onKeyDown={onTriggerKey}
>
{label}
</button>
{open && (
<ul className="menu-panel" onKeyDown={onMenuKey}>
{items.map((item, i) => (
<li key={item.href}>
<a href={item.href} ref={i === 0 ? firstItemRef : null}>
{item.label}
</a>
</li>
))}
</ul>
)}
</div>
);
}
.menu-wrap {
position: relative;
height: 100%; /* fill the header row, not just the button */
display: flex;
align-items: center;
}
.menu-panel {
position: absolute;
top: calc(100% - 8px); /* overlap so there is no dead strip */
left: 50%;
transform: translateX(-50%);
}
The important lines are small: the wrapper fills the row, the panel overlaps it, and onMouseLeave schedules a close instead of closing.
How I tested it
For the delay, I used fake timers in the component tests: leave the wrapper, come back before 200ms, and the menu must still be open. Leave and wait past the delay, and it must be closed. Then one test per key: Enter, Space and ArrowDown open it and focus the first item, Escape closes it and puts focus back on the trigger.
Those tests prove the logic, but they can’t prove the geometry, and the geometry was the bug. So I also checked it in a real browser against a local production build, moving the pointer in a straight line from the trigger onto each item and clicking. All nine items navigated. If I were doing this on a team, I would make that a small browser test that moves the mouse in steps instead of jumping, because jumping straight to an element is exactly what hides this bug.
Tradeoffs
The close delay is a judgment call. Too short and you are back to the original bug for anyone with a slower hand. Too long and the menu lingers over the page after you have moved on, which is its own annoyance. Somewhere between 150ms and 300ms has worked for me.
Opening on hover at all is also a choice. A click-only menu has none of these problems, and on a site where the menu is the main way around, I would seriously consider it. I kept hover here because the header is a marketing page and people expect it, but I made sure hover is an extra, not the only door.
The short version
If a hover menu closes when you reach for it, look for the dead space between the trigger and the panel before you touch any JavaScript. Make the hover area one continuous region, overlap the panel a little, close on a short delay instead of instantly, and give the menu a full keyboard path. Then test it the way a person moves a mouse, not the way a test runner teleports one.