Last active
August 23, 2018 01:12
-
-
Save mathisonian/468d0d0aa770366f943c2c13d678a0d3 to your computer and use it in GitHub Desktop.
Idyll Youtube Component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
let YouTube; | |
class YoutubeComponent extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
mounted: false | |
} | |
} | |
componentDidMount() { | |
this.setState({ mounted: true }); | |
YouTube = require('react-youtube').default; | |
} | |
render() { | |
if (!this.state.mounted) { | |
return null; | |
} | |
const opts = { | |
height: this.props.height, | |
width: this.props.width, | |
playerVars: Object.assign({}, { // https://developers.google.com/youtube/player_parameters | |
autoplay: this.props.play | |
}, this.props.options) | |
}; | |
return ( | |
<div style={{display: 'block', textAlign: 'center'}}> | |
<YouTube | |
videoId={this.props.id} | |
opts={opts} | |
onReady={this._onReady.bind(this)} | |
/> | |
</div> | |
); | |
} | |
componentDidUpdate(props, newState) { | |
if (this._player && props.play !== this.props.play) { | |
this.props.play ? this._player.playVideo() : this._player.pauseVideo(); | |
} | |
if (this._player && props.audio !== this.props.audio) { | |
this.props.audio ? this._player.unMute() : this._player.mute(); | |
} | |
} | |
_onReady(event) { | |
this._player = event.target; | |
if (!this.props.audio) { | |
this._player.mute(); | |
} | |
this.props.onReady && this.props.onReady(); | |
} | |
} | |
module.exports = YoutubeComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
react-youtube
is installed (npm i --save react-youtube
),components/
directoryThen include it Idyll markup like,
All of the parameters are optional except for
id
, which must be provided. See all availableoptions
at https://developers.google.com/youtube/player_parameters