Skip to content

Commit

Permalink
Make ModalDialogs restore focus by default
Browse files Browse the repository at this point in the history
  • Loading branch information
lyzadanger committed Mar 21, 2023
1 parent 5aac4e3 commit 6175c03
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/components/feedback/ModalDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const ModalDialogNext = function ModalDialog({
closeOnClickAway = false,
closeOnFocusAway = false,
initialFocus = 'auto',
restoreFocus = true,

...htmlAndPanelAttributes
}: ModalDialogProps) {
Expand All @@ -43,6 +44,7 @@ const ModalDialogNext = function ModalDialog({
closeOnFocusAway={closeOnFocusAway}
closeOnEscape={closeOnEscape}
initialFocus={initialFocus}
restoreFocus={restoreFocus}
classes={classnames(
// Column-flex layout to constrain content to max-height
'flex flex-col',
Expand Down
10 changes: 10 additions & 0 deletions src/components/feedback/test/ModalDialog-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ describe('ModalDialog', () => {
assert.isFalse(dialogProps.closeOnFocusAway);
});
});

describe('restoring focus', () => {
it('restores focus by default', () => {
const wrapper = mount(
<ModalDialog title="Test modal dialog">This is my dialog</ModalDialog>
);
const dialogProps = wrapper.find('Dialog').props();
assert.isTrue(dialogProps.restoreFocus);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function ModalDialogButtons() {
type ModalDialog_Props = ModalDialogProps & {
/** Pattern-wrapping prop. Not visible in source view */
_nonCloseable?: boolean;
_alwaysShowButton?: boolean;
};

/**
Expand All @@ -40,6 +41,7 @@ type ModalDialog_Props = ModalDialogProps & {
*/
function ModalDialog_({
buttons,
_alwaysShowButton = false,
_nonCloseable,
children,
...dialogProps
Expand All @@ -54,22 +56,25 @@ function ModalDialog_({
<Button onClick={closeDialog}>Escape!</Button>
);

if (!dialogOpen) {
return (
<Button onClick={() => setDialogOpen(!dialogOpen)} variant="primary">
Show modal dialog
</Button>
);
}
const openButton = (
<Button onClick={() => setDialogOpen(!dialogOpen)} variant="primary">
{dialogOpen ? 'Hide' : 'Show'} dialog
</Button>
);

return (
<ModalDialog
buttons={forwardedButtons}
{...dialogProps}
onClose={closeHandler}
>
{children}
</ModalDialog>
<>
{(!dialogOpen || _alwaysShowButton) && openButton}
{dialogOpen && (
<ModalDialog
buttons={forwardedButtons}
{...dialogProps}
onClose={closeHandler}
>
{children}
</ModalDialog>
)}
</>
);
}

Expand Down Expand Up @@ -98,10 +103,11 @@ export default function ModalDialogPage() {
<Library.Usage componentName="ModalDialog" />
<p>
By default, <code>ModalDialog</code>s close when the ESC key is
pressed.
pressed, and will restore focus when closed.
</p>
<Library.Demo title="Basic ModalDialog" withSource>
<ModalDialog_
_alwaysShowButton
buttons={<ModalDialogButtons />}
icon={EditIcon}
initialFocus={inputRef}
Expand Down

0 comments on commit 6175c03

Please sign in to comment.