Skip to content

Commit

Permalink
feat(Visibility): add offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Fedyashov committed Aug 29, 2017
1 parent e1ff28a commit eb6ffb1
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/behaviors/Visibility/Visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export default class Visibility extends Component {
*/
onBottomVisibleReverse: PropTypes.func,

offset: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),

/** When set to false a callback will occur each time an element passes the threshold for a condition. */
once: PropTypes.bool,

Expand Down Expand Up @@ -144,6 +149,7 @@ export default class Visibility extends Component {
static defaultProps = {
context: isBrowser ? window : null,
continuous: false,
offset: 0,
once: true,
}

Expand Down Expand Up @@ -275,16 +281,17 @@ export default class Visibility extends Component {
handleRef = c => (this.ref = c)

handleScroll = () => {
const { offset } = this.props
const { bottom, height, top, width } = this.ref.getBoundingClientRect()

const topPassed = top < 0
const bottomPassed = bottom < 0
const topPassed = top < offset
const bottomPassed = bottom < offset

const pixelsPassed = bottomPassed ? 0 : Math.max(top * -1, 0)
const pixelsPassed = bottomPassed ? offset : Math.max(top * -1, 0)
const percentagePassed = pixelsPassed / height

const bottomVisible = bottom >= 0 && bottom <= window.innerHeight
const topVisible = top >= 0 && top <= window.innerHeight
const bottomVisible = bottom >= offset && bottom <= window.innerHeight
const topVisible = top >= offset && top <= window.innerHeight

const fits = topVisible && bottomVisible
const passing = topPassed && !bottomPassed
Expand Down

0 comments on commit eb6ffb1

Please sign in to comment.