2023-04-16 17:09:04 +03:00
|
|
|
import React, { useCallback, useState } from 'react';
|
|
|
|
|
2023-05-10 13:59:29 +03:00
|
|
|
interface Props {
|
2023-04-16 17:09:04 +03:00
|
|
|
src: string;
|
|
|
|
key: string;
|
|
|
|
alt?: string;
|
|
|
|
lang?: string;
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
onClick?: () => void;
|
2023-05-10 13:59:29 +03:00
|
|
|
}
|
2023-04-16 17:09:04 +03:00
|
|
|
|
|
|
|
export const GIFV: React.FC<Props> = ({
|
|
|
|
src,
|
|
|
|
alt,
|
|
|
|
lang,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
onClick,
|
2023-05-10 00:41:18 +03:00
|
|
|
}) => {
|
2023-04-16 17:09:04 +03:00
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
2023-05-10 00:41:18 +03:00
|
|
|
const handleLoadedData: React.ReactEventHandler<HTMLVideoElement> =
|
|
|
|
useCallback(() => {
|
|
|
|
setLoading(false);
|
|
|
|
}, [setLoading]);
|
2023-04-16 17:09:04 +03:00
|
|
|
|
2023-05-10 00:41:18 +03:00
|
|
|
const handleClick: React.MouseEventHandler = useCallback(
|
|
|
|
(e) => {
|
|
|
|
if (onClick) {
|
|
|
|
e.stopPropagation();
|
|
|
|
onClick();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[onClick]
|
|
|
|
);
|
2023-04-16 17:09:04 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='gifv' style={{ position: 'relative' }}>
|
|
|
|
{loading && (
|
|
|
|
<canvas
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
role='button'
|
|
|
|
tabIndex={0}
|
|
|
|
aria-label={alt}
|
|
|
|
title={alt}
|
|
|
|
lang={lang}
|
|
|
|
onClick={handleClick}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<video
|
|
|
|
src={src}
|
|
|
|
role='button'
|
|
|
|
tabIndex={0}
|
|
|
|
aria-label={alt}
|
|
|
|
title={alt}
|
|
|
|
lang={lang}
|
|
|
|
muted
|
|
|
|
loop
|
|
|
|
autoPlay
|
|
|
|
playsInline
|
|
|
|
onClick={handleClick}
|
|
|
|
onLoadedData={handleLoadedData}
|
|
|
|
style={{ position: loading ? 'absolute' : 'static', top: 0, left: 0 }}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|