|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useRef } from "react"; |
| 4 | +import { useState } from "react"; |
| 5 | +import { AnimatePresence, motion } from "motion/react"; |
| 6 | +import { Button, Dialog, DialogBackdrop, DialogPanel } from "@headlessui/react"; |
| 7 | + |
| 8 | +export function SignUpForm() { |
| 9 | + return ( |
| 10 | + <form className="flex" method="POST" action="https://app.kit.com/forms/7712177/subscriptions"> |
| 11 | + <div className="flex max-w-full items-center rounded-full bg-white"> |
| 12 | + <input |
| 13 | + required |
| 14 | + type="email" |
| 15 | + id="email" |
| 16 | + name="email_address" |
| 17 | + className="w-3xs min-w-0 shrink grow rounded-full bg-transparent px-4 py-2 text-sm/6 text-gray-950 focus:outline-none" |
| 18 | + placeholder="Enter your email" |
| 19 | + aria-label="Email address" |
| 20 | + /> |
| 21 | + <button className="mr-0.5 shrink-0 overflow-hidden rounded-full bg-gray-950 px-3 py-1.5 text-sm/6 font-semibold text-nowrap text-white hover:bg-gray-950/85"> |
| 22 | + Get the course |
| 23 | + </button> |
| 24 | + </div> |
| 25 | + </form> |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +export function HeroActions({ |
| 30 | + onWatchPreview = () => {}, |
| 31 | + onClosePreview = () => {}, |
| 32 | +}: { |
| 33 | + onWatchPreview?: () => void; |
| 34 | + onClosePreview?: () => void; |
| 35 | +}) { |
| 36 | + let [signUpState, setSignUpState] = useState<"closed" | "open">("closed"); |
| 37 | + let input = useRef<HTMLInputElement>(null); |
| 38 | + let getCourseButton = useRef(null); |
| 39 | + let containerRef = useRef<HTMLInputElement>(null); |
| 40 | + let [isDialogOpen, setIsDialogOpen] = useState(false); |
| 41 | + |
| 42 | + const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => { |
| 43 | + if (!input.current) return; |
| 44 | + |
| 45 | + // Check if the related target (where focus is going) is within our container |
| 46 | + if (input.current.value.length === 0 && !containerRef.current?.contains(e.relatedTarget)) { |
| 47 | + setSignUpState("closed"); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="group relative flex gap-4"> |
| 53 | + <div className="isolate flex items-center"> |
| 54 | + <motion.div |
| 55 | + ref={containerRef} |
| 56 | + layout |
| 57 | + transition={{ duration: signUpState === "open" ? 0.1 : 0.2, ease: "circOut" }} |
| 58 | + className="relative flex items-center bg-white data-[state=open]:overflow-hidden" |
| 59 | + style={{ borderRadius: 20 }} |
| 60 | + data-state={signUpState} |
| 61 | + > |
| 62 | + <AnimatePresence mode="popLayout" initial={false}> |
| 63 | + {signUpState === "open" && ( |
| 64 | + <motion.form |
| 65 | + action="https://app.kit.com/forms/7712177/subscriptions" |
| 66 | + method="POST" |
| 67 | + className="flex items-center" |
| 68 | + layout |
| 69 | + onKeyDown={(e) => { |
| 70 | + if (e.key === "Escape" && input.current) { |
| 71 | + input.current.value = ""; |
| 72 | + setSignUpState("closed"); |
| 73 | + } |
| 74 | + }} |
| 75 | + exit={{ |
| 76 | + opacity: 0, |
| 77 | + transition: { |
| 78 | + duration: 0.2, |
| 79 | + }, |
| 80 | + }} |
| 81 | + initial={{ |
| 82 | + opacity: 0, |
| 83 | + }} |
| 84 | + animate={{ |
| 85 | + opacity: 1, |
| 86 | + }} |
| 87 | + transition={{ duration: 0.1 }} |
| 88 | + > |
| 89 | + <input |
| 90 | + autoFocus |
| 91 | + required |
| 92 | + ref={input} |
| 93 | + onBlur={handleBlur} |
| 94 | + type="email" |
| 95 | + name="email_address" |
| 96 | + className="w-3xs rounded-full bg-transparent px-4 py-2 text-sm/6 text-gray-950 focus:outline-none" |
| 97 | + placeholder="Enter your email" |
| 98 | + aria-label="Email address" |
| 99 | + /> |
| 100 | + <button |
| 101 | + type="submit" |
| 102 | + className="mr-0.5 shrink-0 overflow-hidden rounded-full bg-gray-950 px-3 py-1.5 text-sm/6 font-semibold text-nowrap text-white hover:bg-gray-950/85" |
| 103 | + > |
| 104 | + Sign up |
| 105 | + </button> |
| 106 | + </motion.form> |
| 107 | + )} |
| 108 | + {signUpState === "closed" && ( |
| 109 | + <motion.button |
| 110 | + layout |
| 111 | + ref={getCourseButton} |
| 112 | + key="get-course-button" |
| 113 | + onClick={() => { |
| 114 | + setSignUpState("open"); |
| 115 | + }} |
| 116 | + type="button" |
| 117 | + className="inline-flex rounded-full bg-transparent px-4 py-2 text-sm/6 font-semibold text-gray-950 hover:bg-gray-100 focus:outline-none focus-visible:outline focus-visible:outline-offset-2 focus-visible:outline-white focus-visible:outline-solid" |
| 118 | + exit={{ |
| 119 | + opacity: 0, |
| 120 | + transition: { |
| 121 | + duration: 0.05, |
| 122 | + }, |
| 123 | + }} |
| 124 | + initial={{ |
| 125 | + opacity: 0, |
| 126 | + }} |
| 127 | + animate={{ |
| 128 | + opacity: 1, |
| 129 | + }} |
| 130 | + transition={{ duration: 0.2 }} |
| 131 | + > |
| 132 | + Get the free course → |
| 133 | + </motion.button> |
| 134 | + )} |
| 135 | + </AnimatePresence> |
| 136 | + </motion.div> |
| 137 | + </div> |
| 138 | + <AnimatePresence initial={false}> |
| 139 | + {signUpState === "closed" && ( |
| 140 | + <Button |
| 141 | + as={motion.button} |
| 142 | + onClick={() => { |
| 143 | + onWatchPreview(); |
| 144 | + setIsDialogOpen(true); |
| 145 | + }} |
| 146 | + layout |
| 147 | + transition={{ duration: 0.3 }} |
| 148 | + className="inline-flex flex-nowrap items-baseline gap-1.5 self-center rounded-full bg-white/25 px-4 py-2 pl-3 text-sm/6 font-semibold whitespace-nowrap text-white hover:bg-white/30 focus:not-data-focus:outline-none" |
| 149 | + initial={{ |
| 150 | + opacity: 0, |
| 151 | + }} |
| 152 | + animate={{ |
| 153 | + opacity: 1, |
| 154 | + }} |
| 155 | + exit={{ |
| 156 | + opacity: 0, |
| 157 | + }} |
| 158 | + > |
| 159 | + <svg width={20} height={20} fill="none" className="self-center stroke-white"> |
| 160 | + <path d="M19 10a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" /> |
| 161 | + <path |
| 162 | + d="M13.91 9.67a.37.37 0 0 1 0 .66l-5.6 3.11a.38.38 0 0 1-.56-.33V6.9c0-.29.3-.47.56-.33l5.6 3.11Z" |
| 163 | + strokeLinecap="round" |
| 164 | + strokeLinejoin="round" |
| 165 | + /> |
| 166 | + </svg> |
| 167 | + <span> |
| 168 | + Watch <span className="max-sm:hidden">the</span> intro <span className="max-sm:hidden">video</span> |
| 169 | + </span> |
| 170 | + </Button> |
| 171 | + )} |
| 172 | + </AnimatePresence> |
| 173 | + <Dialog |
| 174 | + open={isDialogOpen} |
| 175 | + onClose={() => { |
| 176 | + setIsDialogOpen(false); |
| 177 | + onClosePreview(); |
| 178 | + }} |
| 179 | + > |
| 180 | + <DialogBackdrop className="fixed inset-0 bg-black/85" /> |
| 181 | + <div className="fixed inset-0 grid place-items-center"> |
| 182 | + <DialogPanel className="w-full max-w-7xl p-4 sm:p-8"> |
| 183 | + <video autoPlay controls className="aspect-video w-full rounded-2xl"> |
| 184 | + <source src="https://assets.tailwindcss.com/build-uis-that-dont-suck/intro.mp4" type="video/mp4" /> |
| 185 | + </video> |
| 186 | + </DialogPanel> |
| 187 | + </div> |
| 188 | + </Dialog> |
| 189 | + </div> |
| 190 | + ); |
| 191 | +} |
0 commit comments