Skip to content

fix(headless): return focus to the ancestor reference when the dialog trigger is gone - #9742

Merged
alexcarpenter merged 1 commit into
mainfrom
carp/headless-return-focus-ancestor
Sep 12, 2026
Merged

fix(headless): return focus to the ancestor reference when the dialog trigger is gone#9742
alexcarpenter merged 1 commit into
mainfrom
carp/headless-return-focus-ancestor

Conversation

@alexcarpenter

Copy link
Copy Markdown
Member

Description

A Dialog opened from inside a Menu (a confirmation behind a "Remove" item) has no trigger of its own that survives closing: the menu item unmounts with the menu. useReturnFocus used to hand FloatingFocusManager that detached element, so on Escape or Close focus went to the manager's fallback guard instead of somewhere the user can see.

useReturnFocus now resolves its target lazily, when focus is actually restored. It still prefers the dialog's own trigger while that is on the page. Otherwise it walks up the floating tree to the nearest ancestor whose reference is still connected, which for a menu is its trigger button. Pointer dismissals still yield null so FloatingFocusManager keeps its existing behaviour there.

This is what lets a block like Confirmation be rendered inside Menu.Root and return focus to the menu's trigger without the call site tracking a ref.

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 Sep 12, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated
clerk-js-sandbox Ready Ready Preview Sep 12, 2026 12:24am UTC
swingset Ready Ready Preview Sep 12, 2026 12:24am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Sep 12, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8ebb896

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

@coderabbitai

coderabbitai Bot commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

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

Review profile: ASSERTIVE

Plan: Team

Run ID: 81e17dc1-db21-4961-902a-4edadc76a889

📥 Commits

Reviewing files that changed from the base of the PR and between 08e12ce and 8ebb896.

📒 Files selected for processing (3)
  • .changeset/headless-return-focus-ancestor.md
  • packages/headless/src/hooks/use-return-focus.ts
  • packages/headless/src/primitives/dialog/dialog.test.tsx
🔗 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)

Included review availability: 6 reviews are currently available. Your included PR review attempts over the past 7 days set your current allowance at 10 reviews per hour.


📝 Walkthrough

Walkthrough

useReturnFocus now supports focus restoration through connected ancestor floating references when the original trigger is unavailable. Pointer-based dismissals return null. Dialog tests cover dialogs opened from menu items and verify focus restoration after Escape and Cancel-button dismissal. An empty Changesets metadata block was added.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: maxyinger

Merge Risk: ⚪ Minimal · up to 8ebb8

The focus-restoration change has no substantiated merge-blocking issue.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1… 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 and concisely describes the main change: restoring focus to an ancestor reference when the dialog trigger is removed.
Description check ✅ Passed The description directly explains the focus restoration bug, the lazy ancestor-reference resolution, the pointer-dismissal behavior, and the added use case.
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.
Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI

Warning

Linked repositories: Your configuration references 7 linked repositories, but your current plan allows 5. Analyzed clerk/clerk_go, clerk/dashboard, clerk/accounts, clerk/backoffice, clerk/clerk, skipped clerk/clerk-docs, clerk/cloudflare-workers.


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

@pkg-pr-new

pkg-pr-new Bot commented Sep 12, 2026

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

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

@clerk/backend

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

@clerk/chrome-extension

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

@clerk/clerk-js

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

@clerk/electron

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

@clerk/electron-passkeys

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

@clerk/eslint-plugin

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

@clerk/expo

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

@clerk/expo-google-signin

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

@clerk/expo-passkeys

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

@clerk/express

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

@clerk/fastify

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

@clerk/hono

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

@clerk/localizations

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

@clerk/nextjs

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

@clerk/nuxt

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

@clerk/react

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

@clerk/react-router

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

@clerk/shared

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

@clerk/tanstack-react-start

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

@clerk/testing

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

@clerk/ui

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

@clerk/upgrade

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

@clerk/vue

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

commit: 8ebb896

@alexcarpenter
alexcarpenter merged commit ba35f1c into main Sep 12, 2026
81 of 82 checks passed
@alexcarpenter
alexcarpenter deleted the carp/headless-return-focus-ancestor branch September 12, 2026 01:54
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.

2 participants