Skip to content

Commit 741b65f

Browse files
committed
feat: add withBattery() and @withBattery
1 parent e1fceb2 commit 741b65f

File tree

7 files changed

+74
-27
lines changed

7 files changed

+74
-27
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
React standard library, must-have toolbox for any React project.
88

99
- Collection of React sensors, FaCCs, HOCs, context providers, dummies, and [other goodies](#contents).
10-
- Isomorphic - all components work in browser and Node.js (and some in `react-native`).
11-
- [See demos and docs](https://mailonline.github.io/libreact/).
10+
- Isomorphic - all components work in browser and on server (and some in `react-native`).
11+
- See [demos](https://mailonline.github.io/libreact/) and [docs](#contents).
1212

1313
## Installation
1414

15-
```shell
16-
npm install libreact --save
17-
```
15+
<pre>
16+
npm i <a href="https://www.npmjs.com/package/libreact">libreact</a> --save
17+
</pre>
1818

1919
## Usage
2020

@@ -35,7 +35,7 @@ const MyComponent = mock();
3535
- [`delayed()`](./docs/delayed.md)
3636
- [`invert()`](./docs/invert.md)
3737
- Sensors
38-
- [`<BatterySensor>`](./docs/BatterySensor.md)
38+
- [`<BatterySensor>`](./docs/BatterySensor.md), `withBattery()`, and `@withBattery`
3939
- [`<MediaDeviceSensor>`](./docs/MediaDeviceSensor.md)
4040
- [`<MediaSensor>`](./docs/MediaSensor.md)
4141
- [`<NetworkSensor>`](./docs/NetworkSensor.md) and [`withNetwork()`](./docs/NetworkSensor.md#withnetwork)
@@ -44,7 +44,7 @@ const MyComponent = mock();
4444
- [`<OrientationSensor>`](./docs/OrientationSensor.md) and [`withOrientation()`](./docs/OrientationSensor.md#withorientation)
4545
- [`<ScrollSensor>`](./docs/ScrollSensor.md)
4646
- [`<SizeSensor>`](./docs/SizeSensor.md)
47-
- [`<ViewportSensor>`](./docs/ViewportSensor.md), `<ViewportScrollSensor>`, and `<ViewportObserverSensor>`
47+
- [`<ViewportSensor>`](./docs/ViewportSensor.md), [`<ViewportScrollSensor>`](./docs/ViewportSensor.md#viewportscrollsensor), and [`<ViewportObserverSensor>`](./docs/ViewportSensor.md#viewportobserversensor)
4848
- [`<WidthSensor>`](./docs/WidthSensor.md)
4949
- [`<WindowScrollSensor>`](./docs/WindowScrollSensor.md)
5050
- [`<WindowSizeSensor>`](./docs/WindowSizeSensor.md)

docs/ViewportSensor.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ interface IViewportSensorState {
4646
}
4747
```
4848

49+
4950
## `<ViewportScrollSensor>`
5051

52+
Is the same as `<ViewportSensor>`, but uses only window `scroll` event to check for visibility changes.
53+
5154
The `<ViewportScrollSensor>` has an additional prop `throttle`, which is a number in milliseconds specifying
5255
how much to throttle document's `scroll` event.
56+
57+
58+
## `<ViewportObserverSensor>`
59+
60+
Is the same as `<ViewportSensor>`, but uses only `IntersectionObserver` to detect element's intersection
61+
with viewport.

docs/libreact.png

-9.14 KB
Loading
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {Component, createElement as h} from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
import {action} from '@storybook/addon-actions';
4+
import {linkTo} from '@storybook/addon-links';
5+
import {BatterySensor, withBattery} from '..';
6+
import ShowDocs from '../../../.storybook/ShowDocs';
7+
8+
const Print = (props) => h('pre', {
9+
style: {
10+
fontFamily: 'monospace'
11+
}
12+
},
13+
JSON.stringify(props, null, 4)
14+
);
15+
16+
const PrintBattery = withBattery(Print);
17+
18+
@withBattery
19+
class Printer extends Component<any, any> {
20+
render () {
21+
return h('pre', {
22+
style: {
23+
fontFamily: 'monospace'
24+
}
25+
},
26+
JSON.stringify(this.props, null, 4)
27+
);
28+
}
29+
}
30+
31+
storiesOf('Sensors/BatterySensor', module)
32+
.add('Documentation', () => h(ShowDocs, {name: 'BatterySensor'}))
33+
.add('FaCC', () =>
34+
h(BatterySensor, {}, (state) =>
35+
h('pre', {style: {
36+
fontFamily: 'monospace'
37+
}},
38+
JSON.stringify(state, null, 4)
39+
)
40+
)
41+
)
42+
.add('HOC', () => h(PrintBattery))
43+
.add('Decorator', () => h(Printer));

src/BatterySensor/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Component} from 'react';
2-
import {on, off, isClient} from '../util';
2+
import {h, on, off, isClient} from '../util';
3+
import addClassDecoratorSupport from '../util/addClassDecoratorSupport';
34

45
export interface IBatterySensorProps {
56
children?: (INetworkState) => React.ReactElement<any>;
@@ -68,3 +69,15 @@ export class BatterySensor extends Component<IBatterySensorProps, IBatterySensor
6869
return this.props.children(this.state);
6970
}
7071
}
72+
73+
export const withBattery = (Comp) => {
74+
const Enhanced = (props) =>
75+
h(BatterySensor, null, (battery) =>
76+
h(Comp, {
77+
...props,
78+
battery
79+
})
80+
);
81+
82+
return addClassDecoratorSupport(Enhanced);
83+
};

src/BatterySensor/story.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Media/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export abstract class Media<P extends IMediaProps<M>, S extends IMediaState, M e
101101
play = () => {
102102
if (this.el) {
103103
// TODO: In some browsers `.play()` method returns a `Promise`, where you
104-
// TODO: cannot call `pauer()` or `play()` again before that promise resolves.
104+
// TODO: cannot call `pause()` or `play()` again before that promise resolves.
105105
const promise = this.el.play();
106106
}
107107
};

0 commit comments

Comments
 (0)