From 8e51c75f4db23bb17a304a422fda5a81de89961d Mon Sep 17 00:00:00 2001 From: Benjamin Vellacott Date: Fri, 21 May 2021 12:48:37 +0300 Subject: [PATCH 1/6] [added] ability to disableFocusTrap --- src/helpers/bodyTrap.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/helpers/bodyTrap.js b/src/helpers/bodyTrap.js index 5787f6ce..873d5a0a 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); From 0c584fc3c96e997faf9c6728a87e77a5bd1840b5 Mon Sep 17 00:00:00 2001 From: Benjamin Vellacott Date: Fri, 21 May 2021 13:40:29 +0300 Subject: [PATCH 2/6] [added] ability to disableTabTrap --- specs/Modal.events.spec.js | 19 +++++++++++++++++++ src/components/ModalPortal.js | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) 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..820c3492 100644 --- a/src/components/ModalPortal.js +++ b/src/components/ModalPortal.js @@ -273,7 +273,7 @@ export default class ModalPortal extends Component { }; handleKeyDown = event => { - if (event.keyCode === TAB_KEY) { + if (event.keyCode === TAB_KEY && !this.props.disableTabTrap) { scopeTab(this.content, event); } From d135393d168aabc5cefcffddf291d1fa535d80f3 Mon Sep 17 00:00:00 2001 From: Benjamin Vellacott Date: Fri, 21 May 2021 16:17:40 +0300 Subject: [PATCH 3/6] update readme for disableFocusTrap & disableTabTrap --- docs/index.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/index.md b/docs/index.md index 114a31e5..934f20ac 100644 --- a/docs/index.md +++ b/docs/index.md @@ -100,6 +100,22 @@ 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. + */} + + 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 */} From 26e973983a822b7d0e10886a92b0f25229174495 Mon Sep 17 00:00:00 2001 From: Benjamin Vellacott Date: Fri, 21 May 2021 16:26:19 +0300 Subject: [PATCH 4/6] fix proptypes --- src/components/ModalPortal.js | 2 ++ src/helpers/bodyTrap.js | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/ModalPortal.js b/src/components/ModalPortal.js index 820c3492..a4cdb5a8 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, diff --git a/src/helpers/bodyTrap.js b/src/helpers/bodyTrap.js index 873d5a0a..f95a6133 100644 --- a/src/helpers/bodyTrap.js +++ b/src/helpers/bodyTrap.js @@ -53,7 +53,7 @@ function bodyTrap(eventType, openInstances) { instances = openInstances; - const trapFocus = instances.find(({ props }) => !props.disableFocusTrap) + const trapFocus = instances.find(({ props }) => !props.disableFocusTrap); if (trapFocus && instances.length > 0) { // Add focus trap From 9540c25512b9b08df212c42805355bd0466aca45 Mon Sep 17 00:00:00 2001 From: Benjamin Vellacott Date: Tue, 1 Jun 2021 16:34:32 +0300 Subject: [PATCH 5/6] Update src/components/ModalPortal.js Co-authored-by: Bruno Dias --- src/components/ModalPortal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ModalPortal.js b/src/components/ModalPortal.js index a4cdb5a8..74f3069f 100644 --- a/src/components/ModalPortal.js +++ b/src/components/ModalPortal.js @@ -275,7 +275,7 @@ export default class ModalPortal extends Component { }; handleKeyDown = event => { - if (event.keyCode === TAB_KEY && !this.props.disableTabTrap) { + if (!this.props.disableTabTrap && event.keyCode === TAB_KEY) { scopeTab(this.content, event); } From afada57e55e5752983fc29dfb53a97cdbaeff24b Mon Sep 17 00:00:00 2001 From: Benjamin Vellacott Date: Mon, 14 Jun 2021 09:41:30 +0300 Subject: [PATCH 6/6] Readme: multiple open modals with disableFocusTrap --- docs/index.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/index.md b/docs/index.md index 934f20ac..5503df2a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -106,6 +106,9 @@ import ReactModal from 'react-modal'; 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={