RotNot / src /components /shared /level-up.tsx
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
6.54 kB
"use client";
/**
* LevelUpOverlay — full-screen celebration overlay that auto-dismisses.
*
* Usage:
* <LevelUpOverlay
* show={showLevelUp}
* level={newLevel}
* title={newTitle} // optional — only shown if changed
* category={category} // for accent color
* onDismiss={() => setShowLevelUp(false)}
* />
*
* Auto-dismisses after 3 seconds. Click anywhere to dismiss early.
*/
import { useEffect, useCallback } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { CATEGORY_COLORS, CATEGORY_LABELS, type Category } from "@/config/xp";
import { nativeHaptics } from "@/lib/native";
export interface LevelUpOverlayProps {
show: boolean;
level: number;
title?: string; // new title if unlocked at this level
category: Category;
onDismiss: () => void;
autoDismissMs?: number; // default 3000
}
/** One CSS-only particle — absolutely positioned, randomised via inline style */
function Particle({ accent, delay, i }: { accent: string; delay: number; i: number }) {
// Distribute particles around the screen using index
const angle = (i / 20) * 360;
const distance = 120 + (i % 5) * 40; // 120–280 px
const dx = Math.round(Math.cos((angle * Math.PI) / 180) * distance);
const dy = Math.round(Math.sin((angle * Math.PI) / 180) * distance);
const size = 6 + (i % 4) * 3; // 6–15 px
const colors = [accent, "#ffffff", "#f59e0b", "#22c55e", "#8b5cf6"];
const color = colors[i % colors.length];
return (
<motion.span
aria-hidden
className="absolute rounded-full"
style={{
width: size,
height: size,
background: color,
top: "50%",
left: "50%",
marginLeft: -size / 2,
marginTop: -size / 2,
opacity: 0,
}}
animate={{
x: [0, dx],
y: [0, dy],
opacity: [0, 1, 0],
scale: [0.3, 1, 0.4],
}}
transition={{
duration: 1.2,
delay,
ease: "easeOut",
times: [0, 0.4, 1],
}}
/>
);
}
export function LevelUpOverlay({
show,
level,
title,
category,
onDismiss,
autoDismissMs = 3000,
}: LevelUpOverlayProps) {
const dismiss = useCallback(() => onDismiss(), [onDismiss]);
useEffect(() => {
if (!show) return;
// Trigger heavy haptic on level-up
nativeHaptics.levelUp();
const t = setTimeout(dismiss, autoDismissMs);
return () => clearTimeout(t);
}, [show, dismiss, autoDismissMs]);
const accent = CATEGORY_COLORS[category];
const catLabel = CATEGORY_LABELS[category];
return (
<AnimatePresence>
{show && (
<motion.div
key="level-up-overlay"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.25 }}
className="fixed inset-0 z-50 flex items-center justify-center"
style={{ background: "rgba(0,0,0,0.75)", backdropFilter: "blur(6px)" }}
onClick={dismiss}
role="dialog"
aria-modal="true"
aria-label={`Level up! You reached level ${level}`}
>
{/* Particle field */}
<div className="relative" aria-hidden>
{Array.from({ length: 20 }).map((_, i) => (
<Particle key={i} i={i} accent={accent} delay={i * 0.03} />
))}
</div>
{/* Content card */}
<motion.div
initial={{ scale: 0.6, opacity: 0, y: 30 }}
animate={{ scale: 1, opacity: 1, y: 0 }}
exit={{ scale: 0.8, opacity: 0, y: -20 }}
transition={{ type: "spring", stiffness: 360, damping: 26, delay: 0.05 }}
className="absolute glass rounded-3xl px-12 py-10 text-center select-none"
style={{
borderColor: accent,
borderWidth: 2,
boxShadow: `0 0 60px ${accent}55, 0 0 120px ${accent}22`,
minWidth: 320,
}}
onClick={(e) => e.stopPropagation()}
>
{/* Glow ring */}
<motion.div
className="absolute inset-0 rounded-3xl pointer-events-none"
style={{ background: `radial-gradient(ellipse at center, ${accent}18 0%, transparent 70%)` }}
animate={{ opacity: [0.6, 1, 0.6] }}
transition={{ duration: 2, repeat: Infinity, ease: "easeInOut" }}
/>
{/* Category badge */}
<p
className="text-xs uppercase tracking-widest mb-3 font-semibold"
style={{ color: accent }}
>
{catLabel}
</p>
{/* ⬆ icon */}
<motion.div
className="text-5xl mb-2"
animate={{ y: [0, -8, 0] }}
transition={{ duration: 1.2, repeat: Infinity, ease: "easeInOut" }}
>
⬆
</motion.div>
{/* Level number */}
<motion.p
className="font-bold mb-1"
style={{
fontFamily: "var(--font-mono)",
fontSize: "5rem",
lineHeight: 1,
color: accent,
textShadow: `0 0 30px ${accent}`,
}}
animate={{ textShadow: [`0 0 20px ${accent}88`, `0 0 50px ${accent}`, `0 0 20px ${accent}88`] }}
transition={{ duration: 1.6, repeat: Infinity, ease: "easeInOut" }}
>
{level}
</motion.p>
<p
className="text-xl font-bold mb-1"
style={{ color: "var(--text-primary)" }}
>
Level Up!
</p>
{title && (
<motion.p
initial={{ opacity: 0, y: 8 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.5 }}
className="mt-3 px-4 py-1.5 rounded-full text-sm font-semibold"
style={{
background: `${accent}22`,
color: accent,
border: `1px solid ${accent}66`,
display: "inline-block",
}}
>
New Title: {title}
</motion.p>
)}
<p
className="mt-5 text-xs"
style={{ color: "var(--text-muted)" }}
>
Click anywhere to continue
</p>
</motion.div>
</motion.div>
)}
</AnimatePresence>
);
}