"use client"; import useHaptic from "@/lib/hooks/useHaptics"; import { cn } from…
- User
"use client"; import useHaptic from "@/lib/hooks/useHaptics"; import { cn } from "@/lib/utils"; import { useResizeObserver } from "@mantine/hooks"; import { motion, useMotionValue, animate, useTransform, MotionConfig, AnimatePresence, useMotionValueEvent, useSpring, Transition, ValueAnimationTransition, SpringOptions, } from "framer-motion"; import React, { PropsWithChildren, useCallback, useEffect, useRef, useState, } from "react";
interface SlideToConfirmProps extends PropsWithChildren { onSlideComplete?: () => void; onLongPress?: () => void; holdTreshold?: number; } export function SlideToConfirm(props: SlideToConfirmProps) { const { onSlideComplete, onLongPress, holdTreshold = 500 } = props;
const { triggerHapticFeedback, isHapticSupported } = useHaptic();
const [isSlideComplete, setIsSlideComplete] = useState(false); const [isLongPressed, setIsLongPressed] = useState(false);
const clientX = useMotionValue(0);
const thumbWrapperWidth = useMotionValue(0); const thumbX = useMotionValue(0);
const [refThumbWrapper, thumbWrapperRect] = useResizeObserver(); const [refThumb, thumbRect] = useResizeObserver(); const [refTrack, trackRect] = useResizeObserver();
const [isDragging, setIsDragging] = useState(false);
const startRef = useRef({ x: 0, width: thumbWrapperRect.height }); const [thumbXMax, setThumbXMax] = useState(0);
const longPressTimerRef = useRef<NodeJS.Timeout | null>(null); const longPressThreshold = holdTreshold;
const transition: Transition = !isSlideComplete ? { type: "spring", stiffness: 100, damping: 7, mass: 0.2, } : { type: "tween", duration: 0.27, ease: "easeOut", };
const valueTransition: ValueAnimationTransition = { ...transition };
useEffect(() => { setThumbXMax( trackRect.width - thumbRect.width - refThumb.current?.offsetLeft * 2, ); startRef.current.width = thumbWrapperRect.height; }, [trackRect, thumbRect, refThumb, thumbWrapperRect.height]);
const handleConfetti = useCallback(async () => { const { clientWidth, clientHeight } = document.documentElement; const boundingBox = refThumb.current?.getBoundingClientRect?.();
const targetY = boundingBox?.y ?? 0; const targetX = boundingBox?.x ?? 0; const targetWidth = boundingBox?.width ?? 0; const targetCenterX = targetX + targetWidth / 2; function randomInRange(min: number, max: number) { return Math.random() * (max - min) + min; } const confetti = (await import("canvas-confetti")).default; confetti({ angle: randomInRange(120, 120), spread: randomInRange(50, 70), particleCount: randomInRange(50, 100), zIndex: 999, ticks: 60, origin: { // y: targetY / clientHeight, x: targetCenterX / clientWidth, }, });}, [refThumb]);
const startLongPressTimer = useCallback(() => { if (longPressTimerRef.current) clearTimeout(longPressTimerRef.current); longPressTimerRef.current = setTimeout(() => { setIsLongPressed(true); onLongPress?.(); triggerHapticFeedback(200); handleConfetti(); }, longPressThreshold); }, [longPressThreshold, onLongPress, triggerHapticFeedback, handleConfetti]);
- Assistant