sc0ttj / component

A tiny library for isomorphic JavaScript components
MIT License
2 stars 1 forks source link

Feature: onScroll add-on #43

Closed sc0ttj closed 3 years ago

sc0ttj commented 3 years ago

Do an optional add-on which makes it easy to drive animations based on scrolling, on a per-component basis.

Usage would be something like this:

Component.onScroll = onScroll;

// place an invisible marker line, fixed to the given screen height, past which 
// all components will scroll - defaults to 40%.. The higher the %, the earlier 
// the given onScroll method(s) will be triggered.
Component.onScroll.markerLine = '50%';

const Foo = new Component({ progressBarWidth: 0 });

// Each component can have a different marker line
Foo.onScroll.markerLine = '60%';

Foo.view = props => `<div style="width: ${props.progressBarWidth}%"></div>`;

Foo.onScroll(scrollProps => {
  // do stuff here, like update a progress bar
  Foo.setState({ progressBarWidth: scrollProps.progress * 100 });
});

The scrollProps would contain the following:

sc0ttj commented 3 years ago

Done