From ea13c6b009e882b338459d1f13ba061beb293643 Mon Sep 17 00:00:00 2001 From: Ethan Setnik Date: Tue, 17 Nov 2026 12:30:24 -0500 Subject: [PATCH] fix: toggle hover popups on a tap, not on the touch pointer's events Hover actions get an implicit `touch` action, which toggled the popup on `touchstart`. On a real touch screen three things undid it: - A touch pointer fires `pointerenter` before `touchstart`, and the hover handler opened the popup on it, so `touchstart` closed it again. A hover popup on an element that cannot take focus needed two taps. - `touchstart` set `touchedRef`, which makes hover ignore mouse events, and only a click action cleared it. After one tap, a mouse never opened that popup again (a touch laptop). - A swipe that starts on the target opened the popup on `touchstart`, and a scroll sends no click, so nothing closed it. The hover handlers now ignore `touch` and `pen` pointer events (a pen on a touch screen fires touch events too) and let a `mouse` pointer clear `touchedRef`. The touch action toggles on the `touchend` of a touch the browser did not cancel: a touch it takes over to scroll or zoom ends in `pointercancel` or `touchcancel`, so a swipe never renders the popup. The new handlers are forwarded to a nested trigger like `onTouchStart`. Co-Authored-By: Claude Opus 5.5 --- src/index.tsx | 73 ++++++++++++++++++--- tests/mobile.test.tsx | 2 + tests/touch.test.tsx | 149 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 216 insertions(+), 8 deletions(-) create mode 100644 tests/touch.test.tsx diff --git a/src/index.tsx b/src/index.tsx index 22f29a0a..556b247f 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -281,6 +281,9 @@ export function generateTrigger( React.HTMLAttributes, | 'onClick' | 'onTouchStart' + | 'onTouchEnd' + | 'onTouchCancel' + | 'onPointerCancel' | 'onMouseEnter' | 'onMouseLeave' | 'onMouseMove' @@ -593,10 +596,10 @@ export function generateTrigger( nextOpen: boolean, delay?: number, callback?: (event: Event) => void, - ignoreCheck?: () => boolean, + ignoreCheck?: (event: Event) => boolean, ) { cloneProps[eventName] = (event: any, ...args: any[]) => { - if (!ignoreCheck || !ignoreCheck()) { + if (!ignoreCheck || !ignoreCheck(event)) { callback?.(event); triggerOpen(nextOpen, delay); } @@ -613,18 +616,53 @@ export function generateTrigger( /** Used for prevent `hover` event conflict with mobile env */ const touchedRef = React.useRef(false); + /** A touch that has started and may still turn out to be a tap */ + const touchPendingRef = React.useRef(false); + if (touchToShow || touchToHide) { + // Toggle when a tap ends, not when it starts: a touch the browser takes + // over to scroll or zoom ends in `pointercancel` (or `touchcancel`), and + // toggling on `touchstart` flashed the popup under the finger. cloneProps.onTouchStart = (...args: any[]) => { touchedRef.current = true; + touchPendingRef.current = true; - if (openRef.current && touchToHide) { - triggerOpen(false); - } else if (!openRef.current && touchToShow) { - triggerOpen(true); + // Pass to origin + originChildProps.onTouchStart?.(...args); + }; + + cloneProps.onPointerCancel = ( + event: React.PointerEvent, + ...args: any[] + ) => { + if (event.pointerType !== 'mouse') { + touchPendingRef.current = false; } // Pass to origin - originChildProps.onTouchStart?.(...args); + originChildProps.onPointerCancel?.(event, ...args); + }; + + cloneProps.onTouchCancel = (...args: any[]) => { + touchPendingRef.current = false; + + // Pass to origin + originChildProps.onTouchCancel?.(...args); + }; + + cloneProps.onTouchEnd = (...args: any[]) => { + if (touchPendingRef.current) { + touchPendingRef.current = false; + + if (openRef.current && touchToHide) { + triggerOpen(false); + } else if (!openRef.current && touchToShow) { + triggerOpen(true); + } + } + + // Pass to origin + originChildProps.onTouchEnd?.(...args); }; } @@ -666,7 +704,23 @@ export function generateTrigger( let onPopupMouseEnter: React.MouseEventHandler; let onPopupMouseLeave: VoidFunction; - const ignoreMouseTrigger = () => { + const ignoreMouseTrigger = (event: React.SyntheticEvent) => { + const { pointerType } = event as React.PointerEvent; + + // A touch pointer fires `pointerenter` before `touchstart` and + // `pointerleave` right after it. The touch action already handles the + // tap, so these must not open or close the popup a second time. A pen on + // a touch screen fires touch events too, so it is treated the same. + if (pointerType === 'touch' || pointerType === 'pen') { + return true; + } + + // A mouse pointer means the touch is over: hover works again. + if (pointerType === 'mouse') { + touchedRef.current = false; + } + + // Compatibility mouse events a tap fires after the touch. return touchedRef.current; }; @@ -773,6 +827,9 @@ export function generateTrigger( 'onClick', 'onMouseDown', 'onTouchStart', + 'onTouchEnd', + 'onTouchCancel', + 'onPointerCancel', 'onMouseEnter', 'onMouseLeave', 'onFocus', diff --git a/tests/mobile.test.tsx b/tests/mobile.test.tsx index 5fecdafc..1d01f39f 100644 --- a/tests/mobile.test.tsx +++ b/tests/mobile.test.tsx @@ -43,6 +43,7 @@ describe('Trigger.Mobile', () => { // Touch work fireEvent.touchStart(target); + fireEvent.touchEnd(target); fireEvent.mouseEnter(target); fireEvent.mouseLeave(target); flush(); @@ -50,6 +51,7 @@ describe('Trigger.Mobile', () => { // Touch again fireEvent.touchStart(target); + fireEvent.touchEnd(target); flush(); expect(document.querySelector('.rc-trigger-popup-hidden')).toBeTruthy(); }); diff --git a/tests/touch.test.tsx b/tests/touch.test.tsx new file mode 100644 index 00000000..00708a83 --- /dev/null +++ b/tests/touch.test.tsx @@ -0,0 +1,149 @@ +import { act, fireEvent, render } from '@testing-library/react'; +import React from 'react'; +import Trigger from '../src'; +import { placementAlignMap } from './util'; + +/** + * Dispatches a pointer event with a `pointerType`. jsdom has no + * `PointerEvent`, so it is a mouse event carrying the property. React builds + * `onPointerEnter` and `onPointerLeave` from `pointerover` and `pointerout`. + */ +function firePointer(target: Element, type: string, pointerType: string) { + const event = new MouseEvent(type, { bubbles: true, cancelable: true }); + Object.defineProperty(event, 'pointerType', { value: pointerType }); + fireEvent(target, event); +} + +/** + * The events a browser sends for a tap, in the order Chrome sends them: the + * touch pointer enters, presses and leaves, then the compatibility mouse + * events follow and the tap clicks. + */ +function tap(target: Element, pointerType = 'touch') { + firePointer(target, 'pointerover', pointerType); + firePointer(target, 'pointerdown', pointerType); + fireEvent.touchStart(target); + firePointer(target, 'pointerup', pointerType); + firePointer(target, 'pointerout', pointerType); + fireEvent.touchEnd(target); + fireEvent.mouseEnter(target); + fireEvent.mouseDown(target); + fireEvent.mouseUp(target); + fireEvent.click(target); +} + +describe('Trigger.Touch', () => { + beforeEach(() => { + jest.useFakeTimers(); + }); + + afterEach(() => { + jest.clearAllTimers(); + jest.useRealTimers(); + }); + + function flush() { + act(() => { + jest.runAllTimers(); + }); + } + + function renderHover() { + render( + trigger} + > +
+ , + ); + + return document.querySelector('.target'); + } + + function popup() { + return document.querySelector('.rc-trigger-popup'); + } + + it('opens a hover popup on the first tap', () => { + tap(renderHover()); + flush(); + + expect(popup()).toBeTruthy(); + expect(popup()).not.toHaveClass('rc-trigger-popup-hidden'); + }); + + it('opens on the first tap of a pen that also fires touch events', () => { + tap(renderHover(), 'pen'); + flush(); + + expect(popup()).not.toHaveClass('rc-trigger-popup-hidden'); + }); + + it('closes on a second tap', () => { + const target = renderHover(); + tap(target); + flush(); + + tap(target); + flush(); + + expect(popup()).toHaveClass('rc-trigger-popup-hidden'); + }); + + it('opens on a mouse hover after a tap', () => { + const target = renderHover(); + tap(target); + flush(); + tap(target); + flush(); + expect(popup()).toHaveClass('rc-trigger-popup-hidden'); + + firePointer(target, 'pointerover', 'mouse'); + fireEvent.mouseEnter(target); + flush(); + + expect(popup()).not.toHaveClass('rc-trigger-popup-hidden'); + }); + + it('never renders the popup for a touch that turns into a scroll', () => { + const target = renderHover(); + + firePointer(target, 'pointerover', 'touch'); + firePointer(target, 'pointerdown', 'touch'); + fireEvent.touchStart(target); + // The browser takes the touch over to scroll: no click follows. + firePointer(target, 'pointercancel', 'touch'); + firePointer(target, 'pointerout', 'touch'); + fireEvent.touchEnd(target); + flush(); + + expect(popup()).toBeFalsy(); + }); + + it('opens on a tap when it wraps another trigger', () => { + render( + outer} + > + inner} + > +
+ + , + ); + + tap(document.querySelector('.target')); + flush(); + + expect(document.querySelector('.outer').parentElement).not.toHaveClass( + 'rc-trigger-popup-hidden', + ); + }); +});