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