←back to thread

293 points ulrischa | 1 comments | | HN request time: 0.208s | source
Show context
G1N ◴[] No.42174685[source]
RE this line of code at the bottom of the article:

  const isInvokedByMouse = event =>
  event.type === 'click' && (event.screenX !== 0 || event.screenY !== 0);
Why do you even have to check if screenX and screenY are non-zero (as opposed to just checking typeof event.screenX == "number")? Wouldn't that mean (and this is a wild edge-case) that if someone positioned their browser window so that the menu was in the top left corner (at position 0,0) the event handler would break again? Is this to block synthetic click events like (<div />).click()? Keyboard events don't have a screenX or screenY from what I remember as well.
replies(3): >>42175051 #>>42179466 #>>42180262 #
1. whstl ◴[] No.42175051[source]
This is just a heuristic to determine if the event is keydown or click.

In the article the author says that the issue is that the same function is handling both events, and they will work on refactoring it to something better.

The normal approach is just have different functions answering to different events. Or using more precise information about the event [1], instead of a heuristic.

[1] A suggestion was made by this poster: https://news.ycombinator.com/item?id=42174436