Skip to content

Commit 1e95196

Browse files
committed
feat: add <Video> docs, and improve docs
1 parent 61d76a6 commit 1e95196

File tree

5 files changed

+132
-6
lines changed

5 files changed

+132
-6
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,16 @@ const MyComponent = mock();
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

Lines changed: 2 additions & 0 deletions
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/Video.md

Lines changed: 123 additions & 0 deletions
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+
```

src/Media/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export interface IMediaProps<M> extends React.AllHTMLAttributes<any> {
2828
volume?: number;
2929
noJs?: React.ReactElement<any>;
3030

31+
onMount?: TMediaEvent<M>,
32+
onUnmount?: TMediaEvent<M>,
3133
onAbort?: TMediaEvent<M>,
3234
onCanPlay?: TMediaEvent<M>,
3335
onCanPlayThrough?: TMediaEvent<M>,

src/Video/__story__/story.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {linkTo} from '@storybook/addon-links';
55
import {Video} from '..';
66
import ShowDocs from '../../../.storybook/ShowDocs'
77

8-
const src = 'http://video.dailymail.co.uk/video/mol/2017/10/03/4242224567792913009/1024x576_MP4_4242224567792913009.mp4';
8+
const src = 'http://dailym.ai/2rG7TBS';
99

1010
storiesOf('Generators/Video', module)
1111
.add('Documentation', () => h(ShowDocs, {name: 'Video'}))

0 commit comments

Comments
 (0)