accessible-modal.tsx 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import { useCallback } from 'react'
  2. import { Modal, ModalProps } from 'react-bootstrap'
  3. // A wrapper for the v0.33 React Bootstrap Modal component,
  4. // which ensures that the `aria-hidden` attribute is not set on the modal when it's visible,
  5. // and that role="dialog" is not duplicated.
  6. // https://github.com/react-bootstrap/react-bootstrap/issues/4790
  7. // There are other ARIA attributes on these modals which could be improved,
  8. // but this at least makes them accessible for tests.
  9. function AccessibleModal(props: ModalProps) {
  10. const modalRef = useCallback(
  11. element => {
  12. const modalNode = element?._modal?.modalNode
  13. if (modalNode) {
  14. if (props.show) {
  15. modalNode.removeAttribute('role')
  16. modalNode.removeAttribute('aria-hidden')
  17. } else {
  18. // NOTE: possibly not ever used, as the modal is only rendered when shown
  19. modalNode.setAttribute('aria-hidden', 'true')
  20. }
  21. }
  22. },
  23. [props.show]
  24. )
  25. return <Modal {...props} ref={modalRef} />
  26. }
  27. export default AccessibleModal