Animated Button
nocdn-ui/animated-buttonFreeComponent
A button that smoothly transitions between its primary and secondary states.
Live preview
live · rendered by the registry
Rendered by nocdn/ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://ui.bartoszbak.org/r/animated-button.jsonSource
1"use client"23import { cn } from "@/lib/utils"4import { AnimatePresence, motion } from "motion/react"5import { useEffect, useRef, useState } from "react"67export function AnimatedButton({8 children,9 secondaryChildren,10 className,11 onClick,12 ariaLabel,13 showingSecondary,14 timeout = 1000,15}: {16 children: React.ReactNode17 secondaryChildren?: React.ReactNode18 className?: string19 onClick?: () => void20 ariaLabel: string21 showingSecondary?: boolean22 timeout?: number23}) {24 const [isShowingSecondary, setIsShowingSecondary] = useState(false)25 const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)26 const isControlled = showingSecondary !== undefined2728 useEffect(() => {29 return () => {30 if (timeoutRef.current) {31 clearTimeout(timeoutRef.current)32 }33 }34 }, [])3536 const shouldShowSecondary =37 Boolean(secondaryChildren) && (isControlled ? Boolean(showingSecondary) : isShowingSecondary)3839 return (40 <motion.button41 whileTap={{ scale: 0.95 }}42 tabIndex={0}43 aria-label={ariaLabel}44 className={cn("group flex shrink-0 cursor-pointer items-center justify-center", className)}45 onClick={() => {46 if (shouldShowSecondary) return4748 onClick?.()4950 if (secondaryChildren && !isControlled) {51 if (timeoutRef.current) {52 clearTimeout(timeoutRef.current)53 }54 setIsShowingSecondary(true)55 timeoutRef.current = setTimeout(() => {56 setIsShowingSecondary(false)57 }, timeout)58 }59 }}60 >61 <AnimatePresence mode="popLayout" initial={false}>62 <motion.div63 key={shouldShowSecondary ? "secondary" : "primary"}64 initial={{ opacity: 0, scale: 0.25, filter: "blur(4px)" }}65 animate={{ opacity: 1, scale: 1, filter: "blur(0px)" }}66 exit={{ opacity: 0, scale: 0.25, filter: "blur(4px)" }}67 transition={{68 type: "spring",69 duration: 0.3,70 bounce: 0,71 }}72 className="grid place-content-center"73 >74 {shouldShowSecondary ? secondaryChildren : children}75 </motion.div>76 </AnimatePresence>77 </motion.button>78 )79}
What it pulls in
npm packages
Files it writes
More from nocdn/ui
All 3 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Cornered Buttoncornered-button | nocdn/ui | Components | Free | no deps | |
| Tickerticker | nocdn/ui | Components | Free | 1 dep |
