1"use client"
2
3import { useState } from "react"
4import { Play, XIcon } from "lucide-react"
5import { AnimatePresence, motion } from "motion/react"
6
7import { cn } from "@/lib/utils"
8
9type AnimationStyle =
10 | "from-bottom"
11 | "from-center"
12 | "from-top"
13 | "from-left"
14 | "from-right"
15 | "fade"
16 | "top-in-bottom-out"
17 | "left-in-right-out"
18
19interface HeroVideoProps {
20 animationStyle?: AnimationStyle
21 videoSrc: string
22 thumbnailSrc: string
23 thumbnailAlt?: string
24 className?: string
25}
26
27const animationVariants = {
28 "from-bottom": {
29 initial: { y: "100%", opacity: 0 },
30 animate: { y: 0, opacity: 1 },
31 exit: { y: "100%", opacity: 0 },
32 },
33 "from-center": {
34 initial: { scale: 0.5, opacity: 0 },
35 animate: { scale: 1, opacity: 1 },
36 exit: { scale: 0.5, opacity: 0 },
37 },
38 "from-top": {
39 initial: { y: "-100%", opacity: 0 },
40 animate: { y: 0, opacity: 1 },
41 exit: { y: "-100%", opacity: 0 },
42 },
43 "from-left": {
44 initial: { x: "-100%", opacity: 0 },
45 animate: { x: 0, opacity: 1 },
46 exit: { x: "-100%", opacity: 0 },
47 },
48 "from-right": {
49 initial: { x: "100%", opacity: 0 },
50 animate: { x: 0, opacity: 1 },
51 exit: { x: "100%", opacity: 0 },
52 },
53 fade: {
54 initial: { opacity: 0 },
55 animate: { opacity: 1 },
56 exit: { opacity: 0 },
57 },
58 "top-in-bottom-out": {
59 initial: { y: "-100%", opacity: 0 },
60 animate: { y: 0, opacity: 1 },
61 exit: { y: "100%", opacity: 0 },
62 },
63 "left-in-right-out": {
64 initial: { x: "-100%", opacity: 0 },
65 animate: { x: 0, opacity: 1 },
66 exit: { x: "100%", opacity: 0 },
67 },
68}
69
70export function HeroVideoDialog({
71 animationStyle = "from-center",
72 videoSrc,
73 thumbnailSrc,
74 thumbnailAlt = "Video thumbnail",
75 className,
76}: HeroVideoProps) {
77 const [isVideoOpen, setIsVideoOpen] = useState(false)
78 const selectedAnimation = animationVariants[animationStyle]
79
80 return (
81 <div className={cn("relative", className)}>
82 <button
83 type="button"
84 aria-label="Play video"
85 className="group relative cursor-pointer border-0 bg-transparent p-0"
86 onClick={() => setIsVideoOpen(true)}
87 >
88 <img
89 src={thumbnailSrc}
90 alt={thumbnailAlt}
91 width={1920}
92 height={1080}
93 className="w-full rounded-md border shadow-lg transition-all duration-200 ease-out group-hover:brightness-[0.8]"
94 />
95 <div className="absolute inset-0 flex scale-[0.9] items-center justify-center rounded-2xl transition-all duration-200 ease-out group-hover:scale-100">
96 <div className="bg-primary/10 flex size-28 items-center justify-center rounded-full backdrop-blur-md">
97 <div
98 className={`from-primary/30 to-primary relative flex size-20 scale-100 items-center justify-center rounded-full bg-linear-to-b shadow-md transition-all duration-200 ease-out group-hover:scale-[1.2]`}
99 >
100 <Play
101 className="size-8 scale-100 fill-white text-white transition-transform duration-200 ease-out group-hover:scale-105"
102 style={{
103 filter:
104 "drop-shadow(0 4px 3px rgb(0 0 0 / 0.07)) drop-shadow(0 2px 2px rgb(0 0 0 / 0.06))",
105 }}
106 />
107 </div>
108 </div>
109 </div>
110 </button>
111 <AnimatePresence>
112 {isVideoOpen && (
113 <motion.div
114 initial={{ opacity: 0 }}
115 animate={{ opacity: 1 }}
116 role="button"
117 tabIndex={0}
118 onKeyDown={(e) => {
119 if (e.key === "Escape" || e.key === "Enter" || e.key === " ") {
120 setIsVideoOpen(false)
121 }
122 }}
123 onClick={() => setIsVideoOpen(false)}
124 exit={{ opacity: 0 }}
125 className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-md"
126 >
127 <motion.div
128 {...selectedAnimation}
129 transition={{ type: "spring", damping: 30, stiffness: 300 }}
130 className="relative mx-4 aspect-video w-full max-w-4xl md:mx-0"
131 >
132 <motion.button className="absolute -top-16 right-0 rounded-full bg-neutral-900/50 p-2 text-xl text-white ring-1 backdrop-blur-md dark:bg-neutral-100/50 dark:text-black">
133 <XIcon className="size-5" />
134 </motion.button>
135 <div className="relative isolate z-1 size-full overflow-hidden rounded-2xl border-2 border-white">
136 <iframe
137 src={videoSrc}
138 title="Hero Video player"
139 className="mt-0 size-full rounded-2xl"
140 allowFullScreen
141 allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
142 ></iframe>
143 </div>
144 </motion.div>
145 </motion.div>
146 )}
147 </AnimatePresence>
148 </div>
149 )
150}
151