Skip to content

Commit ff0a7f5

Browse files
camerackerdiasbruno
authored andcommitted
[added] a preventScroll prop
to allows the modal to optionally use a preventScroll flag when focusing the element that had focus prior to display of the modal.
1 parent 2ea6d44 commit ff0a7f5

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

docs/index.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ import ReactModal from 'react-modal';
7979

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

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

124+
preventScroll={
125+
false
126+
/* Boolean indicating if the modal should use the preventScroll flag when
127+
restoring focus to the element that had focus prior to its display. */}
128+
124129
parentSelector={
125130
() => document.body
126131
/* Function that will be called to get the parent element
@@ -136,7 +141,7 @@ import ReactModal from 'react-modal';
136141
data={
137142
{ background: "green" }
138143
/* Additional data attributes (optional). */}
139-
144+
140145
testId={
141146
""
142147
/* String testId that renders a data-testid attribute in the DOM,

specs/Modal.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export default () => {
5151
props.closeTimeoutMS.should.be.eql(0);
5252
props.shouldFocusAfterRender.should.be.ok();
5353
props.shouldCloseOnOverlayClick.should.be.ok();
54+
props.preventScroll.should.be.false();
5455
ReactDOM.unmountComponentAtNode(node);
5556
ariaAppHider.resetForTesting();
5657
Modal.setAppElement(document.body); // restore default

src/components/Modal.js

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Modal extends Component {
6060
shouldFocusAfterRender: PropTypes.bool,
6161
shouldCloseOnOverlayClick: PropTypes.bool,
6262
shouldReturnFocusAfterClose: PropTypes.bool,
63+
preventScroll: PropTypes.bool,
6364
parentSelector: PropTypes.func,
6465
aria: PropTypes.object,
6566
data: PropTypes.object,
@@ -82,6 +83,7 @@ class Modal extends Component {
8283
shouldCloseOnEsc: true,
8384
shouldCloseOnOverlayClick: true,
8485
shouldReturnFocusAfterClose: true,
86+
preventScroll: false,
8587
parentSelector: () => document.body
8688
};
8789

src/components/ModalPortal.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export default class ModalPortal extends Component {
5151
shouldFocusAfterRender: PropTypes.bool,
5252
shouldCloseOnOverlayClick: PropTypes.bool,
5353
shouldReturnFocusAfterClose: PropTypes.bool,
54+
preventScroll: PropTypes.bool,
5455
role: PropTypes.string,
5556
contentLabel: PropTypes.string,
5657
aria: PropTypes.object,
@@ -185,7 +186,7 @@ export default class ModalPortal extends Component {
185186

186187
if (this.props.shouldFocusAfterRender) {
187188
if (this.props.shouldReturnFocusAfterClose) {
188-
focusManager.returnFocus();
189+
focusManager.returnFocus(this.props.preventScroll);
189190
focusManager.teardownScopedFocus();
190191
} else {
191192
focusManager.popWithoutFocus();

src/helpers/focusManager.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ export function markForFocusLater() {
3434
}
3535

3636
/* eslint-disable no-console */
37-
export function returnFocus() {
37+
export function returnFocus(preventScroll = false) {
3838
let toFocus = null;
3939
try {
4040
if (focusLaterElements.length !== 0) {
4141
toFocus = focusLaterElements.pop();
42-
toFocus.focus();
42+
toFocus.focus({ preventScroll });
4343
}
4444
return;
4545
} catch (e) {

0 commit comments

Comments
 (0)