Skip to content

fix(headless): keep pointer interactions from painting a focus ring - #9349

Merged
alexcarpenter merged 1 commit into
mainfrom
carp/headless-return-focus
Aug 6, 2026
Merged

fix(headless): keep pointer interactions from painting a focus ring#9349
alexcarpenter merged 1 commit into
mainfrom
carp/headless-return-focus

Conversation

@alexcarpenter

@alexcarpenter alexcarpenter commented Aug 6, 2026

Copy link
Copy Markdown
Member

Description

Preview: https://swingset-git-carp-headless-return-focus.clerkstage.dev/components/popover

Opening a Popover or Menu with the mouse in Safari painted a :focus-visible ring on the first control inside it, and dismissing it painted one on the trigger. Safari never focuses a button it was clicked on, so the popup becomes the page's first focused element, and every programmatic focus after that matches :focus-visible.

Focus modality now decides both ends:

  • Open — a pointer open focuses the popup container; a keyboard open still focuses the first tabbable control. Popover.Root takes initialFocus="first" to opt back into the old behavior (Autocomplete needs it).
  • Close — the new useReturnFocus hook resolves FloatingFocusManager's returnFocus from the interaction that closed the popup. Escape, Enter/Space, a Close button, and item clicks return focus to the trigger; a pointer press outside or a mouse click on the trigger leaves focus where it is. It backs Popover, Menu, Select, Dialog, and Drawer. Autocomplete is excluded: its reference is a text input, where restoring focus is correct combobox behavior.

BEFORE

before.mov

AFTER

aftrer.mov

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@vercel

vercel Bot commented Aug 6, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
clerk-js-sandbox Ready Ready Preview Aug 6, 2026 1:46pm
swingset Ready Ready Preview Aug 6, 2026 1:46pm

Request Review

@changeset-bot

changeset-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fd1bdbc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Safari never focuses a button it was clicked on, so a popup opened or dismissed
with the mouse leaves the trigger or first control matching :focus-visible.

Focus modality now decides both ends: the popup itself takes focus on a pointer
open, and a pointer dismiss leaves focus where it is instead of restoring it.
@pkg-pr-new

pkg-pr-new Bot commented Aug 6, 2026

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@9349

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@9349

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@9349

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@9349

@clerk/electron

npm i https://pkg.pr.new/@clerk/electron@9349

@clerk/electron-passkeys

npm i https://pkg.pr.new/@clerk/electron-passkeys@9349

@clerk/eslint-plugin

npm i https://pkg.pr.new/@clerk/eslint-plugin@9349

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@9349

@clerk/expo-google-signin

npm i https://pkg.pr.new/@clerk/expo-google-signin@9349

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@9349

@clerk/express

npm i https://pkg.pr.new/@clerk/express@9349

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@9349

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@9349

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@9349

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@9349

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@9349

@clerk/react

npm i https://pkg.pr.new/@clerk/react@9349

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@9349

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@9349

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@9349

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@9349

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@9349

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@9349

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@9349

commit: fd1bdbc

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds keyboard-aware interaction utilities and the useReturnFocus hook. Integrates return-focus references into dialog, drawer, menu, popover, and select primitives. Adds configurable popover initial focus behavior. Expands focus-management tests for keyboard and pointer activation. Adds a Changeset file.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

  • clerk/javascript#9295: Modifies the shared headless Menu and Popover primitives in a separate implementation and styling layer.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the primary Safari focus-ring fix for pointer interactions.
Description check ✅ Passed The description clearly explains the focus behavior changes, affected components, exclusions, and validation steps.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/headless/src/primitives/menu/menu.test.tsx`:
- Around line 403-440: Add a menu-level integration test alongside the pointer
and keyboard opening tests that opens the menu, clicks a focusable element
outside it with the pointer, waits for dismissal, and asserts
document.activeElement remains that outside target rather than returning to the
trigger. Reuse the existing Menu.Root structure and verify the returnFocusRef
behavior through the public interaction.

In `@packages/headless/src/primitives/popover/popover-context.ts`:
- Around line 25-26: Update useReturnFocus and the Popover/Select context
contracts to return a reactive false | ref value, using false when pointer-close
clears the return target so FloatingFocusManager does not resolve or focus a
fallback. Propagate this value through PopoverRoot, PopoverPositioner, and
SelectPositioner into both focus managers, preserving correct behavior for
trigger-click and outside-pointer close paths. Affected sites:
packages/headless/src/primitives/popover/popover-context.ts:25-26 (update
contract); packages/headless/src/primitives/popover/popover-root.tsx:87-88, 114,
135 (propagate value);
packages/headless/src/primitives/popover/popover-positioner.tsx:61-62 (pass
value to focus manager);
packages/headless/src/primitives/select/select-positioner.tsx:87 (update
contract/value usage).

In `@packages/headless/src/primitives/popover/popover.test.tsx`:
- Around line 261-290: Add tests in the popover dismissal suite for return focus
after an outside click and after toggling the trigger closed, asserting focus
returns to the popover trigger for pointer interactions. Keep the existing
Escape and Popover.Close coverage, and do not add Popover.Item-specific tests.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 6b61ac6b-3841-46df-a631-246fbd36e0ee

📥 Commits

Reviewing files that changed from the base of the PR and between 1799158 and fd1bdbc.

📒 Files selected for processing (23)
  • .changeset/headless-return-focus.md
  • packages/headless/src/hooks/use-return-focus.test.ts
  • packages/headless/src/hooks/use-return-focus.ts
  • packages/headless/src/primitives/autocomplete/autocomplete.test.tsx
  • packages/headless/src/primitives/dialog/dialog-context.ts
  • packages/headless/src/primitives/dialog/dialog-popup.tsx
  • packages/headless/src/primitives/dialog/dialog-root.tsx
  • packages/headless/src/primitives/drawer/drawer-popup.tsx
  • packages/headless/src/primitives/drawer/drawer-root.tsx
  • packages/headless/src/primitives/menu/menu-context.ts
  • packages/headless/src/primitives/menu/menu-positioner.tsx
  • packages/headless/src/primitives/menu/menu-root.tsx
  • packages/headless/src/primitives/menu/menu.test.tsx
  • packages/headless/src/primitives/popover/popover-context.ts
  • packages/headless/src/primitives/popover/popover-positioner.tsx
  • packages/headless/src/primitives/popover/popover-root.tsx
  • packages/headless/src/primitives/popover/popover.test.tsx
  • packages/headless/src/primitives/select/select-context.ts
  • packages/headless/src/primitives/select/select-positioner.tsx
  • packages/headless/src/primitives/select/select-root.tsx
  • packages/headless/src/utils/index.ts
  • packages/headless/src/utils/interaction-modality.test.ts
  • packages/headless/src/utils/interaction-modality.ts
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • clerk/clerk_go (manual)
  • clerk/dashboard (manual)
  • clerk/accounts (manual)
  • clerk/backoffice (manual)
  • clerk/clerk (manual)
  • clerk/clerk-docs (manual)
  • clerk/cloudflare-workers (manual)
  • clerk/cli (auto-detected)
  • clerk/clerk-ios (auto-detected)
  • clerk/clerk-android (auto-detected)

Comment thread packages/headless/src/primitives/menu/menu.test.tsx
Comment thread packages/headless/src/primitives/popover/popover-context.ts
Comment thread packages/headless/src/primitives/popover/popover.test.tsx

@austincalvelage austincalvelage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Makes sense to me!

@maxyinger maxyinger left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nice.

@alexcarpenter
alexcarpenter merged commit 2c464a3 into main Aug 6, 2026
87 of 89 checks passed
@alexcarpenter
alexcarpenter deleted the carp/headless-return-focus branch August 6, 2026 15:42
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.

3 participants