1.0.2 • Published 6 years ago

scroll-creep v1.0.2

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

scroll-spy

Travis npm package Coveralls

A super simple scroll-spy component for React.

Usage

Supply the ids of elements you want to monitor while scrolling. The render callback provides the ids of the elements currently in view. Use that to style your navigation bar however you'd like.

import ScrollSpy from 'scroll-spy';

const elementIds = ['a', 'b', 'c']

const ScrollExample = () => (
  <div>
    <ScrollSpy elements={elementIds}>
      {(idsInView) => (
        <nav>
          {elementIds.map(id => (
            <div key={id} style={{ fontWeight: idsInView.includes(id) ? 'bold' : 'normal' }}>
              {id}
            </div>
          ))}
        </nav>
      )}
    </ScrollSpy>
    <main>
    {elementIds.map(id => (
      <div key={id}>{id}</div>
    ))}
    </main>
  </div>
)
PropsTypeDescription
match'first', 'last', 'all'match either the first, last, or all visible elements. default: all
elementsArrayAn array of ids of elements to monitor.
children(Array, string) => React.NodeA render callback containing elements visible.
scrollContainerquery stringDefaults to window.