| 1 |
import React, { useEffect, useState } from "react"; |
| 2 |
|
| 3 |
/** |
| 4 |
* YouTube embed component. |
| 5 |
* |
| 6 |
* @param {Object} props component properties |
| 7 |
* @param {string} props.videoId video id |
| 8 |
* @param {number |null} props.width embed width |
| 9 |
* @param {number |null} props.height embed height |
| 10 |
* @class |
| 11 |
*/ |
| 12 |
function YouTubeEmbed({ videoId, width = null, height = null }) { |
| 13 |
const [embedUrl, setEmbedUrl] = useState(null); |
| 14 |
|
| 15 |
const defaults = { |
| 16 |
width: "100", |
| 17 |
height: "100", |
| 18 |
}; |
| 19 |
|
| 20 |
/** |
| 21 |
* useEffect hook. |
| 22 |
*/ |
| 23 |
useEffect(() => { |
| 24 |
const generatedEmbedUrl = `https://www.youtube.com/embed/${videoId}`; |
| 25 |
setEmbedUrl(generatedEmbedUrl); |
| 26 |
}, []); |
| 27 |
|
| 28 |
return ( |
| 29 |
<div className={"tableberg-youtube-embed"}> |
| 30 |
<iframe |
| 31 |
width={width || defaults.width} |
| 32 |
height={height || defaults.height} |
| 33 |
src={embedUrl} |
| 34 |
title="YouTube video player" |
| 35 |
allow="picture-in-picture; web-share; fullscreen" |
| 36 |
></iframe> |
| 37 |
</div> |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @module YouTubeEmbed |
| 43 |
*/ |
| 44 |
export default YouTubeEmbed; |
| 45 |
|