accessible-modal.js 1.1 KB

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