Skip to content

fix: Fix focusing radio, checkbox, and switch on press #7677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2025
Merged

Conversation

devongovett
Copy link
Member

Fixes two issues introduced in usePress refactor:

  1. Clicking on a RAC Radio, Checkbox, or Switch no longer focused the element. This is because usePress no longer programmatically focuses, and useRadio/useToggle include a preventDefault in the onClick handler on the label. RSP was not affected because there the native input is positioned on top of the label. Removing the preventDefault introduced the second problem...
  2. On iOS, tapping the radio or toggle caused it to be triggered twice. Additionally, in testing we noticed that unexpected focus rings were appearing on listbox items in Tailwind examples on iOS. This was because our own click simulation that we trigger during onPointerUp in case the browser's click event is not fired was triggering in addition to the native click event. After some spelunking into the WebKit source code, I found that iOS has a 32ms delay on the click event for non-form elements without a specific ARIA role. For radios and toggles, you are tapping on the <label> element not the actual input so the fast path exception in WebKit does not apply. So our timer of 5ms was firing before the browser's click event was fired, ending up with two clicks. Raising this timeout fixes the issue.

@devongovett devongovett changed the title Fix focusing radio, checkbox, and switch on press fix: Fix focusing radio, checkbox, and switch on press Jan 28, 2025
@rspbot
Copy link

rspbot commented Jan 28, 2025

Copy link
Member

@reidbarber reidbarber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, tested Chrome+Safari Mac and iOS Safari.

@devongovett devongovett added this pull request to the merge queue Jan 28, 2025
Merged via the queue into main with commit cbdf710 Jan 28, 2025
30 of 31 checks passed
@devongovett devongovett deleted the fix-ios-press branch January 28, 2025 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants