The click that vanished: a MutationObserver was rebuilding a menu under the mouse
Today 2 menu items on our page quit working. Press "Type a line" in the "More" menu and nothing happened. Press again, nothing. The item worked in Vietnamese mode, and it had worked 2 days earlier. The cause was a click
Today 2 menu items on our page quit working. Press "Type a line" in the "More" menu and nothing happened. Press again, nothing. The item worked in Vietnamese mode, and it had worked 2 days earlier.
The cause was a click that never finished, and it is a good reminder of how a MutationObserver can bite.
The layout
ARLTS is a free, non-profit English-learning app. Its row of activity tabs is too wide for a laptop, which is why a little code moves the tabs that don't fit into a "More (2)" menu. The menu is a div of buttons, and a function called layout() rebuilds those buttons whenever the tab row changes.
Two observers call layout():
- One watches the tab row.
- One watches the
classattribute of<html>. We added it 2 days earlier, to let the menu re-measure when a learner turned on Vietnamese, because Vietnamese labels have a different width.
The old trick that made it bite
Months ago we added a hover guard. On every pointerdown, the page puts a class called hov-hold on <html>. It remains until the pointer moves more than 6 px. The point is to keep hover effects from flickering while a finger or a mouse button is held.
Put the 2 together:
- You press the mouse on "Type a line".
- The page adds
hov-holdto<html>. - The class observer fires, and
layout()rebuilds the menu. - The button under your cursor is now a brand-new element.
- You let go. A click needs the press and the release on one element, and the tab drops it.
Why my tests passed
I clicked the item through an automation tool and it opened every time. Automated clicks press and release in one instant, before the rebuild runs. A human takes 80 to 150 ms, and that is long enough to lose the click.
What found it was watching mutations instead of clicking. With a recorder on <html>, hov-hold toggled and the menu rebuilt 3 to 5 times per press. Then I repeated the press with a 120 ms hold, and the old code replaced the button every time.
Vietnamese mode worked because there, all tabs fit on the row, and there is no "More" menu to rebuild.
The fix, in 2 parts
First, the observer now reacts to the one change it was added for:
let wasVi = document.documentElement.classList.contains('ui-vi');
new MutationObserver(() => {
const vi = document.documentElement.classList.contains('ui-vi');
if (vi === wasVi) return; // another class changed, not our concern
wasVi = vi;
setTimeout(layout, 0);
}).observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
Next, layout() keeps the buttons it has when the list hasn't changed:
const key = rest.map(t => t.id + ':' + t.textContent).join(',');
if (menu._key !== key) {
menu._key = key;
menu.textContent = '';
// ...build the buttons again
}
Either part alone would have fixed this bug. Having both means a future class on <html> can't bring it back.
What I'd do again
-
Watch the exact change, not the whole attribute.
attributeFilter: ['class']fires for every class anyone adds. Compare the value you care about and leave early. - Don't rebuild what hasn't changed. A cheap key compare keeps DOM that a person is touching from being replaced.
- Test with a held press. Press, wait 120 ms, release. A tool that clicks in one instant can't reveal this class of bug.
The app is one HTML file, and the whole fix was 2 edits in one place.
ARLTS is free at https://arlts.org.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.