Skip to content

Commit 1822f83

Browse files
authored
Merge pull request #9 from MailOnline/feat/various-2
Feat/various 2
2 parents d54c6ba + f6a39fc commit 1822f83

36 files changed

+1159
-249
lines changed

README.md

+13-14
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,32 +35,31 @@ const MyComponent = mock();
3535
- [`delayed()`](./docs/delayed.md)
3636
- [`invert()`](./docs/invert.md)
3737
- Sensors
38-
- [`<BatterySensor>`](./docs/BatterySensor.md)
39-
- [`<MediaDeviceSensor>`](./docs/MediaDeviceSensor.md)
40-
- [`<MediaSensor>`](./docs/MediaSensor.md)
38+
- [`<BatterySensor>`](./docs/BatterySensor.md), [`withBattery()`](./docs/BatterySensor.md#withbattery), and [`@withBattery`](./docs/BatterySensor.md#withbattery-1)
39+
- [`<MediaDeviceSensor>`](./docs/MediaDeviceSensor.md), [`withMediaDevices()`](./docs/MediaDeviceSensor.md#withmediadevices), and [`@withMediaDevices`](./docs/MediaDeviceSensor.md#withmediadevices-1)
40+
- [`<MediaSensor>`](./docs/MediaSensor.md), [`withMedia()`](./docs/MediaSensor.md#withmedia), and [`@withMedia`](./docs/MediaSensor.md#withmedia-1)
4141
- [`<NetworkSensor>`](./docs/NetworkSensor.md) and [`withNetwork()`](./docs/NetworkSensor.md#withnetwork)
4242
- [`<LightSensor>`](./docs/LightSensor.md)
4343
- [`<LocationSensor>`](./docs/LocationSensor.md)
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)
5151
- Generators
5252
- [`<Alert>`](./docs/Alert.md)
53-
- [`<Audio>`](./docs/Audio.md)
53+
- [`<Audio>`](./docs/Audio.md), [`<Video>`](./docs/Video.md), and `<Media>`
5454
- [`<LocalStorage>`](./docs/LocalStorage.md)
5555
- `<Redirect>`
5656
- [`<Speak>`](./docs/Speak.md)
5757
- [`<Vibrate>`](./docs/Vibrate.md)
58-
- `<Video>`
5958
- Context
60-
- [`<Provider>`](./docs/context.md#provider), [`<Consumer>`](./docs/context.md#consumer), and [`withContext()`](./docs/context.md#withcontext)
61-
- [`<Theme>`](./docs/theme.md#theme), [`<Themed>`](./docs/theme.md#themed), and [`withTheme()`](./docs/theme.md#withtheme)
59+
- [`<Provider>`](./docs/context.md#provider), [`<Consumer>`](./docs/context.md#consumer), [`withContext()`](./docs/context.md#withcontext), and `@withContext`
60+
- [`<Theme>`](./docs/theme.md#theme), [`<Themed>`](./docs/theme.md#themed), [`withTheme()`](./docs/theme.md#withtheme), and `@withTheme`
6261
- `<CssVars>`
63-
- [`<Router>`](./docs/route.md#router), [`<Route>`](./docs/route.md#route), [`go()`](./docs/route.md#go), and [`withRoute()`](./docs/route.md#withroute)
62+
- [`<Router>`](./docs/route.md#router), [`<Route>`](./docs/route.md#route), [`go()`](./docs/route.md#go), [`withRoute()`](./docs/route.md#withroute), and `@withRoute`
6463
- CSS resets
6564
- [`<CssResetEricMeyer>`](./docs/reset/CssResetEricMeyer.md)
6665
- [`<CssResetEricMeyerCondensed>`](./docs/reset/CssResetEricMeyerCondensed.md)

docs/Audio.md

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ interface IAudioProps {
3636
- `preload` - optional, string, `<audio>` element preload attribute.
3737
- `volume` - optional, number, audio volume in `[0..1]` range, defaults to `1`.
3838
- `noJs` - optional, React element(s) to render insided the `<audio>` tag.
39+
- `onMount` - optional, callback, called when component mounts, receives `IAudio` as the first argument.
40+
- `onUnmount` - optional, callback, called when component un-mounts, receives `IAudio` as the first argument.
3941

4042

4143
## Arguments

docs/BatterySensor.md

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# `BatterySensor`
1+
# `<BatterySensor>`
22

33
Uses [`Navigator.getBattery()`](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getBattery) API to track battery level, charging status and time it takes to charge/discharge a battery.
44

5-
# Props
5+
## Props
66

77
None.
88

9-
# Example
9+
## Example
1010

1111
```jsx
1212
import {BatterySensor} from 'libreact/lib/BatterySensor';
@@ -26,3 +26,39 @@ Result
2626
"dischargingTime": null
2727
}
2828
```
29+
30+
## `withBattery()`
31+
32+
Higher order component that injects `battery` prop into your component.
33+
34+
```js
35+
import {withBattery} from 'libreact/lib/BatterySensor';
36+
37+
const MyCompWithBattery = withBattery(MyComp);
38+
```
39+
40+
Overwrite the inject prop name
41+
42+
```js
43+
const MyCompWithBattery = withBattery(MyComp, 'myBattery');
44+
```
45+
46+
or merge the props
47+
48+
```js
49+
const MyCompWithBattery = withBattery(MyComp, null);
50+
```
51+
52+
53+
## `@withBattery`
54+
55+
Stateful component class decorator that injects `battery` prop into your component.
56+
57+
```js
58+
import {withBattery} from 'libreact/lib/BatterySensor';
59+
60+
@withBattery
61+
class MyComp extends Component {
62+
63+
}
64+
```

docs/MediaDeviceSensor.md

+28-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,34 @@ user's connected media devices.
66
## Usage
77

88
```jsx
9-
import {MediaDevicesSensor} from 'libreact/lib/MediaDevicesSensor';
9+
import {MediaDeviceSensor} from 'libreact/lib/MediaDeviceSensor';
1010

11-
<MediaDevicesSensor>{(state) =>
11+
<MediaDeviceSensor>{(state) =>
1212
JSON.stringify(state, null, 4)
13-
}</MediaDevicesSensor>
13+
}</MediaDeviceSensor>
14+
```
15+
16+
17+
## `withMediaDevices()`
18+
19+
Higher order component that injects `devices` prop into your component.
20+
21+
```js
22+
import {withMediaDevices} from 'libreact/lib/MediaDeviceSensor';
23+
24+
const MyCompDevices = withMediaDevices(MyComp);
25+
```
26+
27+
28+
## `@withMediaDevices`
29+
30+
Stateful component class decorator that injects `devices` prop into your component.
31+
32+
```js
33+
import {withMediaDevices} from 'libreact/lib/MediaDeviceSensor';
34+
35+
@withMediaDevices
36+
class MyComp extends Component {
37+
38+
}
1439
```

docs/MediaSensor.md

+25
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,28 @@ import {MediaSensor} from 'libreact/lib/MediaSensor';
1111
`WIDTH IS GREATED THAN 480PX: ${matches}`
1212
}</MediaSensor>
1313
```
14+
15+
16+
## `withMedia()`
17+
18+
Higher order component that injects a boolean prop into your component that specifies if media query matches.
19+
20+
```js
21+
import {withMedia} from 'libreact/lib/MediaSensor';
22+
23+
const MyCompWithSize = withMedia(MyComp, 'isBigScreen', '(min-width: 480px)');
24+
```
25+
26+
27+
## `@withMedia`
28+
29+
Stateful component class decorator that injects a boolean prop into your component that specifies if media query matches.
30+
31+
```js
32+
import {withMedia} from 'libreact/lib/MediaSensor';
33+
34+
@withMedia('isBigScreen', '(min-width: 480px)')
35+
class MyComp extends Component {
36+
37+
}
38+
```

docs/Video.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# `<Video>`
2+
3+
FaCC that creates a `<video>` element to play video media, re-renders on video state changes.
4+
5+
## Usage
6+
7+
```jsx
8+
import {Video} from 'libreact/lib/Video';
9+
10+
<Video autoPlay src='http://dailym.ai/2rG7TBS'>{(actions, state) => {
11+
JSON.stringify(state, null, 4)
12+
}}</Video>
13+
```
14+
15+
16+
## Props
17+
18+
In addition to props below also accepts all [React's media events](https://reactjs.org/docs/events.html#media-events).
19+
20+
```tsx
21+
interface IVideoProps {
22+
src: string;
23+
autoPlay?: boolean;
24+
loop?: boolean;
25+
muted?: boolean;
26+
preload?: 'none' | 'metadata' | 'auto';
27+
volume?: number;
28+
noJs?: React.ReactElement<any>;
29+
}
30+
```
31+
32+
- `src` - required, string, video source file URL.
33+
- `autoPlay` - optional, boolean, whether to autoplay media, defaults to `false`.
34+
- `loop` - optional, boolean, whether to repeat the media when it ends, defaults to `false`.
35+
- `muted` - optional, boolean, whether to mute the media, defaults to `false`.
36+
- `preload` - optional, string, `<video>` element preload attribute.
37+
- `volume` - optional, number, media volume in `[0..1]` range, defaults to `1`.
38+
- `noJs` - optional, React element(s) to render insided the `<video>` tag.
39+
- `onMount` - optional, callback, called when component mounts, receives `IVideo` as the first argument.
40+
- `onUnmount` - optional, callback, called when component un-mounts, receives `IVideo` as the first argument.
41+
42+
43+
## Arguments
44+
45+
The `children` function receives two arguments, first is the `IVideo` *actions* collection and the second
46+
is the state of the video component.
47+
48+
```jsx
49+
<Video autoPlay src={src}>{({video, ...actions}, state) =>
50+
51+
}</Video>
52+
```
53+
54+
First argument is the `<Video>` component instance with the following public signature.
55+
56+
```ts
57+
interface IVideo {
58+
el: HTMLVideoElement;
59+
video: React.ReactElement;
60+
play();
61+
pause();
62+
seek(time: number);
63+
volume(value: number);
64+
mute();
65+
unmute();
66+
}
67+
```
68+
69+
, where
70+
71+
- `el` - `<video>` element DOM node.
72+
- `video` - `<video>` element React node, that you have to insert in the JSX tree.
73+
74+
The second argument is the state of the `<Video>` component with the following signature.
75+
76+
```ts
77+
interface IVideoState {
78+
buffered?: TRange[];
79+
time?: number;
80+
duration?: number;
81+
isPlaying?: boolean;
82+
muted?: boolean;
83+
volume?: number;
84+
}
85+
86+
type TRange = {
87+
start: number;
88+
end: number;
89+
};
90+
```
91+
92+
, where
93+
94+
- `buffered` - a list of ranges representing media intervals that have been buffered by the browser.
95+
- `time` - current time in seconds.
96+
- `duration` - total video duration in seconds.
97+
- `isPlaying` - whether the video is currently playing.
98+
- `muted` - whether `muted` attribute is set on the video element.
99+
- `volume` - current volume in range `[0..1]`.
100+
101+
102+
## Example
103+
104+
Play a sample video.
105+
106+
```jsx
107+
<Video
108+
src='http://dailym.ai/2rG7TBS'
109+
style={{
110+
width: 400,
111+
border: '1px solid tomato'
112+
}}
113+
render={({video, play, pause, seek, volume, mute, unmute}, {isPlaying}) =>
114+
<div>
115+
{video}
116+
<br />
117+
<button onClick={() => isPlaying ? pause() : play()}>
118+
{isPlaying ? 'Pause' : 'Play'}
119+
</button>
120+
</div>
121+
}
122+
/>
123+
```

docs/ViewportSensor.md

+9
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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "libreact",
3-
"version": "0.2.0",
3+
"version": "0.4.0",
44
"description": "React standard library",
55
"main": "lib/index.js",
66
"keywords": [

0 commit comments

Comments
 (0)