"use client"; import { Heart } from "lucide-react"; import { motion, useReducedMotion } from "motion/react"; import { type ComponentPropsWithRef, useState } from "react"; import { DigitSwap } from "@/components/motion/digit-swap"; import { EASE_OUT, SPRING_PRESS } from "@/lib/ease"; import { useMessages } from "@/lib/i18n"; import { useSound } from "@/lib/sound"; import { tapSound } from "@/lib/sounds/tap"; import { cn } from "@/lib/utils"; export const likeButtonMessages = { like: "Like", }; export type LikeButtonMessages = typeof likeButtonMessages; export interface LikeButtonProps extends Omit, "children" | "onChange"> { /** Whether the viewer has liked it, for controlled use. */ liked?: boolean; /** Whether it starts liked when uncontrolled. Default: false */ defaultLiked?: boolean; /** Called with the new state when the button is pressed. */ onLikedChange?: (liked: boolean) => void; /** Likes from everyone else. The viewer's own like is added on top. */ count?: number; /** Override any built-in string, e.g. for translation. */ labels?: Partial; /** Play a soft tap on like and unlike. Off by default; a `SoundProvider` can mute it app-wide. */ sound?: boolean; } // Eight sparks at even angles, alternating near and far so the burst reads as // a ring with depth rather than a flat star. const SPARKS = Array.from({ length: 8 }, (_, index) => { const angle = (index / 8) * Math.PI * 2 - Math.PI / 2; const far = index % 2 === 0; const distance = far ? 15 : 11; return { id: index, x: Math.cos(angle) * distance, y: Math.sin(angle) * distance, size: far ? "size-1" : "size-[3px]", delay: far ? 0.04 : 0.07, }; }); const BLOOM = { duration: 0.32, ease: EASE_OUT } as const; const DRAIN = { duration: 0.18, ease: EASE_OUT } as const; const SPARK = { duration: 0.42, ease: EASE_OUT } as const; /** * A heart toggle. Liking dips the heart, blooms its fill out from the centre * and throws a ring of sparks while the count rolls up; unliking drains the * fill back to the centre. */ export function LikeButton({ liked: likedProp, defaultLiked = false, onLikedChange, count, labels, sound = false, className, onClick, ...rest }: LikeButtonProps) { const t = useMessages("LikeButton", likeButtonMessages, labels); const reduce = useReducedMotion(); const [internal, setInternal] = useState(defaultLiked); const liked = likedProp ?? internal; // Each like gets its own burst; 0 means none has played yet. const [burst, setBurst] = useState(0); const play = useSound(tapSound, { enabled: sound }); return ( ); }