-
Notifications
You must be signed in to change notification settings - Fork 209
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
Feature/bounds #109
Feature/bounds #109
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import React, { PropTypes } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
const POSITIONS = { | ||
above: 'above', | ||
|
@@ -54,6 +53,12 @@ function debugLog() { | |
* Calls a function when you scroll to the element. | ||
*/ | ||
export default class Waypoint extends React.Component { | ||
constructor() { | ||
super(); | ||
|
||
this.refElement = (e) => this._ref = e; | ||
} | ||
|
||
componentWillMount() { | ||
if (this.props.scrollableParent) { // eslint-disable-line react/prop-types | ||
throw new Error('The `scrollableParent` prop has changed name ' + | ||
|
@@ -113,7 +118,7 @@ export default class Waypoint extends React.Component { | |
return this.props.scrollableAncestor; | ||
} | ||
|
||
let node = ReactDOM.findDOMNode(this); | ||
let node = this._ref; | ||
|
||
while (node.parentNode) { | ||
node = node.parentNode; | ||
|
@@ -264,16 +269,15 @@ export default class Waypoint extends React.Component { | |
} | ||
|
||
_getBounds() { | ||
const waypointTop = ReactDOM.findDOMNode(this).getBoundingClientRect().top; | ||
const waypointTop = this._ref.getBoundingClientRect().top; | ||
let contextHeight; | ||
let contextScrollTop; | ||
if (this.scrollableAncestor === window) { | ||
contextHeight = window.innerHeight; | ||
contextScrollTop = 0; | ||
} else { | ||
contextHeight = this.scrollableAncestor.offsetHeight; | ||
contextScrollTop = ReactDOM | ||
.findDOMNode(this.scrollableAncestor) | ||
contextScrollTop = this.scrollableAncestor | ||
.getBoundingClientRect().top; | ||
} | ||
if (this.props.debug) { | ||
|
@@ -328,7 +332,7 @@ export default class Waypoint extends React.Component { | |
render() { | ||
// We need an element that we can locate in the DOM to determine where it is | ||
// rendered relative to the top of its context. | ||
return <span style={{fontSize: 0}} />; | ||
return <span ref={this.refElement} style={{fontSize: 0}} />; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for not using an anonymous function to handle the ref setting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine, but I'm not sure it really makes much difference either way. My preference would be to use the arrow function here to reduce unnecessary indirection. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've never been able to prove from looking at the react source that an anonymous function on eslint if configured to prohibit anonymous functions in |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth noting that this would be a breaking change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand how this changes anything.
this.scrollableAncestor
is a dom element soReactDOM.findDOMNode(this.scrollableAncestor) === this.scrollableAncestor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're right. I was thinking that passing a component instance in as a
scrollableAncestor
would work (by leveragingfindDOMNode
), but I took another look at the source and saw that it would error earlier on in the code.Ignore me! 🙈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!