|
1 | 1 | # react-html5video
|
2 | 2 |
|
3 |
| -A customizeable HTML5 Video that uses the familiar HTML5 video markup but with custom and configurable controls with i18n and a11y. |
| 3 | +A customizeable HoC (Higher Order Component) for HTML5 Video that allows custom and configurable controls with i18n and a11y. |
4 | 4 |
|
| 5 | +V2 API has changed and is not backwards compatible. You can find the old documentation [here](https://github.com/mderrick/react-html5video/blob/1.4.0/README.md). |
| 6 | + |
| 7 | +[](https://travis-ci.org/mderrick/react-html5video) |
5 | 8 | [](https://www.npmjs.com/package/react-html5video)
|
6 | 9 | [](https://www.npmjs.com/package/react-html5video)
|
7 | 10 |
|
8 |
| -<img src="http://mderrick.github.io/react-html5video/example.png?v=1" align="center" height="337" width="600" /> |
| 11 | +<img src="http://mderrick.github.io/react-html5video/example.v2.png?v=1" align="center" height="337" width="600" /> |
9 | 12 |
|
10 | 13 | View the [demo](http://mderrick.github.io/react-html5video/).
|
11 | 14 |
|
12 | 15 | ## Install
|
13 | 16 |
|
14 |
| -`npm install react-html5video --save` or `bower install react-html5video --save` |
15 |
| - |
16 |
| -Include `dist/ReactHtml5Video.css` if you do not want to build your own CSS. Alternatively require `src/assets/video.css` if you want to compile the CSS yourself with [css-loaders](https://github.com/webpack/css-loader) and [url-loaders](https://github.com/webpack/url-loader) etc. See the demo [Webpack config](https://github.com/mderrick/react-html5video/blob/master/demo/webpack.config.js) as an example. |
| 17 | +`npm install react-html5video --save` |
17 | 18 |
|
18 | 19 | ### Peer Dependencies
|
19 | 20 |
|
20 |
| -This component uses ES2015 and needs to be transpiled using something like [babel-loader](https://github.com/babel/babel-loader). You will also need to either [polyfill](https://babeljs.io/docs/usage/polyfill/) or use [babel-runtime](https://www.npmjs.com/package/babel-runtime). It depends on: |
21 |
| -- `react@>=0.14.x` |
22 |
| -- `lodash.throttle@latest`. |
23 |
| - |
24 |
| -### UMD |
25 |
| - |
26 |
| -Alternatively if using the UMD module it is already transpiled to ES5 and `lodash.throttle` is included. You can find this build in the `dist` directory: |
27 |
| - |
28 |
| -```js |
29 |
| -// Includes lodash.throttle and is transpiled already. No ES2015 polyfill is required. |
30 |
| -var Video = require('react-html5video/dist/ReactHtml5Video'); |
31 |
| -``` |
32 |
| - |
33 |
| -```js |
34 |
| -// Exports to this global |
35 |
| -var Video = window.ReactHtml5Video.default; |
36 |
| -``` |
37 |
| - |
38 |
| -```js |
39 |
| -// Requires es6 transpiling, an es6 polyfill/babel-runtime and all peer dependencies installed |
40 |
| -import { default as Video, Controls, Play, Mute, Seek, Fullscreen, Time, Overlay } from 'react-html5video'; |
41 |
| -``` |
42 |
| - |
| 21 | +- `react@>=15.0.x` |
| 22 | +- `react-dom@>=15.0.x` |
43 | 23 |
|
44 | 24 | ## Usage
|
45 | 25 |
|
46 | 26 | ### Simple Usage
|
47 | 27 |
|
48 |
| -Use normal HTML5 `<video>` markup with all the standard [html attributes](https://developer.mozilla.org/en/docs/Web/HTML/Element/video) and supported [React media events](https://facebook.github.io/react/docs/events.html#media-events): |
| 28 | +The simplest way to use this component is to use the default player that is provided. It works the same way as a normal HTML5 video by taking all the supported [HTML5 video attributes](https://developer.mozilla.org/en/docs/Web/HTML/Element/video) except for `controls`. This is now "controlled" and can be an array of supported component names in any order as below: |
49 | 29 |
|
50 | 30 | ```js
|
51 |
| -import Video from 'react-html5video'; |
| 31 | +import { DefaultPlayer as Video } from 'react-html5video'; |
| 32 | +import 'react-html5video/dist/styles.css'; |
52 | 33 | render() {
|
53 | 34 | return (
|
54 |
| - <Video controls autoPlay loop muted |
| 35 | + <Video autoPlay loop muted |
| 36 | + controls={['PlayPause', 'Seek', 'Time', 'Volume', 'Fullscreen']} |
55 | 37 | poster="http://sourceposter.jpg"
|
56 | 38 | onCanPlayThrough={() => {
|
57 | 39 | // Do stuff
|
58 | 40 | }}>
|
59 | 41 | <source src="http://sourcefile.webm" type="video/webm" />
|
| 42 | + <track label="English" kind="subtitles" srcLang="en" src="http://source.vtt" default /> |
60 | 43 | </Video>
|
61 | 44 | );
|
62 | 45 | }
|
63 | 46 | ```
|
64 | 47 |
|
65 |
| -### Advanced Usage |
| 48 | +#### a11y* and i18n |
66 | 49 |
|
67 |
| - You can configure, customize and modify the controls by adding, removing and shuffling them as you desire. You can create your very own custom children components and controls that can interact with the video. All children components will receive [these props](#props-and-methods). Obviously you can still call methods and set properties on the HTML5 DOM element directly if you have access to it with `refs`: |
| 50 | +The custom controls provided are built using `<button>` and `<input type="range">` which means basic keyboard controls are available when they are focused. For example, you can and hit the space bar on mute, play and fullscreen buttons as well as seek using the arrow keys when focused on the slider. `aria-label` attributes for screen readers have been used where user interaction is required. Try tabbing through the [demo](http://mderrick.github.io/react-html5video/) with [Vox](http://www.chromevox.com/) enabled. |
| 51 | + |
| 52 | +You can change translate the `aria-label` values for all controls like so: |
68 | 53 |
|
69 | 54 | ```js
|
70 |
| -import { default as Video, Controls, Play, Mute, Seek, Fullscreen, Time, Overlay } from 'react-html5video'; |
71 |
| -render() { |
72 |
| - return ( |
73 |
| - <Video controls autoPlay loop muted poster="http://sourceposter.jpg"> |
74 |
| - <source src="http://sourcefile.mp4" type="video/mp4" /> |
75 |
| - <source src="http://sourcefile.webm" type="video/webm" /> |
76 |
| - <h1>Optional HTML and components can be added also</h1> |
77 |
| - <CustomComponent /> |
78 |
| - |
79 |
| - /* As soon as a child is supplied that is not a `<source>` |
80 |
| - you have to define all controls and overlays as the default |
81 |
| - controls will have been removed. They are however exported |
82 |
| - and can be re-applied as below in any order. */ |
83 |
| - <Overlay /> |
84 |
| - <Controls> |
85 |
| - <Play /> |
86 |
| - <Seek /> |
87 |
| - <Time /> |
88 |
| - <Mute /> |
89 |
| - <Fullscreen /> |
90 |
| - <CustomControlComponent /> |
91 |
| - </Controls> |
92 |
| - </Video> |
93 |
| - ); |
94 |
| -} |
| 55 | +<Video copy={{ key: value }}> |
95 | 56 | ```
|
96 | 57 |
|
97 |
| -## i18n |
| 58 | +The default english `copy` can be found in [here](https://github.com/mderrick/react-html5video/blob/master/src/DefaultPlayer/copy.js). |
| 59 | + |
| 60 | +*Disclaimer: Unfortuantely I don't much experience with a11y but I have tried to use some of the features from [PayPal's accessible HTML5 player](https://github.com/paypal/accessible-html5-video-player). If anyone has further input on this please raise an issue or a pull request. |
| 61 | + |
| 62 | +### Advanced Usage |
98 | 63 |
|
99 |
| -There is some text used that could require translations. This can be done like so: |
| 64 | +If you want to get creative and create your own video player then you will need to use the higher order component. The HoC connects a React component to all the [HTML5 video attributes](https://developer.mozilla.org/en/docs/Web/HTML/Element/video) and the [HTMLMediaElement](https://developer.mozilla.org/en/docs/Web/API/HTMLMediaElement) of the first video it finds in the component it is wrapping. |
100 | 65 |
|
101 | 66 | ```js
|
102 |
| -<Video copyKeys={{ key: value }}> |
| 67 | +import videoConnect from 'react-html5video'; |
| 68 | + |
| 69 | +const MyVideoPlayer = ({ video, videoEl, children, ...restProps }) => ( |
| 70 | + <div> |
| 71 | + <video {...restProps}> |
| 72 | + { children } |
| 73 | + </video> |
| 74 | + <p> |
| 75 | + Here are the video properties for the above HTML5 video: |
| 76 | + { JSON.stringify(video) } |
| 77 | + </p> |
| 78 | + <a href="#" onClick={(e) => { |
| 79 | + e.preventDefault(); |
| 80 | + // You can do what you like with the HTMLMediaElement DOM element also. |
| 81 | + videoEl.pause(); |
| 82 | + }}> |
| 83 | + Pause video |
| 84 | + </a> |
| 85 | + </div> |
| 86 | +); |
| 87 | + |
| 88 | +export default videoConnect(MyVideoPlayer) |
103 | 89 | ```
|
104 | 90 |
|
105 |
| -The default english `copyKeys` can be found in [here](https://github.com/mderrick/react-html5video/tree/master/src/assets/copy.js). |
| 91 | +The above will simply print out the properties of the HTML5 `<video>` within `MyVideoPlayer`. Now you have these properties and the HTMLMediaElement itself available in your component, it is up to you to create your new custom controls using them. See the [default player](https://github.com/mderrick/react-html5video/blob/master/src/DefaultPlayer/DefaultPlayer.js) as an example. |
106 | 92 |
|
107 |
| -## a11y* |
| 93 | +#### API |
108 | 94 |
|
109 |
| -The custom controls provided are built using `<button>` and `<input type="range">` which means basic keyboard controls are available when they are focused. For example, you can and hit the space bar on mute, play and fullscreen buttons as well as seek using the arrow keys when focused on the slider. All inputs have a visible focus outline and can be tabbed to. `aria-label` attributes for screen readers have been used where user interaction is required. Try tabbing through the [demo](http://mderrick.github.io/react-html5video/) with [Vox](http://www.chromevox.com/) enabled. |
| 95 | +The API behaves much like the [React Redux](https://github.com/reactjs/react-redux/) connect HoC but instead of using dispatch to change the state, we have access to the [HTMLMediaElement](https://developer.mozilla.org/en/docs/Web/API/HTMLMediaElement). |
110 | 96 |
|
111 |
| -*Disclaimer: Unfortuantely I don't much experience with a11y but I have tried to use some of the features from [PayPal's accessible HTML5 player](https://github.com/paypal/accessible-html5-video-player). If anyone has further input on this please raise an issue or a pull request. |
| 97 | +#### `videoConnect(ReactComponent, [mapStateToProps], [mapVideoElToProps], [mergeProps])` |
| 98 | + |
| 99 | +- `mapStateToProps(videoState, ownProps)` - Use this to return the [HTML5 video attributes](https://developer.mozilla.org/en/docs/Web/HTML/Element/video) required for your component. The plain object returned here will be merged with what is returned from `mapVideoElToProps` using the `mergeProps` function. By Default this returns all video attributes so they are accessible on `this.props.video` in your wrapped component. |
112 | 100 |
|
| 101 | +- `mapVideoElToProps(videoEl, videoState, ownProps)` - Use this to return a plain object that uses `videoEl` to update the videos state. `videoEl` is the raw [HTMLMediaElement](https://developer.mozilla.org/en/docs/Web/API/HTMLMediaElement). The object returned here will be merged with what is returned from `mapStateToProps` using the `mergeProps` function. By default the `videoEl` will be accessible on `this.props.videoEl` in your wrapped component. |
113 | 102 |
|
114 |
| -## Props and Methods |
115 |
| - |
116 |
| -All children components will receive the following properties via props: |
117 |
| -- `copyKeys` |
118 |
| -- `duration` |
119 |
| -- `currentTime` |
120 |
| -- `buffered` |
121 |
| -- `paused` |
122 |
| -- `muted` |
123 |
| -- `volume` |
124 |
| -- `playbackRate` |
125 |
| -- `percentageBuffered` |
126 |
| -- `percentagePlayed` |
127 |
| - |
128 |
| -All children components receive the following methods via props: |
129 |
| -- `play` |
130 |
| -- `pause` |
131 |
| -- `togglePlay` |
132 |
| -- `mute` |
133 |
| -- `unmute` |
134 |
| -- `toggleMute` |
135 |
| -- `seek` |
136 |
| -- `fullscreen` |
137 |
| -- `setVolume` |
138 |
| -- `setPlaybackRate` |
| 103 | +- `mergeProps(stateProps, videoElProps, ownProps)` - If specified, it is passed the result of `mapStateToProps` `mapVideoElToProps` and the parent `props`. The plain object you return will be passed to your wrapped component. By default this will do `Object.assign({}, stateProps, videoElProps, ownProps)`. |
139 | 104 |
|
140 | 105 | ## Contributing
|
141 | 106 |
|
142 | 107 | ### Dev Setup
|
143 | 108 |
|
144 |
| -To run a server with hot module replacement: |
| 109 | +To run a development server with HMR: |
145 | 110 |
|
146 | 111 | ```bash
|
147 |
| - $ npm install |
148 |
| - $ cd demo |
149 |
| - $ npm install |
150 |
| - $ npm run dev-server |
| 112 | + $ npm i |
| 113 | + $ npm run i:demo |
| 114 | + $ npm start |
151 | 115 | ```
|
152 | 116 |
|
153 |
| -Open [http://localhost:8082](http://localhost:8082). You can now modify the files in both `src` and `demo/src`. |
154 |
| - |
155 |
| -### Issues |
156 |
| - |
157 |
| -Feel free to raise and solve any existing issues as desired. Where possible please do try and replicate any bugs you may have using the [react-html5video jsfiddle](https://jsfiddle.net/mderrick/7s9a311q/1/) and include them in your ticket. |
158 |
| - |
159 |
| -## Thank You |
160 |
| - |
161 |
| -<img src="http://mderrick.github.io/react-html5video/browserstack.png?v=1" height="22" width="100" /> |
162 |
| - |
163 |
| -[BrowserStack](http://www.browserstack.com) for a free subscription to help test cross browser. |
164 |
| - |
165 | 117 | ## License
|
166 | 118 | MIT
|
0 commit comments