Spaces:
Paused
Paused
feat: complete HUBAPI overhaul (Security, i18n RTL, Dark/Light Mode, A11y & Performance fixes)
587459d Download components/ExitIntentPopup.tsx from samirhosninet/HubAPI: direct link, hf CLI and curl.
- Browser
- Download file 10.6 kB
-
https://huggingface.co/spaces/samirhosninet/HubAPI/resolve/main/components/ExitIntentPopup.tsx
- Command line
-
hf download hf://spaces/samirhosninet/HubAPI/components/ExitIntentPopup.tsx
-
curl -L -o ExitIntentPopup.tsx https://huggingface.co/spaces/samirhosninet/HubAPI/resolve/main/components/ExitIntentPopup.tsx
10.6 kB
| "use client"; | |
| import { useEffect, useState, useRef, useCallback } from "react"; | |
| import { motion, AnimatePresence } from "framer-motion"; | |
| import { X, AlertTriangle } from "lucide-react"; | |
| import { useLanguage } from "@/components/LanguageContext"; | |
| import { useSplitFlapAudio } from "@/components/ui/split-flap-text"; | |
| import { registerWaitlist } from "@/app/actions/waitlist"; | |
| import { ShimmerButton } from "@/components/ui/shimmer-button"; | |
| import { CyberTicketModal } from "@/components/CyberTicketModal"; | |
| export function ExitIntentPopup() { | |
| const { t, language } = useLanguage(); | |
| const audio = useSplitFlapAudio(); | |
| const [isOpen, setIsOpen] = useState(false); | |
| const [email, setEmail] = useState(""); | |
| const [isSubmitting, setIsSubmitting] = useState(false); | |
| const [isTicketOpen, setIsTicketOpen] = useState(false); | |
| const [error, setError] = useState<string | null>(null); | |
| const modalRef = useRef<HTMLDivElement>(null); | |
| useEffect(() => { | |
| // If already registered, don't track exit intent | |
| const isRegistered = localStorage.getItem("hubapi_waitlist_registered"); | |
| if (isRegistered === "true") return; | |
| // Check if exit intent was already shown in this session | |
| const exitShown = sessionStorage.getItem("hubapi_exit_intent_shown"); | |
| if (exitShown === "true") return; | |
| const handleMouseLeave = (e: MouseEvent) => { | |
| // Trigger when mouse moves out of window top boundary (y < 10) | |
| if (e.clientY < 10) { | |
| setIsOpen(true); | |
| sessionStorage.setItem("hubapi_exit_intent_shown", "true"); | |
| // Play audio warning beep | |
| if (audio && !audio.isMuted) { | |
| audio.playClick(); | |
| } | |
| } | |
| }; | |
| document.addEventListener("mouseleave", handleMouseLeave); | |
| return () => { | |
| document.removeEventListener("mouseleave", handleMouseLeave); | |
| }; | |
| }, [audio]); | |
| const handleClose = useCallback(() => { | |
| setIsOpen(false); | |
| if (audio && !audio.isMuted) { | |
| audio.playClick(); | |
| } | |
| }, [audio]); | |
| // Focus trap and escape key listener | |
| useEffect(() => { | |
| if (!isOpen) return; | |
| const modal = modalRef.current; | |
| if (!modal) return; | |
| // Focus the first element or the modal container itself | |
| const focusableElements = modal.querySelectorAll( | |
| 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])' | |
| ); | |
| const firstElement = focusableElements[0] as HTMLElement; | |
| const lastElement = focusableElements[focusableElements.length - 1] as HTMLElement; | |
| // Save previous active element to restore later | |
| const previousActiveElement = document.activeElement as HTMLElement; | |
| // Set a tiny timeout to ensure animation didn't disrupt focus | |
| const focusTimeout = setTimeout(() => { | |
| if (firstElement) { | |
| firstElement.focus(); | |
| } else { | |
| modal.focus(); | |
| } | |
| }, 50); | |
| const handleKeyDown = (e: KeyboardEvent) => { | |
| if (e.key === "Escape") { | |
| handleClose(); | |
| } | |
| if (e.key !== "Tab") return; | |
| if (e.shiftKey) { | |
| if (document.activeElement === firstElement) { | |
| lastElement.focus(); | |
| e.preventDefault(); | |
| } | |
| } else { | |
| if (document.activeElement === lastElement) { | |
| firstElement.focus(); | |
| e.preventDefault(); | |
| } | |
| } | |
| }; | |
| window.addEventListener("keydown", handleKeyDown); | |
| return () => { | |
| clearTimeout(focusTimeout); | |
| window.removeEventListener("keydown", handleKeyDown); | |
| if (previousActiveElement) { | |
| previousActiveElement.focus(); | |
| } | |
| }; | |
| }, [isOpen, handleClose]); | |
| const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { | |
| e.preventDefault(); | |
| setError(null); | |
| if (!email) { | |
| setError(language === "ar" ? "البريد الإلكتروني مطلوب." : "Email address is required."); | |
| return; | |
| } | |
| const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | |
| if (!emailRegex.test(email)) { | |
| setError(language === "ar" ? "يرجى إدخال بريد إلكتروني صالح." : "Please enter a valid email address."); | |
| return; | |
| } | |
| setIsSubmitting(true); | |
| try { | |
| const formData = new FormData(); | |
| formData.append("email", email); | |
| const result = await registerWaitlist(formData); | |
| if (result.success) { | |
| localStorage.setItem("hubapi_waitlist_registered", "true"); | |
| setIsOpen(false); | |
| setIsTicketOpen(true); | |
| } else { | |
| setError(result.error || "An error occurred. Please try again."); | |
| } | |
| } catch { | |
| setError("Failed to connect to waitlist service."); | |
| } finally { | |
| setIsSubmitting(false); | |
| } | |
| }; | |
| // handleClose is defined above with useCallback for effect compatibility | |
| return ( | |
| <> | |
| <AnimatePresence> | |
| {isOpen && ( | |
| <div className="fixed inset-0 z-[100] flex items-center justify-center p-4"> | |
| {/* Dark glass backdrop */} | |
| <motion.div | |
| initial={{ opacity: 0 }} | |
| animate={{ opacity: 1 }} | |
| exit={{ opacity: 0 }} | |
| onClick={handleClose} | |
| className="absolute inset-0 bg-black/60 backdrop-blur-md" | |
| /> | |
| {/* Popup Dialog container */} | |
| <motion.div | |
| ref={modalRef} | |
| role="dialog" | |
| aria-modal="true" | |
| aria-labelledby="exit-modal-title" | |
| tabIndex={-1} | |
| initial={{ scale: 0.95, opacity: 0, y: 20 }} | |
| animate={{ scale: 1, opacity: 1, y: 0 }} | |
| exit={{ scale: 0.95, opacity: 0, y: 20 }} | |
| className="relative w-full max-w-lg p-8 sm:p-10 rounded-xl border border-primary/20 bg-secondary/95 dark:bg-background/95 backdrop-blur-lg shadow-2xl overflow-hidden [clip-path:polygon(20px_0,100%_0,100%_calc(100%_-_20px),calc(100%_-_20px)_100%,0_100%,0_20px)] focus:outline-none" | |
| > | |
| {/* Glowing Corner Effects */} | |
| <div className="absolute top-0 end-0 w-24 h-24 bg-primary/5 rounded-full blur-2xl pointer-events-none" /> | |
| <div className="absolute bottom-0 start-0 w-24 h-24 bg-primary/5 rounded-full blur-2xl pointer-events-none" /> | |
| {/* Close Button */} | |
| <button | |
| onClick={handleClose} | |
| className="absolute top-4 end-4 h-8 w-8 flex items-center justify-center rounded-lg border border-border hover:border-primary/40 text-foreground hover:text-primary transition-all duration-300 focus-visible:ring-1 focus-visible:ring-primary focus-visible:outline-none" | |
| aria-label="Close modal" | |
| > | |
| <X className="h-4 w-4" /> | |
| </button> | |
| <div className="flex flex-col gap-6 text-center sm:text-start"> | |
| {/* Heading Area */} | |
| <div className="space-y-2"> | |
| <div className="inline-flex items-center rounded-full border border-destructive/20 bg-destructive/10 px-3 py-1 text-[10px] font-mono font-bold tracking-widest text-destructive"> | |
| <span className="w-1.5 h-1.5 bg-destructive rounded-full animate-ping me-2 shadow-[0_0_8px_rgba(239,68,68,0.5)]" /> | |
| EXIT_PREVENTED | |
| </div> | |
| <h3 id="exit-modal-title" className="text-2xl font-extrabold tracking-tight bg-clip-text text-transparent bg-gradient-to-br from-foreground to-muted-foreground mt-2"> | |
| {t("exitTitle")} | |
| </h3> | |
| <p className="text-xs text-muted-foreground/80 font-mono leading-relaxed mt-1"> | |
| {t("exitSubtitle")} | |
| </p> | |
| </div> | |
| {/* Form Integration */} | |
| <form onSubmit={handleSubmit} className="flex flex-col gap-4"> | |
| <div className="relative group"> | |
| <div className={`absolute -inset-0.5 rounded-lg blur opacity-0 group-focus-within:opacity-100 transition duration-500 ${error ? 'bg-destructive/45' : 'bg-primary/20'}`}></div> | |
| <input | |
| id="exit-intent-email-input" | |
| name="exit_intent_email" | |
| aria-label="Email address for exit prevention waitlist" | |
| type="email" | |
| placeholder={t("waitlistPlaceholder")} | |
| disabled={isSubmitting} | |
| value={email} | |
| onChange={(e) => { | |
| setEmail(e.target.value); | |
| if (error) setError(null); | |
| }} | |
| className={`relative w-full h-12 px-5 bg-muted/40 dark:bg-white/5 border font-mono text-xs text-foreground placeholder:text-muted-foreground/40 outline-none focus-visible:outline-none focus-visible:ring-1 transition-colors duration-300 [clip-path:polygon(10px_0,100%_0,100%_calc(100%_-_10px),calc(100%_-_10px)_100%,0_100%,0_10px)] ${ | |
| error | |
| ? 'border-destructive focus-visible:ring-destructive focus-visible:ring-destructive/50' | |
| : 'border-border focus-visible:ring-primary focus-visible:ring-primary/50' | |
| }`} | |
| /> | |
| </div> | |
| <ShimmerButton | |
| type="submit" | |
| disabled={isSubmitting} | |
| borderRadius="6px" | |
| className="w-full h-12 font-mono text-[10px] font-bold tracking-widest" | |
| > | |
| <span>{isSubmitting ? t("processing") : t("exitButton")}</span> | |
| </ShimmerButton> | |
| <button | |
| type="button" | |
| onClick={handleClose} | |
| className="text-[10px] font-mono text-muted-foreground/50 hover:text-foreground transition-colors self-center mt-1" | |
| > | |
| {t("exitNoThanks")} | |
| </button> | |
| </form> | |
| {error && ( | |
| <div className="flex items-center gap-2 font-mono text-[10px] text-destructive animate-fade-in ps-1 self-center"> | |
| <AlertTriangle className="h-3.5 w-3.5 shrink-0" /> | |
| <span>{error}</span> | |
| </div> | |
| )} | |
| </div> | |
| </motion.div> | |
| </div> | |
| )} | |
| </AnimatePresence> | |
| {/* Embedded CyberTicketModal */} | |
| <CyberTicketModal | |
| isOpen={isTicketOpen} | |
| onClose={() => setIsTicketOpen(false)} | |
| email={email} | |
| /> | |
| </> | |
| ); | |
| } | |