diff --git a/docs/index.md b/docs/index.md index 114a31e5..5503df2a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -100,6 +100,25 @@ import ReactModal from 'react-modal'; true /* Boolean indicating if the modal should be focused after render. */} + disableFocusTrap={ + false + /* + Boolean indicating if the modal should allow focus to leave the modal when it's visible. + If you set this flag to true, it's likely that you'll want to set disableTabTrap=true also, + to allow focus to leave the modal using the tab button. + + It is worth mentioning that if you have many modals open and one or more of them don't have + disableFocusTrap set, a focus trap will exist until those modals have been closed. + */} + + disableTabTrap={ + true + /* + Boolean indicating if the modal should allow focus to leave the modal using the tab key. + If you set this flag to true, it's likely that you'll want to set disableFocusTrap=true also, + because otherwise the focus will be prevented from leaving the component. + */} + shouldCloseOnOverlayClick={ true /* Boolean indicating if the overlay should close the modal */} diff --git a/specs/Modal.events.spec.js b/specs/Modal.events.spec.js index 0e61c1bf..21ab1921 100644 --- a/specs/Modal.events.spec.js +++ b/specs/Modal.events.spec.js @@ -109,6 +109,25 @@ export default () => { }); }); + + it("does NOT keep focus inside the modal when child has no tabbable elements and disableTabTrap === true", () => { + let tabPrevented = false; + const props = { + isOpen: true, + disableTabTrap: true, + }; + withModal(props, "hello", modal => { + const content = mcontent(modal); + document.activeElement.should.be.eql(content); + tabKeyDown(content, { + preventDefault() { + tabPrevented = true; + } + }); + tabPrevented.should.be.eql(false); + }); + }); + describe("shouldCloseOnEsc", () => { context("when true", () => { it("should close on Esc key event", () => { diff --git a/src/components/ModalPortal.js b/src/components/ModalPortal.js index d28ee455..74f3069f 100644 --- a/src/components/ModalPortal.js +++ b/src/components/ModalPortal.js @@ -59,6 +59,8 @@ export default class ModalPortal extends Component { shouldFocusAfterRender: PropTypes.bool, shouldCloseOnOverlayClick: PropTypes.bool, shouldReturnFocusAfterClose: PropTypes.bool, + disableFocusTrap: PropTypes.bool, + disableTabTrap: PropTypes.bool, preventScroll: PropTypes.bool, role: PropTypes.string, contentLabel: PropTypes.string, @@ -273,7 +275,7 @@ export default class ModalPortal extends Component { }; handleKeyDown = event => { - if (event.keyCode === TAB_KEY) { + if (!this.props.disableTabTrap && event.keyCode === TAB_KEY) { scopeTab(this.content, event); } diff --git a/src/helpers/bodyTrap.js b/src/helpers/bodyTrap.js index 5787f6ce..f95a6133 100644 --- a/src/helpers/bodyTrap.js +++ b/src/helpers/bodyTrap.js @@ -53,7 +53,9 @@ function bodyTrap(eventType, openInstances) { instances = openInstances; - if (instances.length > 0) { + const trapFocus = instances.find(({ props }) => !props.disableFocusTrap); + + if (trapFocus && instances.length > 0) { // Add focus trap if (document.body.firstChild !== before) { document.body.insertBefore(before, document.body.firstChild);