Skip to content

Commit

Permalink
Merge pull request #24 from syastrebov/feature/iframe-support
Browse files Browse the repository at this point in the history
Feature/iframe support
  • Loading branch information
souhe committed Feb 2, 2016
2 parents a1a935c + d890556 commit 9cbb2a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ Inline styles applied to vertical scrollbar's container.
#### verticalScrollbarStyle
Inline styles applied to vertical scrollbar.

#### contentWindow
You can override window to make scrollarea works inside iframe.
**Default: window**

#### ownerDocument
You can override document to make scrollarea works inside iframe.
**Default: document**

### Context
In context of each `<ScrollArea>` child could be injected an object `scrollArea` contains method:

Expand Down
15 changes: 11 additions & 4 deletions src/js/scrollArea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,30 @@ export default class ScrollArea extends React.Component{
}

componentDidMount(){
window.addEventListener("resize", this.bindedHandleWindowResize);
this.props.contentWindow.addEventListener("resize", this.bindedHandleWindowResize);
this.lineHeightPx = lineHeight(findDOMNode(this.refs.content));
this.setSizesToState();
}

componentWillUnmount(){
window.removeEventListener("resize", this.bindedHandleWindowResize);
this.props.contentWindow.removeEventListener("resize", this.bindedHandleWindowResize);
}

componentDidUpdate(){
this.setSizesToState();
}

render(){
let {children, className, contentClassName} = this.props
let {children, className, contentClassName, ownerDocument} = this.props;

var contentStyle = {
marginTop: this.state.topPosition,
marginLeft: this.state.leftPosition
};

var scrollbarY = this.canScrollY()? (
<Scrollbar
ownerDocument={ownerDocument}
realSize={this.state.realHeight}
containerSize={this.state.containerHeight}
position={-this.state.topPosition}
Expand All @@ -83,6 +85,7 @@ export default class ScrollArea extends React.Component{

var scrollbarX = this.canScrollX()? (
<Scrollbar
ownerDocument={ownerDocument}
realSize={this.state.realWidth}
containerSize={this.state.containerWidth}
position={-this.state.leftPosition}
Expand Down Expand Up @@ -306,10 +309,14 @@ ScrollArea.propTypes = {
horizontal: React.PropTypes.bool,
horizontalContainerStyle: React.PropTypes.object,
horizontalScrollbarStyle: React.PropTypes.object,
contentWindow: React.PropTypes.any,
ownerDocument: React.PropTypes.any
};

ScrollArea.defaultProps = {
speed: 1,
vertical: true,
horizontal: true
horizontal: true,
contentWindow: window,
ownerDocument: document
};
14 changes: 8 additions & 6 deletions src/js/scrollBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ class ScrollBar extends React.Component {
}

componentDidMount(){
document.addEventListener("mousemove", this.bindedHandleMouseMove);
document.addEventListener("mouseup", this.bindedHandleMouseUp);
this.props.ownerDocument.addEventListener("mousemove", this.bindedHandleMouseMove);
this.props.ownerDocument.addEventListener("mouseup", this.bindedHandleMouseUp);
}

componentWillReceiveProps(nextProps){
this.setState(this.calculateState(nextProps));
}

componentWillUnmount(){
document.removeEventListener("mousemove", this.bindedHandleMouseMove);
document.removeEventListener("mouseup", this.bindedHandleMouseUp);
this.props.ownerDocument.removeEventListener("mousemove", this.bindedHandleMouseMove);
this.props.ownerDocument.removeEventListener("mouseup", this.bindedHandleMouseUp);
}

calculateState(props){
Expand Down Expand Up @@ -118,10 +118,12 @@ ScrollBar.propTypes = {
position: React.PropTypes.number,
containerStyle: React.PropTypes.object,
scrollbarStyle: React.PropTypes.object,
type: React.PropTypes.oneOf(['vertical', 'horizontal'])
type: React.PropTypes.oneOf(['vertical', 'horizontal']),
ownerDocument: React.PropTypes.any
};

ScrollBar.defaultProps = {
type : 'vertical'
type : 'vertical',
ownerDocument: document
}
export default ScrollBar;

0 comments on commit 9cbb2a1

Please sign in to comment.