79589156

Date: 2025-04-23 17:27:55
Score: 1
Natty:
Report link

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

Reasons:
  • Blacklisted phrase (1): this article
  • Blacklisted phrase (0.5): medium.com
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: npfist