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

fix(Visibility): handle context scroll instead of just window scroll #3400

Merged
18 changes: 15 additions & 3 deletions src/behaviors/Visibility/Visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export default class Visibility extends Component {
if (!isBrowser()) return
const { context, fireOnMount, updateOn } = this.props

this.pageYOffset = window.pageYOffset
this.pageYOffset = this.getPageYOffset()
this.attachHandlers(context, updateOn)

if (fireOnMount) this.update()
Expand Down Expand Up @@ -310,7 +310,7 @@ export default class Visibility extends Component {

this.oldCalculations = this.calculations
this.calculations = this.computeCalculations()
this.pageYOffset = window.pageYOffset
this.pageYOffset = this.getPageYOffset()

const {
onBottomPassed,
Expand Down Expand Up @@ -364,7 +364,8 @@ export default class Visibility extends Component {
const { bottom, height, top, width } = this.ref.current.getBoundingClientRect()
const [topOffset, bottomOffset] = normalizeOffset(offset)

const direction = window.pageYOffset > this.pageYOffset ? 'down' : 'up'
const newOffset = this.getPageYOffset()
const direction = newOffset > this.pageYOffset ? 'down' : 'up'
const topPassed = top < topOffset
const bottomPassed = bottom < bottomOffset

Expand Down Expand Up @@ -397,6 +398,17 @@ export default class Visibility extends Component {
}
}

getPageYOffset() {
const { context } = this.props

if (context) {
// Heads up! `window` doesn't have `pageYOffset` property
return context === window ? window.pageYOffset : context.scrollTop
}

return 0
}

// ----------------------------------------
// Render
// ----------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let info
let warn
let error

const throwOnConsole = method => (...args) => {
const throwOnConsole = (method) => (...args) => {
throw new Error(
`console.${method} should never be called but was called with:\n${args.join(' ')}`,
)
Expand Down
20 changes: 17 additions & 3 deletions test/specs/behaviors/Visibility/Visibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,20 @@ describe('Visibility', () => {
calculations: { direction: 'up' },
})
})

it('gets direction from `context` element', () => {
const context = document.createElement('div')
const onUpdate = sandbox.spy()

sandbox.stub(context, 'scrollTop').value(0)
mount(<Visibility context={context} onUpdate={onUpdate} />)

sandbox.stub(context, 'scrollTop').value(5)
domEvent.scroll(context)
onUpdate.should.have.been.calledWithMatch(null, {
calculations: { direction: 'down' },
})
})
})
})

Expand Down Expand Up @@ -367,8 +381,8 @@ describe('Visibility', () => {
const opts = { [callbackName]: callback }

const offset = 10
const falsyCond = _.map(_.first(falsy), value => value + offset)
const truthyCond = _.map(_.first(truthy), value => value + offset)
const falsyCond = _.map(_.first(falsy), (value) => value + offset)
const truthyCond = _.map(_.first(truthy), (value) => value + offset)

wrapperMount(<Visibility {...opts} offset={offset} />)
mockScroll(...truthyCond)
Expand Down Expand Up @@ -546,7 +560,7 @@ describe('Visibility', () => {
describe('updateOn', () => {
beforeEach(() => {
requestAnimationFrame.restore()
sandbox.stub(window, 'requestAnimationFrame').callsFake(fn => setTimeout(() => fn(), 0))
sandbox.stub(window, 'requestAnimationFrame').callsFake((fn) => setTimeout(() => fn(), 0))
})

it('defaults to "events"', () => {
Expand Down