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

feat(Sticky|Visibility): listen for resize events #2091

Merged
merged 4 commits into from
Sep 23, 2017
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
8 changes: 5 additions & 3 deletions src/behaviors/Visibility/Visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,17 @@ export default class Visibility extends Component {
const { context, fireOnMount } = this.props

this.pageYOffset = window.pageYOffset
eventStack.sub('scroll', this.handleScroll, { target: context })
eventStack.sub('resize', this.handleUpdate, { target: context })
eventStack.sub('scroll', this.handleUpdate, { target: context })

if (fireOnMount) this.update()
}

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

eventStack.unsub('scroll', this.handleScroll, { target: context })
eventStack.unsub('resize', this.handleUpdate, { target: context })
eventStack.unsub('scroll', this.handleUpdate, { target: context })
}

// ----------------------------------------
Expand Down Expand Up @@ -261,7 +263,7 @@ export default class Visibility extends Component {
})
}

handleScroll = () => {
handleUpdate = () => {
if (this.ticking) return

this.ticking = true
Expand Down
2 changes: 2 additions & 0 deletions src/modules/Sticky/Sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ export default class Sticky extends Component {
addListener = () => {
const { scrollContext } = this.props

eventStack.sub('resize', this.handleUpdate, { target: scrollContext })
eventStack.sub('scroll', this.handleUpdate, { target: scrollContext })
}

removeListener = () => {
const { scrollContext } = this.props

eventStack.unsub('resize', this.handleUpdate, { target: scrollContext })
eventStack.unsub('scroll', this.handleUpdate, { target: scrollContext })
}

Expand Down
8 changes: 8 additions & 0 deletions test/specs/behaviors/Visibility/Visibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ describe('Visibility', () => {
onUpdate.should.have.been.calledTwice()
})

it('fires when window resized', () => {
const onUpdate = sandbox.spy()
wrapperMount(<Visibility onUpdate={onUpdate} />)

domEvent.resize(window)
onUpdate.should.have.been.calledOnce()
})

it('passes calculations to onUpdate', () => {
let calculations
const onUpdate = (e, props) => (calculations = props.calculations)
Expand Down
18 changes: 18 additions & 0 deletions test/specs/modules/Sticky/Sticky-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,22 @@ describe('Sticky', () => {
onStick.should.have.been.called()
})
})

describe('update', () => {
it('is called on scroll', () => {
const instance = mount(<Sticky />).instance()
const update = sandbox.spy(instance, 'update')

domEvent.scroll(window)
update.should.have.been.calledOnce()
})

it('is called on resize', () => {
const instance = mount(<Sticky />).instance()
const update = sandbox.spy(instance, 'update')

domEvent.resize(window)
update.should.have.been.calledOnce()
})
})
})
9 changes: 9 additions & 0 deletions test/utils/domEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ export const mouseOver = (node, data) => fire(node, 'mouseover', data)
*/
export const mouseUp = (node, data) => fire(node, 'mouseup', data)

/**
* Dispatch a 'resize' event on a DOM node.
* @param {String|Object} node A querySelector string or DOM node.
* @param {Object} [data] Additional event data.
* @returns {Object} The event
*/
export const resize = (node, data) => fire(node, 'resize', data)

/**
* Dispatch a 'scroll' event on a DOM node.
* @param {String|Object} node A querySelector string or DOM node.
Expand All @@ -82,5 +90,6 @@ export default {
mouseLeave,
mouseOver,
mouseUp,
resize,
scroll,
}