TanStack / TanStack/tanstack.com

Define focus restoration for controlled dialogs

オープン
#1,209 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

source-audit
主要言語
TypeScript
スター
1.1k
フォーク
401
平均マージ
18時間 14分
マージ済み PR(30日)
56

説明

Evidence

Merged PR #1205 moved shipping overlays onto the shared Radix-backed Dialog, but the controlled-dialog call sites do not register an opener with DialogTrigger.

Current source has 16 controlled Dialog roots and no shipping DialogTrigger usage. Radix stores the trigger in context.triggerRef, then its modal onCloseAutoFocus handler prevents the browser default and calls context.triggerRef.current?.focus(). Without a registered trigger, that ref is null, so keyboard focus is not restored to the control that opened the dialog.

This is straightforward for colocated single openers, such as the NPM baseline package picker. It is not one mechanical change for every caller:

  • Some dialogs are controlled by an opener in a parent component.
  • The admin role removal dialog has one dynamic opener per table row.
  • Some design-system examples currently keep their opener outside the Dialog root.

Repeated title, body, keyword, and open-PR searches found no existing owner. Issue #775 tracks unrelated empty-name, label, landmark, and skip-navigation findings.

Expected impact

Closing a dialog should return keyboard and assistive-technology users to the control that opened it. Falling back to the document body loses their place and makes repeated actions harder.

Proposed direction

Adopt one explicit controlled-dialog focus contract:

  1. Require DialogTrigger asChild for dialogs with one colocated opener.
  2. Add a typed shared API for external or dynamic openers, such as an explicit restore-focus ref or a narrowly exposed Radix close-auto-focus callback.
  3. Add a browser-level regression test that opens a dialog, closes it with Escape and the close button, and verifies focus returns to the actual opener.

Alternatives and tradeoffs

  • Requiring DialogTrigger everywhere keeps the shared API smallest, but dynamic table-row openers would need dialog state and content moved per row or another structural change.
  • Restoring the previously active element automatically inside DialogContent reduces call-site work, but it changes every dialog and can restore focus to a stale or unrelated node when opening is indirect.
  • Exposing only onCloseAutoFocus matches Radix directly, but every caller must repeat the same focus logic and the shared primitive cannot enforce consistency.

Decision needed

@tannerlinsley, should the shared controlled-dialog contract support both DialogTrigger for colocated openers and an explicit restore-focus ref for external or dynamic openers, or should every dialog be restructured so one DialogTrigger always owns the opener?

Source: #1205

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、NPM のベースラインパッケージピッカー、管理者ロール削除ダイアログ、デザインシステムの例を含む、16 個の制御された Dialog のルートを確認します。それらの opener の配置を共有の Radix-backed Dialog と比較し、保留中の決定が解決された後にコントラクトを定義します。完了条件は、選択したフォーカス復元動作が実装され、Escape と閉じるボタンによる解除後にフォーカスが戻ることをブラウザレベルのリグレッションテストで検証できることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
accessibility, frontend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。