"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?.();
codeconst 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, }, }); - Assistant
Conquering a UI Challenge: Building a Slide-and-Hold Confirmation Component 🚀
Recently, I took on the task of creating a "Slide and Hold to Confirm" interaction for my app. The idea was simple: users slide a button and hold it to trigger an important action, adding that extra layer of intentionality.
But implementing it? Not so simple! 🧐
The biggest challenges were handling pointer and touch positions accurately and calculating offsets to ensure smooth motion across all devices. I wrestled with quirky behaviors and unexpected offsets that made the slider jump or stick.
Through persistent debugging and a deep dive into event handling in React, I managed to get it working seamlessly. I learned a ton about touch interactions, gestures, and user experience design along the way.
I'm excited to share a video of the component in action. Let me know what you think!
#React #WebDevelopment #UIUX #LearningByDoing