Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ComposedModal): set focus back to trigger element on close #11390

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
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ const ComposedModal = React.forwardRef(function ComposedModal(
{...rest}
role="presentation"
ref={ref}
aria-hidden={!open}
onBlur={handleBlur}
onClick={handleClick}
onKeyDown={handleKeyDown}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import React, { useState } from 'react';
import React, { useState, useRef } from 'react';
import ReactDOM from 'react-dom';
import { action } from '@storybook/addon-actions';
import {
Expand Down Expand Up @@ -273,6 +273,8 @@ export const PassiveModal = () => {
};

export const WithStateManager = () => {
const closeButton = useRef();

/**
* Simple state manager for modals.
*/
Expand All @@ -296,10 +298,19 @@ export const WithStateManager = () => {
return (
<ModalStateManager
renderLauncher={({ setOpen }) => (
<Button onClick={() => setOpen(true)}>Launch composed modal</Button>
<Button ref={closeButton} onClick={() => setOpen(true)}>
Launch composed modal
</Button>
)}>
{({ open, setOpen }) => (
<ComposedModal open={open} onClose={() => setOpen(false)}>
<ComposedModal
open={open}
onClose={() => {
setOpen(false);
setTimeout(() => {
closeButton.current.focus();
});
joshblack marked this conversation as resolved.
Show resolved Hide resolved
}}>
<ModalHeader label="Account resources" title="Add a custom domain" />
<ModalBody>
<p style={{ marginBottom: '1rem' }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports[`<ComposedModal /> renders 1`] = `
selectorPrimaryFocus="[data-modal-primary-focus]"
>
<div
aria-hidden={false}
className="cds--modal is-visible"
onBlur={[Function]}
onClick={[Function]}
Expand Down