accessible-modal.tsx 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. import { useCallback } from 'react'
  2. import { Modal, ModalProps } from 'react-bootstrap'
  3. // a bootstrap Modal with its `aria-hidden` attribute removed. Visisble modals
  4. // should not have their `aria-hidden` attribute set but that's a bug in our
  5. // version of react-bootstrap.
  6. function AccessibleModal({ show, ...otherProps }: ModalProps) {
  7. // use a callback ref to track the modal. This will re-run the function
  8. // when the element node or any of the dependencies are updated
  9. const setModalRef = useCallback(
  10. element => {
  11. if (!element) return
  12. const modalNode = element._modal && element._modal.modalNode
  13. if (!modalNode) return
  14. if (show) {
  15. modalNode.removeAttribute('aria-hidden')
  16. } else {
  17. modalNode.setAttribute('aria-hidden', 'true')
  18. }
  19. },
  20. // `show` is necessary as a dependency, but eslint thinks it is not
  21. // eslint-disable-next-line react-hooks/exhaustive-deps
  22. [show]
  23. )
  24. return <Modal show={show} {...otherProps} ref={setModalRef} />
  25. }
  26. export default AccessibleModal