Skip to content
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

Avoid body scrolling when dialog open #19

Merged
merged 3 commits into from
Mar 17, 2016
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
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