Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions components/responsive-dialog.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { act } from 'react';
import { createRoot } from 'react-dom/client';
import { expect, it, vi } from 'vitest';

import { ResponsiveDialog } from './responsive-dialog';

it('focuses the dialog itself on open instead of its first control', async () => {
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
const container = document.body.appendChild(document.createElement('div'));
const root = createRoot(container);
await act(async () =>
root.render(
<ResponsiveDialog
open
onOpenChange={() => {}}
title="Title"
description="Description"
>
<button type="button">Copy value</button>
</ResponsiveDialog>,
),
);
expect(document.activeElement?.getAttribute('role')).toBe('dialog');
await act(async () => root.unmount());
container.remove();
vi.unstubAllGlobals();
});
13 changes: 12 additions & 1 deletion components/responsive-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,18 @@ export const ResponsiveDialog: FC<ResponsiveDialogProps> = ({
);
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className={className}>
<DialogContent
className={cn('outline-hidden', className)}
// Focus the dialog itself, not its first control: the first control is
// usually a minor action like a copy button, and focusing the dialog
// lets screen readers announce the title first. It also keeps that
// button's tooltip from popping up while the dialog animates in.
// Vaul's drawer already skips autofocus.
onOpenAutoFocus={(event) => {
event.preventDefault();
(event.currentTarget as HTMLElement).focus({ preventScroll: true });
}}
>
<DialogHeader>
<DialogTitle>{title}</DialogTitle>
<DialogDescription className="sr-only">
Expand Down
Loading