|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import ReactDOM from 'react-dom'; |
| 3 | +import YouTube from 'react-youtube'; |
| 4 | + |
| 5 | +import './styles.css'; |
| 6 | + |
| 7 | +const VIDEOS = ['XxVg_s8xAms', '-DX3vJiqxm4']; |
| 8 | + |
| 9 | +function YouTubeComponentExample() { |
| 10 | + const [player, setPlayer] = useState(0); |
| 11 | + const [videoIndex, setVideoIndex] = useState(0); |
| 12 | + const [width, setWidth] = useState(600); |
| 13 | + const [hidden, setHidden] = useState(false); |
| 14 | + const [autoplay, setAutoplay] = useState(false); |
| 15 | + |
| 16 | + return ( |
| 17 | + <div className="App"> |
| 18 | + <div style={{ display: 'flex', marginBottom: '1em' }}> |
| 19 | + <button type="button" onClick={() => player.seekTo(120)}> |
| 20 | + Seek to 2 minutes |
| 21 | + </button> |
| 22 | + <button type="button" onClick={() => setVideoIndex((videoIndex + 1) % VIDEOS.length)}> |
| 23 | + Change video |
| 24 | + </button> |
| 25 | + <label> |
| 26 | + <input |
| 27 | + type="range" |
| 28 | + min="300" |
| 29 | + max="1080" |
| 30 | + value={width} |
| 31 | + onChange={(event) => setWidth(event.currentTarget.value)} |
| 32 | + /> |
| 33 | + Width ({width}px) |
| 34 | + </label> |
| 35 | + <button type="button" onClick={() => setHidden(!hidden)}> |
| 36 | + {hidden ? 'Show' : 'Hide'} |
| 37 | + </button> |
| 38 | + <label> |
| 39 | + <input type="checkbox" value={autoplay} onChange={(event) => setAutoplay(event.currentTarget.checked)} /> |
| 40 | + Autoplaying |
| 41 | + </label> |
| 42 | + </div> |
| 43 | + |
| 44 | + {hidden ? ( |
| 45 | + 'mysterious' |
| 46 | + ) : ( |
| 47 | + <YouTube |
| 48 | + videoId={VIDEOS[videoIndex]} |
| 49 | + autoplay={autoplay} |
| 50 | + width={width} |
| 51 | + height={width * (9 / 16)} |
| 52 | + className="container" |
| 53 | + onReady={(event) => setPlayer(event.target)} |
| 54 | + /> |
| 55 | + )} |
| 56 | + </div> |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +ReactDOM.render(<YouTubeComponentExample />, document.getElementById('app')); |
0 commit comments