Skip to content

[fix] keep references of modals. #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var ReactDOM = require('react-dom');
var ExecutionEnvironment = require('exenv');
var ModalPortal = React.createFactory(require('./ModalPortal'));
var ariaAppHider = require('../helpers/ariaAppHider');
var refCount = require('../helpers/refCount');
var elementClass = require('element-class');
var renderSubtreeIntoContainer = require("react-dom").unstable_renderSubtreeIntoContainer;
var Assign = require('lodash.assign');
Expand Down Expand Up @@ -61,12 +62,16 @@ var Modal = React.createClass({
this.node = document.createElement('div');
this.node.className = this.props.portalClassName;

if (this.props.isOpen) refCount.add(this);

var parent = getParentElement(this.props.parentSelector);
parent.appendChild(this.node);
this.renderPortal(this.props);
},

componentWillReceiveProps: function(newProps) {
if (newProps.isOpen) refCount.add(this);
if (!newProps.isOpen) refCount.remove(this);
var currentParent = getParentElement(this.props.parentSelector);
var newParent = getParentElement(newProps.parentSelector);

Expand All @@ -79,6 +84,8 @@ var Modal = React.createClass({
},

componentWillUnmount: function() {
refCount.remove(this);

if (this.props.ariaHideApp) {
ariaAppHider.show(this.props.appElement);
}
Expand All @@ -105,11 +112,13 @@ var Modal = React.createClass({
ReactDOM.unmountComponentAtNode(this.node);
var parent = getParentElement(this.props.parentSelector);
parent.removeChild(this.node);
elementClass(document.body).remove('ReactModal__Body--open');
if (refCount.count() === 0) {
elementClass(document.body).remove('ReactModal__Body--open');
}
},

renderPortal: function(props) {
if (props.isOpen) {
if (props.isOpen || refCount.count() > 0) {
elementClass(document.body).add('ReactModal__Body--open');
} else {
elementClass(document.body).remove('ReactModal__Body--open');
Expand Down
19 changes: 19 additions & 0 deletions lib/helpers/refCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const modals = [];

export function add (element) {
if (modals.indexOf(element) === -1) {
modals.push(element);
}
}

export function remove (element) {
const index = modals.indexOf(element);
if (index === -1) {
return;
}
modals.splice(index, 1);
}

export function count () {
return modals.length;
}
23 changes: 19 additions & 4 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ describe('Modal', () => {
});
});

it('give back focus to previous element or modal.', function (done) {
var modal = renderModal({
it('give back focus to previous element or modal.', (done) => {
function cleanup () {
unmountModal();
done();
}
const modal = renderModal({
isOpen: true,
onRequestClose: function () {
done();
onRequestClose () {
cleanup();
}
}, null);

Expand Down Expand Up @@ -215,6 +219,17 @@ describe('Modal', () => {
Modal.defaultStyles.content.position = previousStyle;
});

it('should remove class from body when no modals opened', () => {
const findReactModalOpenClass = () => document.body.className.indexOf('ReactModal__Body--open');
renderModal({ isOpen: true });
renderModal({ isOpen: true });
expect(findReactModalOpenClass() > -1).toBe(true);
unmountModal();
expect(findReactModalOpenClass() > -1).toBe(true);
unmountModal();
expect(findReactModalOpenClass() === -1).toBe(true);
});

it('adds class to body when open', function() {
renderModal({ isOpen: false });
expect(document.body.className.indexOf('ReactModal__Body--open') !== -1).toEqual(false);
Expand Down