Skip to content

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

TypeScript

fix(Dialog): hardening — swarm findings (#775)

Joey19aeac30f3d4
diff --git a/packages/core/src/Dialog/XDSDialog.test.tsx b/packages/core/src/Dialog/XDSDialog.test.tsx
index 0a0a0920ff..434fa0a4d8 100644
--- a/packages/core/src/Dialog/XDSDialog.test.tsx
+++ b/packages/core/src/Dialog/XDSDialog.test.tsx
@@ -114,7 +114,7 @@ describe('XDSDialog', () => {
</XDSDialog>,
);
- const dialog = screen.getByRole('dialog');
+ const dialog = screen.getByRole('alertdialog');
const escapeEvent = new KeyboardEvent('keydown', {
key: 'Escape',
bubbles: true,
@@ -132,7 +132,7 @@ describe('XDSDialog', () => {
</XDSDialog>,
);
- const dialog = screen.getByRole('dialog');
+ const dialog = screen.getByRole('alertdialog');
const cancelEvent = new Event('cancel', {cancelable: true});
dialog.dispatchEvent(cancelEvent);
@@ -218,4 +218,49 @@ describe('XDSDialog', () => {
);
expect(screen.getByTestId('custom-dialog')).toBeInTheDocument();
});
+
+ it('does not forward native open prop to dialog element', () => {
+ render(
+ <XDSDialog
+ isOpen={false}
+ onOpenChange={() => {}}
+ {...({open: true} as Record<string, unknown>)}>
+ Content
+ </XDSDialog>,
+ );
+ const dialog = screen.getByRole('dialog', {hidden: true});
+ // isOpen=false controls state; native open prop must not leak through
+ expect(dialog).not.toHaveAttribute('open');
+ });
+
+ describe('alertdialog role', () => {
+ it('sets role="alertdialog" when purpose is "required"', () => {
+ render(
+ <XDSDialog isOpen={true} onOpenChange={() => {}} purpose="required">
+ Content
+ </XDSDialog>,
+ );
+ expect(screen.getByRole('alertdialog')).toBeInTheDocument();
+ });
+
+ it('does not set role="alertdialog" when purpose is "info"', () => {
+ render(
+ <XDSDialog isOpen={true} onOpenChange={() => {}} purpose="info">
+ Content
+ </XDSDialog>,
+ );
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument();
+ expect(screen.getByRole('dialog')).toBeInTheDocument();
+ });
+
+ it('does not set role="alertdialog" when purpose is "form"', () => {
+ render(
+ <XDSDialog isOpen={true} onOpenChange={() => {}} purpose="form">
+ Content
+ </XDSDialog>,
+ );
+ expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument();
+ expect(screen.getByRole('dialog')).toBeInTheDocument();
+ });
+ });
});
diff --git a/packages/core/src/Dialog/XDSDialog.tsx b/packages/core/src/Dialog/XDSDialog.tsx
index 45706a82a6..26f4651416 100644
--- a/packages/core/src/Dialog/XDSDialog.tsx
+++ b/packages/core/src/Dialog/XDSDialog.tsx
@@ -13,7 +13,6 @@
* - /apps/storybook/stories/Dialog.stories.tsx (storybook stories)
*/
-
import {useEffect, useRef, type ReactNode} from 'react';
import type {XDSBaseProps} from '../XDSBaseProps';
import * as stylex from '@stylexjs/stylex';
@@ -92,7 +91,7 @@ const styles = stylex.create({
flexDirection: 'column',
overflow: 'hidden',
height: 'fit-content',
- // Animation for opening
+ // Animation for open/close
opacity: {
default: 0,
':where([open])': 1,
@@ -101,9 +100,17 @@ const styles = stylex.create({
default: 'scale(0.95) translateY(8px)',
':where([open])': 'scale(1) translateY(0)',
},
- transitionProperty: 'opacity, transform',
+ transitionProperty: 'opacity, transform, display, overlay',
transitionDuration: durationVars['--duration-medium'],
transitionTimingFunction: easeVars['--ease-standard'],
+ transitionBehavior: 'allow-discrete',
+ '@starting-style': {
+ opacity: 0,
+ transform: 'scale(0.95) translateY(8px)',
+ },
+ '@media (prefers-reduced-motion: reduce)': {
+ transitionDuration: '0s',
+ },
outline: {
default: null,
':focus-visible': `2px solid ${colorVars['--color-ring-focus']}`,
@@ -146,10 +153,10 @@ const dynamicStyles = stylex.create({
) => ({
// When position is set, disable auto margin and use fixed positioning
margin: 0,
- top: formatPosition(top),
- right: formatPosition(right),
- bottom: formatPosition(bottom),
- left: formatPosition(left),
+ top: top !== undefined ? formatPosition(top) : 'auto',
+ right: right !== undefined ? formatPosition(right) : 'auto',
+ bottom: bottom !== undefined ? formatPosition(bottom) : 'auto',
+ left: left !== undefined ? formatPosition(left) : 'auto',
}),
});
@@ -340,12 +347,16 @@ export function XDSDialog({
const isFullscreen = variant === 'fullscreen';
const hasPosition = position != null && !isFullscreen;
+ // Filter out native open to prevent InvalidStateError when accidentally passed
+ const {open: _open, ...safeProps} = props as Record<string, unknown>;
+
return (
<dialog
ref={setRefs}
onClick={handleClick}
onCancel={handleCancel}
aria-modal="true"
+ role={purpose === 'required' ? 'alertdialog' : undefined}
{...mergeProps(
xdsClassName('dialog', {variant}),
stylex.props(
@@ -365,7 +376,7 @@ export function XDSDialog({
className,
style,
)}
- {...props}>
+ {...safeProps}>
{children}
</dialog>
);