Skip to content

Commit

Permalink
Attach events to document.body, not window
Browse files Browse the repository at this point in the history
As those do not propagate to `window` on Chrome
  • Loading branch information
renchap committed Jun 18, 2024
1 parent c215bbf commit 14176c8
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions app/javascript/mastodon/components/hover_card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ export const HoverCard: React.FC = () => {
(e: MouseEvent) => {
const { target } = e;

if (!(target instanceof HTMLElement)) return;

if (target.matches('[data-hover-card]')) {
if (
target instanceof HTMLElement &&
target.matches('[data-hover-card]')
) {
clearTimeout(leaveTimerRef.current);
clearTimeout(enterTimerRef.current);

Expand Down Expand Up @@ -84,18 +85,18 @@ export const HoverCard: React.FC = () => {
}, [setOpen]);

useEffect(() => {
window.addEventListener('mouseenter', handleAnchorMouseEnter, {
document.body.addEventListener('mouseenter', handleAnchorMouseEnter, {
passive: true,
capture: true,
});
window.addEventListener('mouseleave', handleAnchorMouseLeave, {
document.body.addEventListener('mouseleave', handleAnchorMouseLeave, {
passive: true,
capture: true,
});

return () => {
window.removeEventListener('mouseenter', handleAnchorMouseEnter);
window.removeEventListener('mouseleave', handleAnchorMouseLeave);
document.body.removeEventListener('mouseenter', handleAnchorMouseEnter);
document.body.removeEventListener('mouseleave', handleAnchorMouseLeave);
};
}, [handleAnchorMouseEnter, handleAnchorMouseLeave]);

Expand Down

0 comments on commit 14176c8

Please sign in to comment.