@render-props/scrollable v0.1.20
Scrollable
A state container which provides an interface for listening to the scroll event of its child component and providing valuable data about direction, distance, and more. It also provides convenience functions for scrollTo with optional animation.
Installation
yarn add @render-props/scrollable or npm i @render-props/scrollable
Usage
import Scrollable from '@render-props/scrollable'
const ScrollableBox = props => (
<Scrollable>
{
({
scrollRef,
scrollToX,
scrollToY,
scrollTo,
scrollHeight,
scrollWidth,
scrollY,
scrollX,
clientHeight,
clientWidth,
max,
direction,
distance,
}) => (
<div
ref={scrollRef}
style={{
width: 300,
height: 300,
overflow: 'auto',
background: '#000'
}}
>
<div style={{width: 600, height: 16000, position: 'relative'}}>
<pre
onClick={() => {
scrollTo(250, 500, {timing: p => p})
}}
style={{
position: 'absolute',
top: scrollY + 10,
left: scrollX + 10
}}
>
{JSON.stringify({
scrollRef,
scrollToX,
scrollToY,
scrollTo,
scrollHeight,
scrollWidth,
scrollY,
scrollX,
clientHeight,
clientWidth,
max,
direction,
distance,
}, null, 2)}
</pre>
</div>
</div>
)
}
</Scrollable>
)Props
initialX {integer}- initial position for the
xcoordinate
- initial position for the
initialY {integer}- initial position for the
ycoordinate
- initial position for the
onScroll {function (state <object>)}- called each time the scroll position updates, accepts one argument for
state
- called each time the scroll position updates, accepts one argument for
Render Props
Ref
scrollRef- This
refmust be provided to whatever element you are trying to observe the the scroll behavior of. e.g.<div ref={scrollRef}>
- This
Methods
scrollTo(x <integer>, y<integer>, [options <object{duration <ms>, timing <function>}>])scrolls to the provided
x,ycoordinates in the container. You can optionally animate this by providing an options object with a duration. By default the timing function is linear, but you could for example use this bezier-easing library: https://github.com/gre/bezier-easingconst bezierCurve = BezierEasing(0, 0, 1, 0.5); scrollTo(0, 250, {timing: bezierCurve}) const cubicIn = x => x * x * x scrollTo(0, 250, {timing: cubicIn, duration: 400})
scrollToY(y, [options <object{duration <ms>, timing <function>}>])- scrolls to the provided
ycoordinate in the container
- scrolls to the provided
scrollToX(x, [options <object{duration <ms>, timing <function>}>])- scrolls to the provided
xcoordinate in the container
- scrolls to the provided
State
scrollX {integer}- the horizontal scroll position in the container from the left edge
scrollY {integer}- the vertical scroll position in the container from the top edge
scrollWidth {integer}- the width of the scrollable area
scrollHeight {integer}- the height of the scrollable area
clientWidth {integer}- client width of the container
clientHeight {integer}- client height of the container
max {object: {x <integer>, y <integer>}}- the maximum possible scroll position in the container along both
xandycoordinates
- the maximum possible scroll position in the container along both
direction {object: {x <integer>, y <integer>}}- the direction the container was just scrolled.
1=rightforx,downfory-1=leftforx,upfory0= had no direction
- the direction the container was just scrolled.
distance {object: {x <integer>, y <integer>}}- the distance between the latest recorded scroll activity in the frame and the previous scroll activity
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago