Skip to content

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

TypeScript

fix(Dialog): preserve root event handlers (#7452)

Cindy Zhang0675971ec1db
diff --git a/.changeset/dialog-compose-root-events.md b/.changeset/dialog-compose-root-events.md
new file mode 100644
index 0000000000..52e1262522
--- /dev/null
+++ b/.changeset/dialog-compose-root-events.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `Dialog` now preserves consumer `onClick` handlers while retaining its controlled backdrop-dismissal behavior.
+
+@cixzhang
diff --git a/packages/core/src/Dialog/Dialog.test.tsx b/packages/core/src/Dialog/Dialog.test.tsx
index 98061bead4..62fa2c4f94 100644
--- a/packages/core/src/Dialog/Dialog.test.tsx
+++ b/packages/core/src/Dialog/Dialog.test.tsx
@@ -9,7 +9,7 @@
* SYNC: When Dialog.tsx changes, update tests to match new behavior
*/
-import {useState} from 'react';
+import {useState, type SyntheticEvent} from 'react';
import {readFileSync} from 'node:fs';
import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';
import type {MockInstance} from 'vitest';
@@ -1030,6 +1030,32 @@ describe('Dialog', () => {
expect(screen.getByTestId('custom-dialog')).toBeInTheDocument();
});
+ it('runs backdrop dismissal before a consumer onClick', () => {
+ const callOrder: string[] = [];
+ const onClick = vi.fn((event: SyntheticEvent) => {
+ event.preventDefault();
+ callOrder.push('consumer');
+ });
+ const onOpenChange = vi.fn(() => {
+ callOrder.push('dialog');
+ });
+ render(
+ <Dialog
+ isOpen={true}
+ onOpenChange={onOpenChange}
+ onClick={onClick}
+ data-testid="click-dialog">
+ Content
+ </Dialog>,
+ );
+
+ fireEvent.click(screen.getByTestId('click-dialog'));
+
+ expect(onClick).toHaveBeenCalledTimes(1);
+ expect(onOpenChange).toHaveBeenCalledWith(false);
+ expect(callOrder).toEqual(['dialog', 'consumer']);
+ });
+
it('does not forward native open prop to dialog element', () => {
render(
<Dialog
diff --git a/packages/core/src/Dialog/Dialog.tsx b/packages/core/src/Dialog/Dialog.tsx
index dd0bbe50e5..297eeb309e 100644
--- a/packages/core/src/Dialog/Dialog.tsx
+++ b/packages/core/src/Dialog/Dialog.tsx
@@ -5,7 +5,7 @@
/**
* @file Dialog.tsx
* @input Uses React, DialogHTMLAttributes, ReactNode, container (Layout), DialogContext, layerTextReset,
- * layerStructureReset, modalOutlet, interactionModality
+ * layerStructureReset, modalOutlet, interactionModality, chainEventHandlers
* @output Exports Dialog component, DialogProps, DialogVariant, DialogPurpose types
* @position Core implementation; consumed by index.ts, tested by Dialog.test.tsx
*
@@ -65,6 +65,7 @@ import {
} from '../Layout/padding.stylex';
import type {SpacingStep} from '../utils/types';
import {mergeProps} from '../utils';
+import {chainEventHandlers} from '../utils/chainEventHandlers';
import {devWarn} from '../utils/devWarning';
import {DialogContext} from './DialogContext';
import {themeProps} from '../utils/themeProps';
@@ -679,9 +680,19 @@ export function Dialog({
</div>
);
- // Filter out native open to prevent InvalidStateError when accidentally passed
+ // Filter out native open to prevent InvalidStateError when accidentally passed.
+ // Keep the consumer click handler separate so the modal path can compose it
+ // with Dialog-owned behavior without changing the inline preview path.
const hasPosition = position != null && !isFullscreen;
- const {open: _open, ...safeProps} = props as Record<string, unknown>;
+ const {onClick: onClickProp, ...propsWithoutClick} = props;
+ const {open: _open, ...safeProps} = propsWithoutClick as Record<
+ string,
+ unknown
+ >;
+ const inlineSafeProps: Record<string, unknown> = {
+ ...safeProps,
+ onClick: onClickProp,
+ };
// --- Inline rendering path (for documentation previews) ---
if (isInline) {
@@ -691,7 +702,7 @@ export function Dialog({
return (
<div
- {...safeProps}
+ {...inlineSafeProps}
{...mergeProps(
themeProps('dialog', {variant}),
stylex.props(
@@ -760,7 +771,7 @@ export function Dialog({
className,
style,
)}
- onClick={handleClick}
+ onClick={chainEventHandlers(handleClick, onClickProp)}
onCancel={handleCancel}
aria-modal="true"
// The default aria-labelledby (from a DialogHeader title) is set