I believe I've figured out the solution. It feels a bit convoluted but it works.
I added a ref
to the <Container>
for the component and then use that ref to create focusableElements
to find all the buttons. When this ref is finally initialized, there are only two buttons: delete and cancel. I create a variable cancelButton
to keep this info. When the function for closing the modal is called, it then triggers cancelButton.focus()
which will shift focus to the button when the modal gets closed.
/*...*/
const imagesRef = useRef(null);
const imagesElements = imagesRef.current;
const focusableElements = imagesElements?.querySelectorAll("button");
const cancelButton = focusableElements && focusableElements[focusableElements.length - 1];
/*...*/
const handleFocus = () => {
setShowDeleteModal(false);
setSelectedImages([]);
cancelButton.focus();
};
return (
<Container ref={imagesRef}>
...
/*Inside showDeleteModal */
<Button secondary onKeyDown={handleFocus} onClick={handleFocus}>
No
</Button>
...
</Container>
)
Found the solution from this article here: https://medium.com/cstech/achieving-focus-trapping-in-a-react-modal-component-3f28f596f35b