# Why a mega menu closes on the second tap on a touch screen

On a touch screen, one tap fires pointerenter, pointerleave, sometimes focus, and then click. A hover-intent mega menu with a tap handler added on top has two code paths that write the same state. The hover path opens the tapped panel instantly when another is open, and the click handler then sees it open and closes it. The fix removes a writer instead of adding timing.

Published: 2026-12-04
Canonical: https://umar.codes/mega-menu-second-tap-touch

## Revisions

- 2026-12-02 — Created from notes on a storefront theme's menu history (anonymized), read 2026-11-30. No device trace exists, so the event sequence is derived from the code and the Pointer Events model.
- 2026-12-04 — Published on its Dec 4 (Fri) slot, from the draft written ahead and merged before its day.

---

A hover-intent mega menu can close on the second tap on a touch screen because one tap
fires `pointerenter`, `pointerleave`, sometimes `focus`, and then `click`. In the code I
read, a hover path and a tap handler both wrote the menu's state: the hover path opened
the second panel, and the click handler saw it open and closed it.

I read this bug out of the history of a storefront theme on a hosted commerce platform.
The retailer is anonymized. The repository holds commits and code, not a recording of
the bug, so the event sequences below are my reading of the code against the Pointer
Events model. No device trace exists, and I do not name a device or a browser because
the repository does not.

## How a hover-intent menu decides when to open

The desktop menu was a custom element in plain JavaScript, imported with the theme in
spring 2026 as a 179-line script. It held one piece of state: which top-level item was
active, if any. Four bindings on each top-level item fed it. `focus` and `pointerenter`
called one handler, which I will call activate, and `blur` and `pointerleave` called
another, deactivate.

The timing was a standard hover-intent design:

| Event | Behaviour |
|---|---|
| Activate, nothing open | Open after a **250 ms** delay |
| Activate, a panel already open | Switch to the new item at once, **0 ms** |
| Deactivate on the active item | Close after a **350 ms** delay |
| Activate during a pending close | Cancel the close |

The 250 ms delay stops a mouse that only crosses the bar from opening anything. The
0 ms switch lets a mouse slide along the bar between panels. The 350 ms close gives the
pointer time to travel down into the panel. Each number solves a mouse problem.

There was no click handler. A top-level item with a panel linked to a bare `#`, so the
link itself led nowhere.

## Why a mega menu breaks only at 750 px and up, on touch

The stylesheet hides the desktop menu below 750 px and shows a drawer instead. At 750 px
and up, every device gets the desktop menu, with or without a mouse. Nothing in the code
asked whether the device could hover.

That gives three cases at once:

- **Below 750 px:** the drawer. The mega menu script has nothing visible to act on.
- **750 px and up, fine pointer:** a mouse drives the hover path as designed.
- **750 px and up, coarse pointer:** a tablet, or a phone in landscape, gets a menu
  built for a pointer that hovers. This is the only broken case.

A commit title in the fix calls this the "tablet/landscape-mobile breakpoint", but the
code has no upper width limit. A large touch-only screen is in the same class. The
layout switch was a width query, and width was standing in for "has a mouse".

## Why a hover-intent menu ignores a first tap on touch

The first report, on 2026-07-13, was that a tap on a top-level item opened nothing.
In the Pointer Events model, a finger that cannot hover sends `pointerenter` just before
the touch and `pointerleave` just after the lift, then the compatibility mouse events
and the `click`. Against this code, by my reading:

1. `pointerenter`: nothing is open, so a 250 ms open is scheduled.
2. The finger lifts. `pointerleave` cancels the scheduled open.
3. `click` reaches no handler, and the link points at `#`.

If that reading is right, the delay never completes, because a usual tap is shorter
than 250 ms. The commit message puts it more briefly: touch devices "never fire a real hover state".

The first fix landed the same day: 42 lines added and 2 removed, across 2 files. It
created one `matchMedia('(pointer: coarse)')` list, moved the open logic into a shared
function, and added a click handler. On a fine pointer the handler returns at once. On a
coarse pointer it calls `preventDefault` and toggles: close the item if it is active,
open it if not.

## Why a tap on a second item closes a hover menu on touch

The second report came the next day, after the first fix, and it is the one in the
title. A tap opened a panel. A tap on a *different* top-level item then closed the menu, and the new panel
never stayed open.

The first fix added a third input path and left the other two running. On a touch
screen, one tap now fed all three. The first tap on item A still worked:

1. `pointerenter` on A schedules a 250 ms open.
2. `pointerleave` cancels it.
3. If the browser focuses the link, `focus` schedules another 250 ms open.
4. `click`: A is not active, so the click handler opens A immediately.
5. If step 3 happened, the scheduled open fires later, finds A already active, and does
   nothing.

The second tap, on item B while A was open, went differently:

1. `pointerenter` on B. A panel is open, so the 0 ms rule applies: A closes, B opens,
   and B is now the active item.
2. `pointerleave` on B. B is active, so a 350 ms close is scheduled.
3. If the browser focuses B's link, activate cancels that close. Nothing else changes.
4. `click` on B. The click handler asks whether B is active. It is, since step 1, so
   the handler takes its "toggle closed" branch.

A is closed and B is closed. One tap produced two activations: the hover path ran first
and changed the state, and the click path read that state and inverted it. Focus is
conditional because browsers differ on focusing a link on tap. The failure does not
need it.

The detail I find most useful is the asymmetry. The 250 ms hover-intent delay protected
the first tap: the hover path was too slow to write before the click did. The 0 ms
"switch at once when a panel is open" rule removed that protection once a panel was open.
That would explain how a fix could look correct after a single tap. The repository
does not say how it was tested.

## Fixing a touch menu by removing a state writer, not adding timing

The second fix landed 17 hours, 7 minutes and 38 seconds after the first. It was 33
lines added and 2 removed in 1 file, and only 2 of those added lines are statements.
The rest are comments or blank lines. One guard sits at the top of activate and one at
the top of deactivate: if `(pointer: coarse)` matches, return.

On a coarse pointer, `pointerenter`, `pointerleave`, `focus` and `blur` now change
nothing. The click handler is the only code that writes the menu's state. The commit
message calls it "the sole source of truth". No timer, no flag and no event-ordering
check was added.

Three more commits followed that day. Two only shortened the long comments from the
second fix (4 lines added and 23 removed between them), which took the script from 247
lines to 228. The last added the click binding to the overflow "More" button, which the
first fix had missed. All told, 5 commits touched the menu's behaviour in July.

The fix is the general rule for this class of bug. When two event paths can write one
piece of state, a race between them is a design problem, and tuning a delay only moves
the race. Removing one writer per input type ends it.

## What a coarse-pointer guard on a menu still misses

The guard has two costs that I can read in the code but did not see reported.

- **A keyboard on a tablet.** Focus is ignored whenever the primary pointer is coarse,
  so moving focus with a hardware keyboard no longer opens a panel. Enter or Space on
  a focused link still sends a `click`, and the click handler opens the panel.
- **A touch laptop.** `(pointer: coarse)` describes the *primary* pointer only. A laptop
  with a touch screen usually reports a fine primary pointer, so the click handler
  returns early and a finger tap goes through the hover path. That is a property of the
  media feature, not a defect anyone recorded here. The code does not use `any-pointer`,
  `hover` or `any-hover`, and does not read `pointerType` from the event.

Across the theme, 4 files use a pointer or hover media feature at all. The menu has 0
automated tests, before or after the fixes. The package manifest has 2 scripts and
neither runs tests. Both July failures arrived as reports, not as a failing test.

## Making the second tap on a touch menu follow the link

The menu script was not touched again until 2026-11-04, 113 days after the second fix.
That change was a feature, not a regression fix: top-level items with a panel got their
real URL in place of `#`.

It edits the same click handler, and it only works because of the July guard. On touch,
the first tap on an item opens its panel and the second tap on the same item now
follows the link. The handler returns early without `preventDefault`. In July, that same
second tap closed the panel, a designed toggle, because a `#` link left nothing else to
do. Once the second tap navigates, the menu needs another way to close, so the change
also added a document-level listener that closes the panel on a tap outside the menu.
The first commit was 13 lines added and 6 removed, and a follow-up 24 minutes later
added an initial collapsed ARIA state to the links.

"First tap opens, second tap navigates" only works if exactly one handler changes the
menu's state per tap. The July guard is what makes that true on touch.

The new early return also covers one more case, and its code comment names it: a tap on
a link inside an open panel. In the July version, that tap bubbled to the top-level
item's click handler, which called `preventDefault` and closed the panel. By my reading,
links inside the panels did not navigate on touch between July and November. I found no
report saying anyone saw that, and I did not run the old code to confirm it.

## What I would check first in any hover menu

- **Count the writers.** List every handler that changes "which panel is open". If more
  than one runs on a single tap, the bug is already there and is waiting for the right
  state.
- **Do not treat width as a stand-in for input.** A width query decides layout. Whether
  the device can hover is a separate question, and `(hover: hover)`, `(pointer: coarse)`
  and their `any-` forms answer different versions of it.
- **Test the second tap, not just the first.** The July
  bug lived in the transition from one open panel to another, and with 0 tests nothing
  exercised it.
