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): add scroll context #1978

Merged
merged 4 commits into from
Aug 20, 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
3 changes: 3 additions & 0 deletions src/behaviors/Visibility/Visibility.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export interface VisibilityProps {
/** Primary content. */
children?: React.ReactNode;

/** Context which sticky element should stick to. */
context?: object;
Copy link
Member

@levithomason levithomason Aug 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd assume the description here would match the Visibility component:

/** The scroll context visibility should use. */


/**
* When set to true a callback will occur anytime an element passes a condition not just immediately after the
* threshold is met.
Expand Down
10 changes: 8 additions & 2 deletions src/behaviors/Visibility/Visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export default class Visibility extends Component {
/** Primary content. */
children: PropTypes.node,

/** Context which visibility should attach onscroll events. */
context: PropTypes.object,

/**
* When set to true a callback will occur anytime an element passes a condition not just immediately after the
* threshold is met.
Expand Down Expand Up @@ -138,6 +141,7 @@ export default class Visibility extends Component {
}

static defaultProps = {
context: window,
continuous: false,
once: true,
}
Expand Down Expand Up @@ -166,11 +170,13 @@ export default class Visibility extends Component {
}

componentDidMount() {
window.addEventListener('scroll', this.handleScroll)
const { context } = this.props
context.addEventListener('scroll', this.handleScroll)
}

componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll)
const { context } = this.props
context.removeEventListener('scroll', this.handleScroll)
}

execute = (callback, name) => {
Expand Down
3 changes: 3 additions & 0 deletions src/modules/Sticky/Sticky.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export interface StickyProps {

/** Whether element should be "pushed" by the viewport, attaching to the bottom of the screen when scrolling up. */
pushing?: boolean;

/** Context which sticky should attach onscroll events. */
scrollContext?: object;
}

declare const Sticky: React.ComponentClass<StickyProps>;
Expand Down
10 changes: 8 additions & 2 deletions src/modules/Sticky/Sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ export default class Sticky extends Component {

/** Whether element should be "pushed" by the viewport, attaching to the bottom of the screen when scrolling up. */
pushing: PropTypes.bool,

/** Context which sticky should attach onscroll events. */
scrollContext: PropTypes.object,
}

static defaultProps = {
bottomOffset: 0,
offset: 0,
scrollContext: window,
}

static _meta = {
Expand All @@ -83,12 +87,14 @@ export default class Sticky extends Component {
}

componentDidMount() {
const { scrollContext } = this.props
this.handleUpdate()
window.addEventListener('scroll', this.handleUpdate)
scrollContext.addEventListener('scroll', this.handleUpdate)
}

componentWillUnmount() {
window.removeEventListener('scroll', this.handleUpdate)
const { scrollContext } = this.props
scrollContext.removeEventListener('scroll', this.handleUpdate)
}

// ----------------------------------------
Expand Down
17 changes: 17 additions & 0 deletions test/specs/behaviors/Visibility/Visibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ describe('Visibility', () => {
if (wrapper && wrapper.unmount) wrapper.unmount()
})

it('should use window as default scroll context', () => {
const onUpdate = sandbox.spy()
mount(<Visibility onUpdate={onUpdate} />)
window.dispatchEvent(new Event('scroll'))
onUpdate.should.have.been.called()
})

it('should set a scroll context', () => {
const div = document.createElement('div')
const onUpdate = sandbox.spy()
mount(<Visibility onUpdate={onUpdate} context={div} />)
window.dispatchEvent(new Event('scroll'))
onUpdate.should.not.have.been.called()
div.dispatchEvent(new Event('scroll'))
onUpdate.should.have.been.called()
})

describe('calculations', () => {
expectations.forEach((expectation) => {
it(`calculates ${expectation.name}`, () => {
Expand Down
21 changes: 21 additions & 0 deletions test/specs/modules/Sticky/Sticky-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ describe('Sticky', () => {
window.requestAnimationFrame = requestAnimationFrame
})

it('should use window as default scroll context', () => {
const onStick = sandbox.spy()
const wrapper = mount(<Sticky onStick={onStick} />)
const instance = wrapper.instance()
instance.triggerRef = { getBoundingClientRect: () => ({ top: -1 }) }
window.dispatchEvent(new Event('scroll'))
onStick.should.have.been.called()
})

it('should set a scroll context', () => {
const div = document.createElement('div')
const onStick = sandbox.spy()
const wrapper = mount(<Sticky scrollContext={div} onStick={onStick} />)
const instance = wrapper.instance()
instance.triggerRef = { getBoundingClientRect: () => ({ top: -1 }) }
window.dispatchEvent(new Event('scroll'))
onStick.should.not.have.been.called()
div.dispatchEvent(new Event('scroll'))
onStick.should.have.been.called()
})

it('should create two divs', () => {
const children = shallow(<Sticky />).children()

Expand Down