Skip to content

Commit d510fe0

Browse files
committed
docs: add <IdleSensor> docs
1 parent 7aa019d commit d510fe0

File tree

7 files changed

+68
-3
lines changed

7 files changed

+68
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const MyComponent = mock();
5252
- [`<FocusSensor>`](./docs/en/FocusSensor.md), [`withFocus()`](./docs/en/FocusSensor.md#withfocus-hoc), and [`@withFocus`](./docs/en/FocusSensor.md#withfocus-decorator)
5353
- [`<GeoLocationSensor>`](./docs/en/GeoLocationSensor.md), [`withGeoLocation()`](./docs/en/GeoLocationSensor.md#withgeolocation-hoc), and [`@withGeoLocation`](./docs/en/GeoLocationSensor.md#withgeolocation-decorator)
5454
- [`<HoverSensor>`](./docs/en/HoverSensor.md), [`withHover()`](./docs/en/HoverSensor.md#withhover-hoc), and [`@withHover`](./docs/en/HoverSensor.md#withhover-decorator)
55+
- [`<IdleSensor>`](./docs/en/IdleSensor.md), [`withIdle()`](./docs/en/IdleSensor.md#withidle-hoc), and [`@withIdle`](./docs/en/IdleSensor.md#withidle-decorator)
5556
- [`<MediaDeviceSensor>`](./docs/en/MediaDeviceSensor.md), [`withMediaDevices()`](./docs/en/MediaDeviceSensor.md#withmediadevices), and [`@withMediaDevices`](./docs/en/MediaDeviceSensor.md#withmediadevices-1)
5657
- [`<MediaSensor>`](./docs/en/MediaSensor.md), [`withMedia()`](./docs/en/MediaSensor.md#withmedia), and [`@withMedia`](./docs/en/MediaSensor.md#withmedia-1)
5758
- [`<MotionSensor>`](./docs/en/MotionSensor.md), [`withMotion()`](./docs/en/MotionSensor.md#withmotion-hoc), and [`@withMotion`](./docs/en/MotionSensor.md#withmotion-decorator)

docs/en/IdleSensor.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# `<IdleSensor>`
2+
3+
Tracks if use is active on the page. If user does not perform any events on the page
4+
during a specified timeout, `IdleSensor` fires an event and changes its state.
5+
6+
Uses these events to track user activity:
7+
8+
```js
9+
['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'wheel', 'visibilitychange']
10+
```
11+
12+
All events, except `visibilitychange`, are fired by `window` object and the list can
13+
be set in props.
14+
15+
16+
## Usage
17+
18+
```jsx
19+
import {IdleSensor} from 'libreact/lib/IdleSensor';
20+
21+
<IdleSensor ms={10000} onChange={(idle) => console.log(idle)}>
22+
{({idle}) =>
23+
<div>
24+
User is idle: {idle ? 'TRUE' : 'FALSE'}
25+
</div>
26+
}
27+
</IdleSensor>
28+
```
29+
30+
31+
## Props
32+
33+
Signature
34+
35+
```ts
36+
interface IIdleSensorProps {
37+
events?: string[];
38+
ms?: number;
39+
onChange?: (idle: boolean) => void;
40+
throttle?: number;
41+
}
42+
```
43+
44+
, where
45+
46+
- `events` &mdash; optional, list of `window` events to listen to.
47+
- `ms` &mdash; optional, number, time in milliseconds after which to consider user idle, defaults to `1000 * 60 * 2`.
48+
- `onChange` &mdash; optional, callback, fires when `IdleSensor` changes its state, receives a single boolean argument.
49+
- `throttle` &mdash; optional, number, time in milliseconds used to throttle events, defaults to `50`. This prop cannot be changed dynamically.
50+
51+
52+
## `withIdle()` HOC
53+
54+
Enhancer that injects `idle` prop into your component.
55+
56+
57+
## `@withIdle` decorator
58+
59+
Stateful component class decorator that injects `idle` prop.

docs/en/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [`<FocusSensor>`](./FocusSensor.md), [`withFocus()`](./FocusSensor.md#withfocus-hoc), and [`@withFocus`](./FocusSensor.md#withfocus-decorator)
2222
- [`<GeoLocationSensor>`](./GeoLocationSensor.md), [`withGeoLocation()`](./GeoLocationSensor.md#withgeolocation-hoc), and [`@withGeoLocation`](./GeoLocationSensor.md#withgeolocation-decorator)
2323
- [`<HoverSensor>`](./HoverSensor.md), [`withHover()`](./HoverSensor.md#withhover-hoc), and [`@withHover`](./HoverSensor.md#withhover-decorator)
24+
- [`<IdleSensor>`](./IdleSensor.md), [`withIdle()`](./IdleSensor.md#withidle-hoc), and [`@withIdle`](./IdleSensor.md#withidle-decorator)
2425
- [`<MediaDeviceSensor>`](./MediaDeviceSensor.md), [`withMediaDevices()`](./MediaDeviceSensor.md#withmediadevices), and [`@withMediaDevices`](./MediaDeviceSensor.md#withmediadevices-1)
2526
- [`<MediaSensor>`](./MediaSensor.md), [`withMedia()`](./MediaSensor.md#withmedia), and [`@withMedia`](./MediaSensor.md#withmedia-1)
2627
- [`<MotionSensor>`](./MotionSensor.md), [`withMotion()`](./MotionSensor.md#withmotion-hoc), and [`@withMotion`](./MotionSensor.md#withmotion-decorator)

docs/en/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* [FocusSensor](FocusSensor.md)
3030
* [GeoLocationSensor](GeoLocationSensor.md)
3131
* [HoverSensor](HoverSensor.md)
32+
* [IdleSensor](IdleSensor.md)
3233
* [MediaDeviceSensor](MediaDeviceSensor.md)
3334
* [MediaSensor](MediaSensor.md)
3435
* [MotionSensor](MotionSensor.md)

docs/en/Sensors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ For example, the below `<div>` will be re-rendered every time mouse position cha
1818
- [`<FocusSensor>`](./FocusSensor.md) &mdash; tracks element has focus.
1919
- [`<GeoLocationSensor>`](./GeoLocationSensor.md) &mdash; tracks device geographical location.
2020
- [`<HoverSensor>`](./HoverSensor.md) &mdash; tracks element's hover status.
21+
- [`<IdleSensor>`](./IdleSensor.md) &mdash; tracks is user is active on the page.
2122
- [`<MediaDeviceSensor>`](./MediaDeviceSensor.md) &mdash; tracks media devices connected to your device.
2223
- [`<MediaSensor>`](./MediaSensor.md) &mdash; tracks matches of a media query.
2324
- [`<MotionSensor>`](./MotionSensor.md) &mdash; tracks physical motion of your device.

src/IdleSensor/__story__/story.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const Print = ({idle}) =>
1313
const Hoc1 = withIdle(Print, '', {ms: 3000});
1414

1515
storiesOf('Sensors/IdleSensor', module)
16-
// .add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/IdleSensor.md')}))
16+
.add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/IdleSensor.md')}))
1717
.add('FaCC - 2 sec', () =>
1818
<IdleSensor ms={2000}>{({idle}) =>
1919
<div>

src/IdleSensor/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface IIdleSensorProps {
2424
events?: string[];
2525
ms?: number;
2626
onChange?: (idle: boolean) => void;
27+
throttle?: number;
2728
}
2829

2930
export interface IIdleSensorState {
@@ -32,8 +33,9 @@ export interface IIdleSensorState {
3233

3334
export class IdleSensor extends Component<IIdleSensorProps, IIdleSensorState> {
3435
static defaultProps = {
35-
events: ['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'scroll'],
36+
events: ['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'wheel'],
3637
ms: 1000 * 60 * 2,
38+
throttle: 50
3739
}
3840

3941
state = {
@@ -85,7 +87,7 @@ export class IdleSensor extends Component<IIdleSensorProps, IIdleSensorState> {
8587
}
8688
};
8789

88-
onEvent = throttle(50, false, () => {
90+
onEvent = throttle(this.props.throttle, false, () => {
8991
if (this.state.idle) {
9092
this.change(false);
9193
}

0 commit comments

Comments
 (0)