Skip to content

Commit 9efc692

Browse files
committed
fix(Dialog): panel type * 4
1 parent cca1d77 commit 9efc692

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/components/overlays/Dialog/Dialog.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useDOMRef } from '@react-spectrum/utils';
2-
import { DismissButton, FocusScope } from 'react-aria';
3-
import { forwardRef, ReactElement, useMemo } from 'react';
2+
import { DismissButton, FocusScope, useFocusManager } from 'react-aria';
3+
import { forwardRef, ReactElement, useEffect, useMemo, useState } from 'react';
44
import { useDialog, useMessageFormatter, AriaDialogProps } from 'react-aria';
55
import { DOMRef } from '@react-types/shared';
66

@@ -149,12 +149,12 @@ export const Dialog = forwardRef(function Dialog(
149149
return <DialogContent key="content" {...props} ref={ref} />;
150150
}, [props, ref]);
151151

152-
if (context.type === 'panel' || !isEntered) {
153-
return content;
154-
}
155-
156152
return (
157-
<FocusScope contain restoreFocus autoFocus>
153+
<FocusScope
154+
restoreFocus
155+
autoFocus={isEntered}
156+
contain={isEntered && context.type !== 'panel'}
157+
>
158158
{content}
159159
</FocusScope>
160160
);
@@ -177,6 +177,8 @@ const DialogContent = forwardRef(function DialogContent(
177177
...otherProps
178178
} = props;
179179

180+
const [isCloseDisabled, setIsCloseDisabled] = useState(true);
181+
180182
size = sizeMap[size.toUpperCase()] || size;
181183

182184
const styles: Styles = extractStyles(otherProps, STYLES_LIST);
@@ -196,6 +198,15 @@ const DialogContent = forwardRef(function DialogContent(
196198
dismissButton = <DismissButton onDismiss={onDismiss} />;
197199
}
198200

201+
const focusManager = useFocusManager();
202+
203+
useEffect(() => {
204+
setTimeout(() => {
205+
focusManager?.focusFirst();
206+
setIsCloseDisabled(false);
207+
});
208+
}, []);
209+
199210
// let hasHeader = useHasChild('[data-id="Header"]', domRef);
200211
// let hasFooter = useHasChild('[data-id="Footer"]', domRef);
201212

@@ -261,9 +272,9 @@ const DialogContent = forwardRef(function DialogContent(
261272
<SlotProvider slots={slots}>
262273
{isDismissable && (
263274
<Button
275+
isDisabled={isCloseDisabled}
264276
qa="ModalCloseButton"
265277
type="neutral"
266-
tabIndex="-1"
267278
styles={CLOSE_BUTTON_STYLES}
268279
icon={closeIcon || <CloseIcon />}
269280
label={formatMessage('dismiss')}

src/components/overlays/Dialog/stories/Dialog.stories.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,12 @@ CloseOnEscCloseBehaviorHide.play = async (context) => {
227227

228228
await userEvent.click(trigger);
229229

230+
await timeout(500);
231+
230232
const dialog = await findByRole('dialog');
231233

232234
await expect(dialog).toBeInTheDocument();
233235

234-
await timeout(500);
235-
236236
await expect(dialog.contains(document.activeElement)).toBe(true);
237237

238238
await userEvent.keyboard('{Escape}');
@@ -241,8 +241,6 @@ CloseOnEscCloseBehaviorHide.play = async (context) => {
241241

242242
await expect(dialog).toBeInTheDocument();
243243

244-
await timeout(500);
245-
246244
await waitFor(() => expect(document.activeElement).toBe(trigger));
247245
};
248246

0 commit comments

Comments
 (0)