Slide to Unlock
ncdai-chirags/slide-to-unlockFreeComponent
A sleek, interactive slider inspired by the classic iPhone OS 'slide to unlock' gesture.
Install it
npx shadcn@latest add https://www.chirags.dev/r/slide-to-unlock.jsonSource
1"use client";23import {4 animate,5 motion,6 type MotionValue,7 useMotionValue,8 useTransform,9} from "motion/react";10import {11 type ComponentPropsWithoutRef,12 createContext,13 useCallback,14 useContext,15 useRef,16 useState,17} from "react";1819import { cn } from "@/lib/utils";2021type SlideToUnlockContextValue = {22 x: MotionValue<number>;23 trackRef: React.RefObject<HTMLDivElement | null>;24 isDragging: boolean;25 handleWidth: number;26 textOpacity: MotionValue<number>;27 onDragStart: () => void;28 onDragEnd: () => void;29};3031const SlideToUnlockContext = createContext<SlideToUnlockContextValue | null>(32 null33);3435function useSlideToUnlock() {36 const context = useContext(SlideToUnlockContext);37 if (!context) {38 throw new Error(39 `SlideToUnlock components must be used within SlideToUnlock`40 );41 }42 return context;43}4445export type SlideToUnlockRootProps = React.ComponentProps<"div"> & {46 handleWidth?: number;47 onUnlock?: () => void;48};4950export function SlideToUnlock({51 className,52 handleWidth = 56,53 children,54 onUnlock,55 ...props56}: SlideToUnlockRootProps) {57 const trackRef = useRef<HTMLDivElement>(null);58 const [isDragging, setIsDragging] = useState(false);59 const x = useMotionValue(0);6061 const fadeDistance = handleWidth;62 const textOpacity = useTransform(x, [0, fadeDistance], [1, 0]);6364 const handleDragStart = useCallback(() => {65 setIsDragging(true);66 }, []);6768 const handleDragEnd = useCallback(() => {69 setIsDragging(false);7071 const trackWidth = trackRef.current?.offsetWidth || 0;72 const maxX = trackWidth - handleWidth;7374 if (x.get() >= maxX) {75 onUnlock?.();76 } else {77 animate(x, 0, { type: "spring", bounce: 0, duration: 0.25 });78 }79 }, [x, onUnlock, handleWidth]);8081 return (82 <SlideToUnlockContext.Provider83 value={{84 x,85 trackRef,86 isDragging,87 handleWidth,88 textOpacity,89 onDragStart: handleDragStart,90 onDragEnd: handleDragEnd,91 }}92 >93 <div94 data-slot="slide-to-unlock"95 className={cn(96 "w-[216px] rounded-xl bg-zinc-100 p-1 shadow-inner ring ring-black/5 ring-inset dark:bg-zinc-900 dark:ring-white/10",97 className98 )}99 {...props}100 >101 {children}102 </div>103 </SlideToUnlockContext.Provider>104 );105}106107export type SlideToUnlockTrackProps = React.ComponentProps<"div">;108109export function SlideToUnlockTrack({110 className,111 children,112 ...props113// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from ncdai
All 28 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Apple Hello Effectapple-hello-effect | ncdai | Components | Free | 1 dep | |
| Consent Managerconsent-manager | ncdai | Components | Free | 1 dep · 2 files | |
| Flip Sentencesflip-sentences | ncdai | Components | Free | 1 dep | |
| GitHub Starsgithub-stars | ncdai | Components | Free | no deps | |
| Scroll Fade Effectscroll-fade-effect | ncdai | Components | Free | no deps | |
| Shimmering Textshimmering-text | ncdai | Components | Free | 1 dep | |
| Testimonials Marqueetestimonials-marquee | ncdai | Components | Free | no deps | |
| Theme Switchertheme-switcher | ncdai | Components | Free | 3 deps |
