Scrolling
Cross-browser scroll hooks to manage scrolling between react elements.
Features
- Cross Browser
- Scroll Inside Container
- Scroll Entire Document
- Simple Hook Based API
- Efficient
Hooks
- useScroll: A hook to manage the scrolling between elements.
- useScrollSequence: Includes ref management on top of
useScroll
. Use thecreateScrollRef
function returned by the hook to quickly attach refs to a sequence of elements, then usenext
,previous
andgoToPosition
methods to scroll between them.
useScroll Hook
The useScroll
hook can be used to scroll an entire document or within a container.
Usage
; const Component = { const containerRef = React; const elementRef = React; const scrollSpeed = 50; const scrollToElement scrollToY = return <div ref=containerRef style= position: 'relative' overflow: 'scroll' > <button onClick= ... <p ref=elementRef></p> </div> }
Note: The container in which scrolling occurs requires the following CSS properties to work as expected:
Props
The hook takes an object as a parameter with the following properties:
Parameter | Type | Default | Description |
---|---|---|---|
scrollSpeed | number | 40 | The speed at which scrolling occurs |
containerRef | Ref | document.documentElement | A ref to the container. If a ref is not provided then it scrolls the entire document. |
Returns
Property | Type | Description |
---|---|---|
scrollToElement | (ref: MutableRefObject<.HTMLElement>, verticalOffset: number) => undefined | Use this method to scroll to an element with a given ref leaving verticalOffset pixels between element and the top of the container/document. |
scrollToY | (y: number) => undefined | Use this method to scroll to a y position in the container/document. |
You can destructure these properties as follows:
const scrollToElement scrollToY = ;
useScrollSequence Hook
The useScrollSequence
hook can be used to scroll through a sequence of elements using methods such as next
and previous
.
You can scroll within a container or the entire document.
Usage
; const Component = { const containerRef = React; const createScrollRef next previous goToPosition active = ; return <div ref=containerRef style= position: 'relative' overflow: 'scroll' > <button onClick= >next</button> <button onClick= >previous</button> <button onClick= >Go To Position 2</button> ... <p ...>Position 0</p> <p ...>Position 1</p> <p ...>Position 2</p> </div> }
Note: The container in which scrolling occurs requires the following CSS properties to work as expected:
Props
The hook takes an object as a parameter with the following properties:
Parameter | Type | Default | Description |
---|---|---|---|
initialActive | number | -1 | The index of the initial active element. This will scroll to the element on first render. |
verticalOffset | number | 0 | The space between the top of the container/document and the element being scrolled to. |
scrollSpeed | number | 40 | The speed at which scrolling occurs. |
containerRef | Ref | document.documentElement | A ref to the container. If a ref is not provided then it scrolls the entire document. |
Returns
Property | Type | Description |
---|---|---|
createScrollRef | () => {ref: MutableRefObject<.HTMLElement>, index: number} | This method returns a ref to attach to an element. And the index the element will be in the scroll order. |
next | () => undefined | Scrolls to the next element in the sequence. |
previous | () => undefined | Scrolls to the previous element in the sequence. |
goToPosition | (index: number) => undefined | Scrolls to the element at the given index in the sequence. |
active | number | The index of the currently active element. |
You can destructure these properties as follows:
const createScrollRef next previous goToPosition active = ;