Skip to content

Commit

Permalink
Merge pull request #111 from Brandcast/pass-node-to-YT-instead-of-ID
Browse files Browse the repository at this point in the history
Pass DOM node to YouTube Player instead of ID
  • Loading branch information
cookpete authored Oct 6, 2016
2 parents 1da323f + 8980133 commit b04fa4a
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/players/YouTube.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const SDK_URL = 'https://www.youtube.com/iframe_api'
const SDK_GLOBAL = 'YT'
const SDK_GLOBAL_READY = 'onYouTubeIframeAPIReady'
const MATCH_URL = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/
const PLAYER_ID = 'youtube-player'
const BLANK_VIDEO_URL = 'https://www.youtube.com/watch?v=GlCmAC4MHek'
const DEFAULT_PLAYER_VARS = {
autoplay: 0,
Expand All @@ -18,14 +17,11 @@ const DEFAULT_PLAYER_VARS = {
iv_load_policy: 3
}

let playerIdCount = 0

export default class YouTube extends Base {
static displayName = 'YouTube'
static canPlay (url) {
return MATCH_URL.test(url)
}
playerId = PLAYER_ID + '-' + playerIdCount++
componentDidMount () {
const { url, youtubeConfig } = this.props
if (!url && youtubeConfig.preload) {
Expand Down Expand Up @@ -65,7 +61,7 @@ export default class YouTube extends Base {
}
this.loadingSDK = true
this.getSDK().then(YT => {
this.player = new YT.Player(this.playerId, {
this.player = new YT.Player(this.refNode, {
width: '100%',
height: '100%',
videoId: id,
Expand Down Expand Up @@ -136,14 +132,17 @@ export default class YouTube extends Base {
if (!this.isReady || !this.player.getVideoLoadedFraction) return null
return this.player.getVideoLoadedFraction()
}
setRefNode = (node) => {
this.refNode = node
}
render () {
const style = {
height: '100%',
display: this.props.url ? 'block' : 'none'
}
return (
<div style={style}>
<div id={this.playerId} />
<div ref={this.setRefNode} />
</div>
)
}
Expand Down

0 comments on commit b04fa4a

Please sign in to comment.