1.0.0-beta.0 • Published 6 years ago
react-scroll-composer v1.0.0-beta.0
React Scroll Composer
Installation
npm install react-scroll-composerConcept
A scroll is composed of three steps:
- Trigger: an
Eventor a custom trigger. - Middleware: Middlewares are assembled into a pipeline. Each middleware receives the delta from the previous one, and returns a new delta.
- Scroll
If you keep the native scroll, the delta will be ignored, because the scroll is controled by the browser. But you can still use middlewares to add some scroll effects.
API
Provider
import { Provider } from 'react-scroll-composer'
const App = () => <Provider native={false}>{/* ... */}</Provider>useComposer
import { useComposer } from 'react-scroll-composer'
import { key, touch, wheel } from 'react-scroll-composer/lib/middlewares'
const Component = () => {
const trigger = useComposer([
key({ order: 0 }),
touch({ order: 1 }),
wheel({ order: 2 }),
])
useEffect(() => {
trigger(/* ... */)
}, [])
}The argument of useComposer can be undefined, one middleware, or an array of middlewares.
The value passed to trigger will be consumed by the middlewares. If the scroll is native, this will NOT scroll the page.
Middleware
const middleware = {
index: 0,
callback: ({ value, trigger, delta }) => {
// ...
return { x, y }
},
}index defines the position of the middleware in the pipeline.
In callback:
valueis the current scroll position, which has propertiesxandy.triggercan be an event of typekeydown,touchstart,touchmove,touchend,touchcancel, orwheel. It can also be an object provided intrigger.deltahas propertiesxandy.
React Scroll Composer provides three middlewares to handle the three types of events:
key:KeyboardEventtouch:TouchEventwheel:WheelEvent
1.0.0-beta.0
6 years ago