'use client';
import { createContext, useContext, useCallback, useState, ReactNode } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { Check } from 'lucide-react';
/**
* OptimisticUI — provides instant feedback for user actions.
*
* - Cart fly animation: when "Add to Cart" is clicked, a checkmark
* bursts with a bouncy spring animation at the click location.
* - Like burst: when a like button is clicked, a small confetti-like
* burst plays.
*
* Usage:
* const { burst } = useOptimisticUI();
*
*/
interface Burst {
id: number;
x: number;
y: number;
type: 'check' | 'heart';
}
const OptimisticContext = createContext<{ burst: (x: number, y: number, type?: 'check' | 'heart') => void }>({
burst: () => {},
});
export function OptimisticUIProvider({ children }: { children: ReactNode }) {
const [bursts, setBursts] = useState([]);
const burst = useCallback((x: number, y: number, type: 'check' | 'heart' = 'check') => {
const id = Date.now() + Math.random();
setBursts(prev => [...prev, { id, x, y, type }]);
setTimeout(() => {
setBursts(prev => prev.filter(b => b.id !== id));
}, 800);
}, []);
return (
{children}
{/* Burst animations overlay */}
{bursts.map(b => (
{b.type === 'check' ? (
) : (
)}
))}
);
}
export function useOptimisticUI() {
return useContext(OptimisticContext);
}