Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Dialog): auto-capture trigger element, remove triggerRef prop (#1146)

Cindy Zhange27da31695b6
diff --git a/apps/sandbox/src/app/(fullscreen)/pages/theme-editor/page.tsx b/apps/sandbox/src/app/(fullscreen)/pages/theme-editor/page.tsx
index be56fe7705..89413c526d 100644
--- a/apps/sandbox/src/app/(fullscreen)/pages/theme-editor/page.tsx
+++ b/apps/sandbox/src/app/(fullscreen)/pages/theme-editor/page.tsx
@@ -1669,7 +1669,6 @@ function MotionPreview() {
>(null);
const [modalOpen, setModalOpen] = React.useState(false);
const containerRef = React.useRef<HTMLDivElement>(null);
- const modalTriggerRef = React.useRef<HTMLButtonElement>(null);
const [containerWidth, setContainerWidth] = React.useState(1200);
React.useEffect(() => {
@@ -1849,7 +1848,6 @@ function MotionPreview() {
</div>
<XDSHStack gap={1}>
<XDSButton
- ref={modalTriggerRef}
label="Modal"
variant="secondary"
onClick={() => setModalOpen(true)}
@@ -2008,10 +2006,7 @@ function MotionPreview() {
</div>
</div>
</XDSAppShell>
- <XDSDialog
- isOpen={modalOpen}
- onOpenChange={setModalOpen}
- triggerRef={modalTriggerRef}>
+ <XDSDialog isOpen={modalOpen} onOpenChange={setModalOpen}>
<div style={{padding: 'var(--spacing-3)', flex: 1}}>
<XDSText type="body" color="secondary">
This modal uses XDSDialog with directional entry animation from the
@@ -2095,7 +2090,6 @@ function NavigationOverlaysPreview({
setOverlayOpen: (v: boolean) => void;
}) {
const [modalOpen, setModalOpen] = React.useState(false);
- const modalTriggerRef = React.useRef<HTMLButtonElement>(null);
return (
<>
@@ -2110,7 +2104,6 @@ function NavigationOverlaysPreview({
</XDSText>
<div style={{marginTop: 8}}>
<XDSButton
- ref={modalTriggerRef}
label="Open Modal"
onClick={() => setModalOpen(true)}
/>
@@ -2139,10 +2132,7 @@ function NavigationOverlaysPreview({
</div>
</XDSVStack>
</XDSCard>
- <XDSDialog
- isOpen={modalOpen}
- onOpenChange={setModalOpen}
- triggerRef={modalTriggerRef}>
+ <XDSDialog isOpen={modalOpen} onOpenChange={setModalOpen}>
<div style={{padding: 'var(--spacing-3)'}}>
<XDSText type="body" color="secondary">
This modal uses the XDSDialog component with directional entry
diff --git a/packages/core/src/Dialog/Dialog.doc.mjs b/packages/core/src/Dialog/Dialog.doc.mjs
index 43a6c81bb7..2400214d23 100644
--- a/packages/core/src/Dialog/Dialog.doc.mjs
+++ b/packages/core/src/Dialog/Dialog.doc.mjs
@@ -147,12 +147,6 @@ function Example() {
description: 'Dialog content.',
required: true,
},
- {
- name: 'triggerRef',
- type: 'RefObject<HTMLElement | null>',
- description:
- 'Ref to the trigger element that opened the dialog. When provided, the entry animation slides from the direction of the trigger and focus returns to the trigger on close.',
- },
{
name: 'width',
type: 'number | string',
@@ -412,12 +406,6 @@ function Example() {
description: '对话框内容。',
required: true,
},
- {
- name: 'triggerRef',
- type: 'RefObject<HTMLElement | null>',
- description:
- '打开对话框的触发元素的引用。提供时,进入动画从触发方向滑入,关闭时焦点返回到触发元素。',
- },
{
name: 'width',
type: 'number | string',
diff --git a/packages/core/src/Dialog/XDSDialog.tsx b/packages/core/src/Dialog/XDSDialog.tsx
index 5e7fbe1010..9e41ee993d 100644
--- a/packages/core/src/Dialog/XDSDialog.tsx
+++ b/packages/core/src/Dialog/XDSDialog.tsx
@@ -13,7 +13,7 @@
* - /apps/storybook/stories/Dialog.stories.tsx (storybook stories)
*/
-import {useEffect, useRef, type ReactNode, type RefObject} from 'react';
+import {useEffect, useRef, type ReactNode} from 'react';
import type {XDSBaseProps} from '../XDSBaseProps';
import * as stylex from '@stylexjs/stylex';
import {useScrollLock} from '../hooks/useScrollLock';
@@ -215,14 +215,6 @@ export interface XDSDialogProps extends XDSBaseProps<HTMLDialogElement> {
*/
onOpenChange: (isOpen: boolean) => unknown;
- /**
- * Ref to the trigger element that opens the dialog.
- * When provided, the dialog entry animation slides from the direction
- * of the trigger toward the viewport center. Focus is returned to
- * the trigger when the dialog closes.
- */
- triggerRef?: RefObject<HTMLElement | null>;
-
/**
* The width of the dialog.
* Numbers are treated as pixels, strings are used as-is.
@@ -292,7 +284,6 @@ export interface XDSDialogProps extends XDSBaseProps<HTMLDialogElement> {
export function XDSDialog({
isOpen,
onOpenChange,
- triggerRef,
width = 400,
maxHeight = '75vh',
position,
@@ -307,6 +298,10 @@ export function XDSDialog({
}: XDSDialogProps) {
const dialogRef = useRef<HTMLDialogElement>(null);
+ // Capture the element that was focused when the dialog opened,
+ // for directional animation origin and focus restoration on close.
+ const triggerElementRef = useRef<HTMLElement | null>(null);
+
// Derive dismissal behavior from purpose
const allowEscape = purpose !== 'required';
const allowBackdropClick = purpose === 'info';
@@ -328,9 +323,13 @@ export function XDSDialog({
if (!dialog) return;
if (isOpen) {
+ // Capture the currently focused element as the trigger — used for
+ // directional animation origin and focus restoration on close.
+ triggerElementRef.current = document.activeElement as HTMLElement | null;
+
// Set directional CSS custom properties before opening
- const trigger = triggerRef?.current;
- if (trigger) {
+ const trigger = triggerElementRef.current;
+ if (trigger && trigger !== document.body) {
const dir = getDialogDirection(trigger);
dialog.style.setProperty('--dialog-dir-x', `${dir.x}px`);
dialog.style.setProperty('--dialog-dir-y', `${dir.y}px`);
@@ -354,10 +353,11 @@ export function XDSDialog({
if (dialog.open) {
dialog.close();
}
- // Return focus to trigger
- triggerRef?.current?.focus();
+ // Return focus to the element that opened the dialog
+ triggerElementRef.current?.focus();
+ triggerElementRef.current = null;
}
- }, [isOpen, triggerRef]);
+ }, [isOpen]);
// Lock body scroll when dialog is open (iOS Safari workaround)
useScrollLock(isOpen);