@kubit-ui-web/react-components
Version:
Kubit React Components is a customizable, accessible library of React web components, designed to enhance your application's user experience
197 lines • 8.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Video = exports.VideoUnControlled = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importDefault(require("react"));
const useMediaDevice_1 = require("../../hooks/useMediaDevice/useMediaDevice");
const videoControlled_1 = require("./videoControlled");
const VideoUnControlled = ({ hasInitialOverlay = true, timeToHideButtonsBar = 4000, autoFullScreen = false, onCanPlay, onVolumeChange, onFullScreenClick, onSubtitlesClick, onVolumeButtonClick, onLoadedMetadata, onVideoPause, onVideoPlay, onTimeUpdate, onTogglePlay, ...props }) => {
const ref = react_1.default.useRef(null);
const videoRef = react_1.default.useRef(null);
const device = (0, useMediaDevice_1.useMediaDevice)();
const videoContainerRef = react_1.default.useRef(null);
const [isLoading, setIsLoading] = react_1.default.useState(true);
const [showOverlay, setShowOverlay] = react_1.default.useState(hasInitialOverlay);
const [isFullScreen, setIsFullScreen] = react_1.default.useState(false);
const [isPlaying, setIsPlaying] = react_1.default.useState(false);
const [showScreenButtons, setShowScreenButtons] = react_1.default.useState(true);
const [volume, setVolume] = react_1.default.useState('50');
const [volumeSaved, setVolumeSaved] = react_1.default.useState('50');
const [currentTime, setCurrentTime] = react_1.default.useState(0);
const [duration, setDuration] = react_1.default.useState(0);
const [subtitlesActivated, setSubtitlesActivated] = react_1.default.useState(false);
const [showControls, setShowControls] = react_1.default.useState(true);
// Load video
react_1.default.useEffect(() => {
if (videoRef.current) {
videoRef.current.load();
setIsPlaying(!videoRef.current.paused);
setShowOverlay(hasInitialOverlay);
setShowScreenButtons(true);
}
}, [props.videoSrc]);
// Listen when fullscren changes
const handleFullScreenHasChanged = () => {
if (document.fullscreenElement) {
setIsFullScreen(true);
}
else {
setIsFullScreen(false);
}
};
react_1.default.useEffect(() => {
document.addEventListener('fullscreenchange', handleFullScreenHasChanged);
return () => {
document.removeEventListener('fullscreenchange', handleFullScreenHasChanged);
};
}, []);
// Executed when video is loaded enough to be played
const handleCanPlay = e => {
onCanPlay?.(e);
setIsLoading(false);
};
const handleLoadedMetadata = e => {
if (!videoRef.current) {
return;
}
onLoadedMetadata?.(e);
setDuration(videoRef.current.duration);
};
// Press play/pause video
const handleTogglePlay = e => {
if (!videoRef.current || isLoading) {
return;
}
onTogglePlay?.(e, videoRef.current.paused);
if (videoRef.current.paused) {
videoRef.current.play();
}
else {
videoRef.current.pause();
}
// The screens buttons only appear at the beginning, as soon as the user presses play,
// neither the play nor the overlay appears again, even if the user presses pause.
// (It is shown again when videoSrc changes)
if (showScreenButtons) {
setShowScreenButtons(false);
}
};
// Executed when video is played
const handlePlayVideo = e => {
onVideoPlay?.(e);
// Hide overlay when video is played
setShowOverlay(false);
setIsPlaying(true);
autoFullScreen &&
!isFullScreen &&
handleClickFullScreen({});
};
// Executed when video is paused
const handlePauseVideo = e => {
onVideoPause?.(e);
setIsPlaying(false);
};
// Executed when video time is updated
const handleTimeUpdate = e => {
if (!videoRef.current) {
return;
}
onTimeUpdate?.(e);
setCurrentTime(videoRef.current.currentTime);
};
// Excecuted when moving the progress bar
const handleChangeProgressBar = value => {
if (!videoRef.current) {
return;
}
videoRef.current.currentTime = (videoRef.current.duration * value) / 100;
};
const isPausedBfDrag = react_1.default.useRef(true);
// Excecuted when dragging progressbar
const handleDragStartProgressBar = () => {
if (!videoRef.current) {
return;
}
isPausedBfDrag.current = videoRef.current.paused;
videoRef.current.pause();
};
// Excecuted when strop dragging progressbar
const handleDragEndProgressBar = () => {
if (!videoRef.current) {
return;
}
if (!isPausedBfDrag.current) {
videoRef.current.play();
}
};
const handleChangeVolume = event => {
onVolumeChange?.(event);
setVolume(event.target.value);
setVolumeSaved(event.target.value);
if (videoRef.current) {
//Change volume on video
videoRef.current.volume = parseFloat(event.target.value) / 100;
}
};
const handleClickVolumeButton = e => {
if (videoRef.current) {
if (volume !== '0') {
onVolumeButtonClick?.(e, '0');
setVolume('0');
//Mute video
videoRef.current.volume = 0;
}
else {
onVolumeButtonClick?.(e, volumeSaved);
setVolume(volumeSaved);
//Change volume on video
videoRef.current.volume = parseFloat(volumeSaved) / 100;
}
}
};
const handleClickFullScreen = e => {
if (!videoContainerRef.current) {
return;
}
onFullScreenClick?.(e, !document.fullscreenElement);
if (document.fullscreenElement) {
// Video exits fullscreen
document.exitFullscreen?.();
}
else {
// Video goes to fullscreen
videoContainerRef.current.requestFullscreen();
}
};
const handleClickSubtitles = e => {
const _subtitlesActivated = !subtitlesActivated;
onSubtitlesClick?.(e, _subtitlesActivated);
setSubtitlesActivated(_subtitlesActivated);
};
// Hide controls when timeToHideButtonsBar passes
const timeoutShowHideButtonsBar = react_1.default.useRef();
const handleShowHidControls = () => {
setShowControls(true);
if (timeToHideButtonsBar) {
clearTimeout(timeoutShowHideButtonsBar.current);
timeoutShowHideButtonsBar.current = setTimeout(() => {
setShowControls(false);
}, timeToHideButtonsBar);
}
};
react_1.default.useEffect(() => {
if (timeToHideButtonsBar) {
window.addEventListener('mousemove', handleShowHidControls);
}
return () => {
window.removeEventListener('mousemove', handleShowHidControls);
};
}, [timeToHideButtonsBar]);
return ((0, jsx_runtime_1.jsx)(videoControlled_1.VideoControlled, { ...props, ref: ref, currentTime: currentTime, duration: duration, fullScreen: isFullScreen, hasOverlay: showOverlay, loading: isLoading, playing: isPlaying, showControls: showControls, showScreenButtons: showScreenButtons, subtitlesActivated: subtitlesActivated, videoContainerRef: videoContainerRef, videoRef: videoRef, volume: volume, onCanPlay: handleCanPlay, onFullScreenClick: handleClickFullScreen, onLoadedMetadata: handleLoadedMetadata, onProgressBarChange: handleChangeProgressBar, onProgressBarDragEnd: handleDragEndProgressBar, onProgressBarDragStart: handleDragStartProgressBar, onSubtitlesClick: handleClickSubtitles, onTimeUpdate: handleTimeUpdate, onTogglePlay: handleTogglePlay, onVideoPause: handlePauseVideo, onVideoPlay: handlePlayVideo, onVolumeButtonClick: handleClickVolumeButton, onVolumeChange: handleChangeVolume }));
};
exports.VideoUnControlled = VideoUnControlled;
exports.Video = exports.VideoUnControlled;
//# sourceMappingURL=videoUnControlled.js.map