Skip to content

Commit

Permalink
feat: adds play video on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
malcodeman committed Nov 19, 2024
1 parent 37e2701 commit 15e90c4
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion app/_components/Post.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Link from "next/link";
import { useRef } from "react";
import { FcVideoCall, FcImageFile, FcGallery } from "react-icons/fc";

type Props = {
Expand Down Expand Up @@ -29,12 +30,32 @@ const renderIcon = ({

export default function Post(props: Props) {
const { url, isVideo, href, isGallery, title } = props;
const videoRef = useRef<HTMLVideoElement>(null);

const handlePlay = () => {
videoRef.current?.play();
};

const handlePauseAndReset = () => {
if (videoRef.current) {
videoRef.current.pause();
videoRef.current.currentTime = 0;
}
};

return (
<Link href={href} aria-label={title} data-testid="post">
<div className="relative aspect-square h-full w-full">
{isVideo ? (
<video src={url} className="h-full w-full object-cover" />
<video
ref={videoRef}
src={url}
onMouseEnter={handlePlay}
onMouseLeave={handlePauseAndReset}
onTouchStart={handlePlay}
onTouchEnd={handlePauseAndReset}
className="h-full w-full object-cover"
/>
) : (
<img src={url} alt="" className="h-full w-full object-cover" />
)}
Expand Down

0 comments on commit 15e90c4

Please sign in to comment.