on-scroll-to v1.0.0
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 --saveIf 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).
import OnScrollTo from 'on-scroll-to'
new OnScrollTo(
getElement,
onScrollTo,
onStateChange,
options
)getElement()function should return the DOM Element which will get theonScrollToaction 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 wheneverOnScrollToinstance's state changes.optionsis an optional argument.
Available options:
distance— Is anumberin 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 = 0
let list = []
function onScrollTo() {
return http.get('/items', { skip: page * 10, limit: 10 })
.then(({ items, hasMore }) => {
list = list.concat(items)
page++
// If there're more items to load
// then re-enable the `OnScrollTo` component.
// Otherwise, don't render the `OnScrollTo` DOM Element.
return hasMore
})
}The OnScrollTo class instance provides methods:
onMount()— Should be called when theOnScrollTocomponent is "mounted" (rendered) on a page.onUnmount()— Should be called when theOnScrollTocomponent is "unmounted" (removed) from the page.getState()— ReturnsOnScrollTostate.retry()— Can be used for manually re-enabling theOnScrollTocomponent if anerrorhappened.
OnScrollTo state provides properties:
hidden— Is set totruewhen theOnScrollTocomponent should no longer be rendered.loading— Is set totruewhen theOnScrollTocomponent is in "disabled" state (onScrollTo()returned aPromisewhich hasn't been resolved or rejected yet).error— Is present ifonScrollTo()threw anerror(or returned aPromisethat rejected with anerror).
DOM
This is an example of using on-scroll-to/dom component. It's the source code of the DOM demo.
import { OnScrollTo } from 'on-scroll-to/dom'
// Renders the "Loading more..." DOM Element.
function render(state) {
// Create `element`.
const element = document.createElement('div')
element.classList.add('load-more')
// Clear container element.
const container = document.getElementById('load-more-items')
while (container.firstChild) {
container.removeChild(container.firstChild)
}
// If there was an error then show it.
if (state.error) {
element.classList.add('load-more--error')
element.textContent = 'Error while loading more items'
}
// If there're no more items then
// `element` is removed from `document`.
else if (state.hidden) {
return
}
// Create `element`.
else {
if (state.loading) {
// May render a spinner animation.
element.textContent = 'Loading more...'
} else {
element.textContent = 'Load more'
}
}
// Insert `element` into `document`.
container.appendChild(element)
// `render()` function must return the DOM Element.
return element
}
const onScrollToComponent = new OnScrollTo(
onScrollTo,
render,
options
)
// Call `.mount()` for the initial render.
// All subsequent renders will be automatic.
onScrollToComponent.mount()
// 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 theOnScrollTocomponent.optionsis an optional argument. These are the options for the coreOnScrollToclass constructor.
OnScrollTo instance provides methods:
retry()— Can be used for manually re-enabling theOnScrollTocomponent if anerrorhappened.
React
This is an example of using the React OnScrollTo component. It's the source code of the React demo.
import React from 'react'
import PropTypes from 'prop-types'
import OnScrollTo from 'on-scroll-to/react'
function Example({ onScrollTo }) {
return (
<OnScrollTo
onScrollTo={onScrollTo}
component={LoadMoreItemsOnScroll}
)
}
Example.propTypes = {
onScrollTo: PropTypes.func.isRequired
}
function LoadMoreItemsOnScroll({
setDOMNode,
loading,
error,
retry
}) {
// May render a spinner animation.
return (
<div ref={setDOMNode} className="load-more">
{error ? 'Error while loading more items' : (loading ? 'Loading more...' : 'Load more')}
</div>
)
}
LoadMoreItemsOnScroll.propTypes = {
setDOMNode: PropTypes.func.isRequired,
loading: PropTypes.bool,
error: PropTypes.any,
retry: PropTypes.func.isRequired
}<OnScrollTo/> component receives properties:
onScrollTo()— The function that gets called when the component is scrolled to.component— A React component for theOnScrollToDOM Element.distance— (optional) Trigger distance.
component receives properties:
setDOMNode()— Should be passed asrefon the root DOM Element.loading— (optional) Will betrueifOnScrollTocomponent is in "disabled" state (onScrollTo()returned aPromisewhich hasn't been resolved or rejected yet).error— (optional) IfonScrollTo()throws anerror(or rejects with anerror) then theerrorproperty will be passed.retry()— Can be used for manually re-enabling theOnScrollTocomponent if anerrorhappened.
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. -->
<script src="https://unpkg.com/on-scroll-to@1.x/bundle/on-scroll-to.js"></script>
<script>
new OnScrollTo(...)
</script>
<!-- DOM component. -->
<script src="https://unpkg.com/on-scroll-to@1.x/bundle/on-scroll-to-dom.js"></script>
<script>
new OnScrollTo(...)
</script>
<!-- React component. -->
<script src="https://unpkg.com/on-scroll-to@1.x/bundle/on-scroll-to-react.js"></script>
<script>
<OnScrollTo .../>
</script>License
6 years ago