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

Smoother stickyOnScroll...? #126

Open
mo-ib opened this issue Oct 17, 2017 · 0 comments
Open

Smoother stickyOnScroll...? #126

mo-ib opened this issue Oct 17, 2017 · 0 comments

Comments

@mo-ib
Copy link

mo-ib commented Oct 17, 2017

This way the wrapper spaces out the height of the inner component, so there's no snapping:

onScroll() {
      const {scrollY} = window
      const {height, sticky} = this.state
      const {clientHeight} = this.componentWrap
      const scrolledPastThreshold = scrollY >= scrollThreshold

      if (height === 0 && clientHeight > 0) {
        this.setState({height: clientHeight})
      }

      if (scrolledPastThreshold && !sticky) {
        this.setState({sticky: true})
      } else if (!scrolledPastThreshold && sticky) {
        this.setState({sticky: false})
      }
    }

    render() {
      const {height, sticky} = this.state
      const wrapProps = {
        className: 'sticky-wrap',
        ref: c => { this.componentWrap = c },
        style: {},
      }

      if (height > 0) {
        wrapProps.style.height = height
      }

      return (
        <div {...wrapProps}>
          <InnerComponent
            {...this.props}
            ref={c => { this.innerComponent = c }}
            height={height}
            sticky={sticky}
          />
        </div>
      )
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant