Skip to content

Commit

Permalink
[added] a preventScroll prop
Browse files Browse the repository at this point in the history
to allows the modal to optionally use a preventScroll
flag when focusing the element that had focus
prior to display of the modal.
  • Loading branch information
cameracker authored and diasbruno committed Aug 19, 2020
1 parent 2ea6d44 commit ff0a7f5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
11 changes: 8 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ import ReactModal from 'react-modal';

bodyOpenClassName={
"ReactModal__Body--open"
/* String className to be applied to the document.body
/* String className to be applied to the document.body
(must be a constant string).
This attribute when set as `null` doesn't add any class
This attribute when set as `null` doesn't add any class
to document.body.
See the `Styles` section for more details. */}

Expand Down Expand Up @@ -121,6 +121,11 @@ import ReactModal from 'react-modal';
to be applied if desired.
This attribute is `dialog` by default. */}

preventScroll={
false
/* Boolean indicating if the modal should use the preventScroll flag when
restoring focus to the element that had focus prior to its display. */}

parentSelector={
() => document.body
/* Function that will be called to get the parent element
Expand All @@ -136,7 +141,7 @@ import ReactModal from 'react-modal';
data={
{ background: "green" }
/* Additional data attributes (optional). */}

testId={
""
/* String testId that renders a data-testid attribute in the DOM,
Expand Down
1 change: 1 addition & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default () => {
props.closeTimeoutMS.should.be.eql(0);
props.shouldFocusAfterRender.should.be.ok();
props.shouldCloseOnOverlayClick.should.be.ok();
props.preventScroll.should.be.false();
ReactDOM.unmountComponentAtNode(node);
ariaAppHider.resetForTesting();
Modal.setAppElement(document.body); // restore default
Expand Down
2 changes: 2 additions & 0 deletions src/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Modal extends Component {
shouldFocusAfterRender: PropTypes.bool,
shouldCloseOnOverlayClick: PropTypes.bool,
shouldReturnFocusAfterClose: PropTypes.bool,
preventScroll: PropTypes.bool,
parentSelector: PropTypes.func,
aria: PropTypes.object,
data: PropTypes.object,
Expand All @@ -82,6 +83,7 @@ class Modal extends Component {
shouldCloseOnEsc: true,
shouldCloseOnOverlayClick: true,
shouldReturnFocusAfterClose: true,
preventScroll: false,
parentSelector: () => document.body
};

Expand Down
3 changes: 2 additions & 1 deletion src/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default class ModalPortal extends Component {
shouldFocusAfterRender: PropTypes.bool,
shouldCloseOnOverlayClick: PropTypes.bool,
shouldReturnFocusAfterClose: PropTypes.bool,
preventScroll: PropTypes.bool,
role: PropTypes.string,
contentLabel: PropTypes.string,
aria: PropTypes.object,
Expand Down Expand Up @@ -185,7 +186,7 @@ export default class ModalPortal extends Component {

if (this.props.shouldFocusAfterRender) {
if (this.props.shouldReturnFocusAfterClose) {
focusManager.returnFocus();
focusManager.returnFocus(this.props.preventScroll);
focusManager.teardownScopedFocus();
} else {
focusManager.popWithoutFocus();
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/focusManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export function markForFocusLater() {
}

/* eslint-disable no-console */
export function returnFocus() {
export function returnFocus(preventScroll = false) {
let toFocus = null;
try {
if (focusLaterElements.length !== 0) {
toFocus = focusLaterElements.pop();
toFocus.focus();
toFocus.focus({ preventScroll });
}
return;
} catch (e) {
Expand Down

0 comments on commit ff0a7f5

Please sign in to comment.