Pull To Refresh
npm install @dockethealth/pulltorefreshjs


A small, but powerful Javascript library crafted to power your webapp's pull to refresh feature. No markup needed, highly customizable and dependency-free!
---
If you found this project useful, please consider making a donation.

Download PulltoRefresh either from the NPM Registry, CDNJS or UNPKG:
``bash`
$ npm install pulltorefreshjs --save-dev
$ wget -O pulltorefresh.js https://unpkg.com/pulltorefreshjs
Include the JS file in your webapp and initialize it:
`js`
const ptr = PullToRefresh.init({
mainElement: 'body',
onRefresh() {
window.location.reload();
}
});
Bundlers can consume pulltorefreshjs as CommonJS and ES6-modules syntax:
`js`
import PullToRefresh from 'pulltorefreshjs';
// or
const PullToRefresh = require('pulltorefreshjs');
API
---
- init(options)
Will return a unique ptr-instance with a destroy() method.destroyAll()
- setPassiveMode(isPassive)
Stop and remove all registered ptr-instances.
-
Enable or disable passive mode for event handlers (new instances only).
- distThreshold (integer)
Minimum distance required to trigger the refresh.
— Defaults to 60distReload
- (integer)distThreshold
After the is reached and released, the element will have this height.50
— Defaults to distIgnore
- (integer)0
After which distance should we start pulling?
— Defaults to mainElement
- (string)body
Before which element the pull to refresh elements will be?
— Defaults to triggerElement
- (string)body
Which element should trigger the pull to refresh?
— Defaults to ptrElement
- (string).ptr
Which class will the main element have?
— Defaults to classPrefix
- (string)ptr--
Which class prefix for the elements?
— Defaults to cssProp
- (string)min-height
Which property will be used to calculate the element's proportions?
— Defaults to iconArrow
- (string)instructionsPullToRefresh
The icon for both and instructionsReleaseToRefresh⇣
— Defaults to iconRefreshing
- (string)…
The icon when the refresh is in progress.
— Defaults to instructionsPullToRefresh
- (string)Pull down to refresh
The initial instructions string.
— Defaults to instructionsReleaseToRefresh
- (string)distThreshold
The instructions string when the has been reached.Release to refresh
— Defaults to instructionsRefreshing
- (string)Refreshing
The refreshing text.
— Defaults to refreshTimeout
- (integer)onRefresh
The delay, in milliseconds before the is triggered.500
— Defaults to getMarkup
- (function)__PREFIX__
It returns the default HTML for the widget, is replaced.getStyles
— See src/lib/markup.js
- (function)__PREFIX__
It returns the default CSS for the widget, is replaced.onInit
— See src/lib/styles.js
- (function)onRefresh
The initialize function.
- (function)window.location.reload()
What will the pull to refresh trigger? You can return a promise.
— Defaults to distanceResistedFunction
- (function)distExtra
Calculates the resisted pull distance. Receives an object with (distance pulled past distIgnore) and distThreshold (threshold to trigger refresh). Returns the actual distance the PTR element should be pulled.({ distExtra, distThreshold }) => Math.min(1, (distExtra / distThreshold) / 2.5) * distExtra
— Defaults to onReleasingWhilePulling
- (function)distThreshold
Callback triggered when transitioning from the pulling state to the releasing state (i.e., when the user has pulled past ).shouldPullToRefresh
- (function)!window.scrollY
Which condition should be met for pullToRefresh to trigger?
— Defaults to • Please note that this default is useful whenever you're setting mainElement as the body of the document, if you need it in another element with overflow, use !this.mainElement.scrollTop. Refer to the multiple instances demo for reference.
With ReactDOMServer and renderToString() you can use components as
icons instead of just strings.
In this example we also use Font Awesome to get nice icons with animation, but you can
use any React component you like.
`jsx harmony
import React, { Component } from 'react';
import ReactDOMServer from 'react-dom/server';
import PullToRefresh from 'pulltorefreshjs';
import { faSyncAlt} from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
class App extends Component
{
componentDidMount()
{
PullToRefresh.init({
mainElement: 'body',
onRefresh() {
window.location.reload();
},
iconArrow: ReactDOMServer.renderToString(
),
iconRefreshing: ReactDOMServer.renderToString(
),
});
}
componentWillUnmount()
{
// Don't forget to destroy all instances on unmout
// or you will get some glitches.
PullToRefresh.destroyAll();
}
render()
{
return (
export default App;
`
To quickly start the development workflow:
1. Install NodeJS (NVM)
2. Run nvm use 10 && npm installnpm run dev
3. Then
> This will watch and compile the bundle for browser usage.
E2E tests are executed with Testcafé.
- Run npm test to fire tests in the default browser, use BROWSER to change thismake
- ...or just run to setup the dependencies and run tests only (e.g. CI)
Advanced debug can be achieved with testcafe-live, e.g.
`bash`
$ npm test --live chrome tests/e2e/cases --debug-on-fail
- [ ] More events: onPullStart, onPullDown(direction, willRefresh), onRelease(willRefresh)`
- [ ] Fully customizable CSS
- [ ] Gallery of use cases
- [ ] Advanced demos
- [x] Tests
- [x] Minified releases