Skip to content

Commit

Permalink
[fixed] Modal Null Exception when react-bootstrap is loaded before th…
Browse files Browse the repository at this point in the history
…e Body tag

Make scrollbar size check lazy, Only called after components have
mounted. fixes react-bootstrap#849
  • Loading branch information
jquense committed Jun 18, 2015
1 parent 3423e70 commit 2594dce
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ function onFocus(context, handler) {

let scrollbarSize;

if (domUtils.canUseDom) {
function getScrollbarSize(){
if ( scrollbarSize !== undefined ){
return scrollbarSize;
}

let scrollDiv = document.createElement('div');

scrollDiv.style.position = 'absolute';
Expand All @@ -64,13 +68,13 @@ if (domUtils.canUseDom) {
scrollDiv.style.overflow = 'scroll';

document.body.appendChild(scrollDiv);

scrollbarSize = scrollDiv.offsetWidth - scrollDiv.clientWidth;

document.body.removeChild(scrollDiv);

scrollDiv = null;
}


const Modal = React.createClass({

mixins: [BootstrapMixin, FadeMixin],
Expand Down Expand Up @@ -210,7 +214,7 @@ const Modal = React.createClass({
this._originalPadding = container.style.paddingRight;

if (this._containerIsOverflowing) {
container.style.paddingRight = parseInt(this._originalPadding || 0, 10) + scrollbarSize + 'px';
container.style.paddingRight = parseInt(this._originalPadding || 0, 10) + getScrollbarSize() + 'px';
}

if (this.props.backdrop) {
Expand Down Expand Up @@ -308,8 +312,8 @@ const Modal = React.createClass({

return {
dialogStyles: {
paddingRight: containerIsOverflowing && !modalIsOverflowing ? scrollbarSize : void 0,
paddingLeft: !containerIsOverflowing && modalIsOverflowing ? scrollbarSize : void 0
paddingRight: containerIsOverflowing && !modalIsOverflowing ? getScrollbarSize() : void 0,
paddingLeft: !containerIsOverflowing && modalIsOverflowing ? getScrollbarSize() : void 0
}
};
}
Expand Down

0 comments on commit 2594dce

Please sign in to comment.