Package Exports
- react-idle-manager
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-idle-manager) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
react-idle-manager
A decorator for React components that notifies of idle and timed-out states
Table of contents
Installation
$ npm i react-idle-manager --save
Summary
This decorator wraps whatever React
component you apply it to with a higher-order component that will supply idle state and timeout state information to the component via props
. It leverages localStorage
to ensure that the idle state is respected between multiple tab instances, and will handle multiple unique instances based on the key
s passed.
Usage
Basic usage:
import React from 'react';
import idleManager from 'react-idle-manager';
@idleManager('my-super-awesome-app')
class App extends React.PureComponent {
...
}
Advanced usage:
import React from 'react';
import idleManager from 'react-idle-manager';
const IDLE_MANAGER_OPTIONS = {
key: 'my-super-awesome-app',
idleAfter: 50000,
isPure: false,
timeoutAfter: 10000
};
@idleManager(IDLE_MANAGER_OPTIONS)
class App extends React.PureComponent {
...
}
Set the list of available options below.
Props injected
When the idleManager
decorator is applied to a component, the following props will be passed to the component:
isIdle
boolean
Is the component currently considered idle.
isTimedOut
boolean
Is the component currently considered timed ONE_MINUTE
timeoutIn
(null|number)
If the component is in an idle state, the number of milliseconds until the component will time out is passed. If the component is not idle, null
is passed.
This number is updated for every second between the start of idle state and when timeout state is reached, providing a countdown until timeout.
- The number of milliseconds may not update in exact 1 second intervals, due to the nature of
setTimeout
. If you want to provide a clean count of the number of seconds, you can doMath.ceil(timeoutIn / 1000)
. - If you do not want to provide this timeout, pass the
timeoutAfter
option value of0
.
Required values
key
string
The key to use in localStorage
for storage of when idle and timeout states are reached. This value can either be passed as the only parameter to the decorator, or as a property on the object of options.
Please note that if the same key is used for multiple components, there may be collision of specific time states, so make the keys consistent but specific to each implementation.
It is recommended to follow the format ${uniqueAppName}_${componentName}
:
@idleManager('planttheidea-idle-manager-demo_App')
class App extends PureComponent {
...
}
Options
idleAfter
number (defaults to 840000, or 14 minutes in milliseconds)
The number of milliseconds since activity that the component is considered in idle state.
isPure
boolean (defaults to true
)
Should PureComponent
be used as the basis of the higher-order component the decorator creates. If false
is passed, the standard Component
class is used.
timeoutAfter
number (defaults to 60000, or 1 minute in milliseconds)
The number of milliseconds since idle state was reached that the component is considered in timeout state.
Please note that if a custom idleAfter
is provided but no custom timeoutAfter
is provided, timeout is assumed to be 1 minute later than idleAfter
. If you do not want a distinction between idle and timeout state, simply pass 0
as the value for timeoutAfter
.
Development
Standard stuff, clone the repo and npm i
to get the dependencies. npm scripts available:
build
=> builds the distributed JS withNODE_ENV=development
and with sourcemapsbuild:minified
=> builds the distributed JS withNODE_ENV=production
and minifiedclean
=> removes thelib
anddist
folders created during publishdev
=> runs the webpack dev server for the playgroundlint
=> runs ESLint against files in thesrc
folderlint:fix
=> runs ESLint against files in thesrc
folder and fixes any fixable issues discoveredprepublish
=> if in publish, runsprepublish:compile
prepublish:compile
=> runs thelint
,test
,transpile
,dist
scriptstest
=> runava
with NODE_ENV=testtest:coverage
=> runava
withnyc
to calculate code coveragetest:update
=> runstest
but updates all snapshotstest:watch
=> runstest
but with persistent watchertranspile
=> runs Babel against files insrc
to files inlib