OnScrollTo
A component that triggers an action when it's scrolled to.
Can be used to render a "Load more items on scroll down" component for "infinite scroll" lists.
Demo
Install
npm install on-scroll-to --save
If you're not using a bundler then use a standalone version from a CDN.
Use
The default export is the OnScrollTo
class. It implements the core logic and can be used for building an OnScrollTo
component for any UI framework. on-scroll-to/dom
and on-scroll-to/react
are both built upon it so this OnScrollTo
utility class is a low-level core and is meant to be used by UI framework library authors and not by the end users — the end users should use high-level components like on-scroll-to/dom
(for pure JS) and on-scroll-to/react
(for React).
getElement onScrollTo onStateChange options
getElement()
function should return the DOM Element which will get theonScrollTo
action triggered when it's scrolled to.onScrollTo()
function is the action that will get triggered when the DOM Element is scrolled to.onStateChange(state)
function gets called wheneverOnScrollTo
instance's state changes.options
is an optional argument.
Available options
:
distance
— Is anumber
in pixels and is screen height by default meaning thatonScrollTo()
function will get called as soon as the user scrolls down to the component's Y position minus screen height. Can be used to perform some action ahead of time before the user scrolls down to the bottom. For example, to load next list items before the user scrolls down to the end of the currently shown list items.
When OnScrollTo
DOM Element is scrolled to the OnScrollTo
component is "deactivated" and onScrollTo()
function is called. The onScrollTo()
function must return either true
(or a Promise
resolving to true
) to re-enable the OnScrollTo
component or false
(or a Promise
resolving to false
) if the OnScrollTo
component is no longer needed on the page in which case it disappears (returns nothing when rendered).
The onScrollTo()
function could look like this:
let page = 0let list = { return http }
The OnScrollTo
class instance provides methods:
onMount()
— Should be called when theOnScrollTo
component is "mounted" (rendered) on a page.onUnmount()
— Should be called when theOnScrollTo
component is "unmounted" (removed) from the page.getState()
— ReturnsOnScrollTo
state.retry()
— Can be used for manually re-enabling theOnScrollTo
component if anerror
happened.
OnScrollTo
state provides properties:
hidden
— Is set totrue
when theOnScrollTo
component should no longer be rendered.loading
— Is set totrue
when theOnScrollTo
component is in "disabled" state (onScrollTo()
returned aPromise
which hasn't been resolved or rejected yet).error
— Is present ifonScrollTo()
threw anerror
(or returned aPromise
that rejected with anerror
).
DOM
This is an example of using on-scroll-to/dom
component. It's the source code of the DOM demo.
// Renders the "Loading more..." DOM Element. { // Create `element`. const element = document elementclassList // Clear container element. const container = document while containerfirstChild container // If there was an error then show it. if stateerror elementclassList elementtextContent = 'Error while loading more items' // If there're no more items then // `element` is removed from `document`. else if statehidden return // Create `element`. else if stateloading // May render a spinner animation. elementtextContent = 'Loading more...' else elementtextContent = 'Load more' // Insert `element` into `document`. container // `render()` function must return the DOM Element. return element} const onScrollToComponent = onScrollTo render options // Call `.mount()` for the initial render.// All subsequent renders will be automatic.onScrollToComponent // For "Single Page Apps":// router.onPageUnload(onScrollToComponent.onUnmount)
OnScrollTo
class constructor receives arguments:
onScrollTo()
function is the action that will get triggered when the DOM Element is scrolled to.render(state)
function renders the DOM Element for theOnScrollTo
component.options
is an optional argument. These are the options for the coreOnScrollTo
class constructor.
OnScrollTo
instance provides methods:
retry()
— Can be used for manually re-enabling theOnScrollTo
component if anerror
happened.
React
This is an example of using the React OnScrollTo
component. It's the source code of the React demo.
{ return <OnScrollTo onScrollTo=onScrollTo component=LoadMoreItemsOnScroll } ExamplepropTypes = onScrollTo: PropTypesfuncisRequired { // May render a spinner animation. return <div ref=setDOMNode className="load-more"> error ? 'Error while loading more items' : loading ? 'Loading more...' : 'Load more' </div> } LoadMoreItemsOnScrollpropTypes = setDOMNode: PropTypesfuncisRequired loading: PropTypesbool error: PropTypesany retry: PropTypesfuncisRequired
<OnScrollTo/>
component receives properties:
onScrollTo()
— The function that gets called when the component is scrolled to.component
— A React component for theOnScrollTo
DOM Element.distance
— (optional) Trigger distance.
component
receives properties:
setDOMNode()
— Should be passed asref
on the root DOM Element.loading
— (optional) Will betrue
ifOnScrollTo
component is in "disabled" state (onScrollTo()
returned aPromise
which hasn't been resolved or rejected yet).error
— (optional) IfonScrollTo()
throws anerror
(or rejects with anerror
) then theerror
property will be passed.retry()
— Can be used for manually re-enabling theOnScrollTo
component if anerror
happened.
Debug
Set window.OnScrollToDebug
to true
to output debug messages to console
.
CDN
One can use any npm CDN service, e.g. unpkg.com or jsdelivr.net
<!-- Core. --> <!-- DOM component. --> <!-- React component. -->