Skip to content

Commit

Permalink
Merge pull request #19 from yesmeck/avoid-body-scrolling
Browse files Browse the repository at this point in the history
Avoid body scrolling when dialog open
  • Loading branch information
yiminghe committed Mar 17, 2016
2 parents 8213f60 + 594d90e commit 8e34b22
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 11 deletions.
3 changes: 2 additions & 1 deletion assets/bootstrap.less
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@import "./bootstrap/body.less";
@import "./bootstrap/Dialog.less";
@import "./bootstrap/Mask.less";
@import "./bootstrap/Mask.less";
3 changes: 2 additions & 1 deletion assets/bootstrap/Mask.less
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
opacity: .5;
z-index: 1000;
filter: alpha(opacity=50);
}
overflow: auto;
}
3 changes: 3 additions & 0 deletions assets/bootstrap/body.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.rc-dialog-scrolling {
overflow: hidden;
}
3 changes: 2 additions & 1 deletion assets/index.less
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@prefixCls: rc-dialog;

@import "./index/body.less";
@import "./index/Dialog.less";
@import "./index/Mask.less";
@import "./index/Mask.less";
1 change: 1 addition & 0 deletions assets/index/Mask.less
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
height: 100%;
z-index: 1000;
filter: alpha(opacity=50);
overflow: auto;

&-hidden {
display: none;
Expand Down
5 changes: 5 additions & 0 deletions assets/index/body.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.@{prefixCls} {
&-scrolling {
overflow: hidden;
}
}
2 changes: 1 addition & 1 deletion src/DOMWrap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const DOMWrap = React.createClass({
props.className += ' ' + props.hiddenClassName;
}
const Tag = props.tag;
return <Tag {...props} />;
return <Tag {...props}>{props.children}</Tag>;
},
});

Expand Down
33 changes: 26 additions & 7 deletions src/Dialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const Dialog = React.createClass({
// first show
if (!prevProps.visible) {
this.lastOutSideFocusNode = document.activeElement;
this.addScrollingClass();
ReactDOM.findDOMNode(this.refs.dialog).focus();
}
} else if (prevProps.visible) {
Expand All @@ -80,6 +81,7 @@ const Dialog = React.createClass({
this.lastOutSideFocusNode = null;
}
this.lastOutSideFocusNode = null;
this.removeScrollingClass();
}
}
},
Expand All @@ -89,10 +91,12 @@ const Dialog = React.createClass({
},

onMaskClick(e) {
if (this.props.closable && this.props.maskClosable) {
this.close(e);
if (e.target === e.currentTarget) {
if (this.props.closable && this.props.maskClosable) {
this.close(e);
}
ReactDOM.findDOMNode(this.refs.dialog).focus();
}
ReactDOM.findDOMNode(this.refs.dialog).focus();
},

onKeyDown(e) {
Expand Down Expand Up @@ -207,7 +211,7 @@ const Dialog = React.createClass({
</Animate>);
},

getMaskElement() {
getMaskElement(dialog) {
const props = this.props;
const maskProps = {
onClick: this.onMaskClick,
Expand All @@ -221,8 +225,10 @@ const Dialog = React.createClass({
const maskTransition = this.getMaskTransitionName();
maskElement = (<DOMWrap {...maskProps} key="mask"
className={`${props.prefixCls}-mask`}
visible={props.visible}
hiddenClassName={`${props.prefixCls}-mask-hidden`} />);
visible={props.visible}
hiddenClassName={`${props.prefixCls}-mask-hidden`}>
{dialog}
</DOMWrap>);
if (maskTransition) {
maskElement = (<Animate key="mask" showProp="visible"
transitionAppear component=""
Expand Down Expand Up @@ -256,6 +262,19 @@ const Dialog = React.createClass({
return this.refs[part];
},

addScrollingClass() {
const props = this.props;
const scrollingClassName = `${props.prefixCls}-scrolling`;
document.body.className += ` ${scrollingClassName}`;
},

removeScrollingClass() {
const props = this.props;
const scrollingClassName = `${props.prefixCls}-scrolling`;
const body = document.body;
body.className = body.className.replace(scrollingClassName, '');
},

close(e) {
this.props.onClose(e);
},
Expand All @@ -268,7 +287,7 @@ const Dialog = React.createClass({
};

return (<div className={classNames(className)} ref="root">
{[this.getMaskElement(), this.getDialogElement()]}
{this.getMaskElement(this.getDialogElement())}
</div>);
},
});
Expand Down

0 comments on commit 8e34b22

Please sign in to comment.