TanStack / TanStack/tanstack.com

Define focus restoration for controlled dialogs

Đang mở
#1,209 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

source-audit
Ngôn ngữ chính
TypeScript
Star
1.1k
Fork
401
Merge trung bình
18 giờ 14 phút
Pull request đã merge (30 ngày)
56

Mô tả

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

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng việc xem xét 16 Dialog root được kiểm soát, bao gồm trình chọn package baseline của NPM, dialog gỡ bỏ vai trò quản trị viên và các ví dụ của design-system. So sánh cách bố trí opener của chúng với Dialog dùng chung dựa trên Radix và xác định contract sau khi quyết định đang chờ được giải quyết. Được xem là hoàn tất khi hành vi khôi phục focus đã chọn được triển khai và một bài kiểm thử hồi quy ở cấp trình duyệt xác minh rằng focus quay lại sau Escape và sau khi đóng bằng nút đóng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
accessibility, frontend
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.