components/linyi/liquid-glass-button.tsx
lucide-react
"use client"; import * as React from "react";import { cn } from "@/lib/cn"; export type LiquidGlassButtonVariant = "clear" | "dark" | "accent";export type LiquidGlassButtonSize = "sm" | "md" | "lg"; export interface LiquidGlassButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> { variant?: LiquidGlassButtonVariant; size?: LiquidGlassButtonSize; radius?: number; blur?: number; sheen?: boolean; beam?: boolean;} const SIZE_CLASSES: Record<LiquidGlassButtonSize, string> = { sm: "h-8 px-3.5 text-xs", md: "h-9 px-4 text-xs", lg: "h-11 px-6 text-sm",}; const VARIANT_CLASSES: Record<LiquidGlassButtonVariant, string> = { clear: "lg-clear", dark: "lg-dark", accent: "lg-accent",}; export function LiquidGlassButton({ variant = "accent", size = "md", radius, blur, sheen = true, beam = false, disabled, className, style, onPointerMove, onPointerLeave, children, type = "button", ...props}: LiquidGlassButtonProps) { const handlePointerMove = (e: React.PointerEvent<HTMLButtonElement>) => { if (sheen && !disabled) { const rect = e.currentTarget.getBoundingClientRect(); if (rect.width > 0 && rect.height > 0) { const mx = ((e.clientX - rect.left) / rect.width) * 100; const my = ((e.clientY - rect.top) / rect.height) * 100; e.currentTarget.style.setProperty("--mx", `${mx.toFixed(1)}%`); e.currentTarget.style.setProperty("--my", `${my.toFixed(1)}%`); } } onPointerMove?.(e); }; const handlePointerLeave = (e: React.PointerEvent<HTMLButtonElement>) => { if (sheen) { e.currentTarget.style.setProperty("--mx", "50%"); e.currentTarget.style.setProperty("--my", "0%"); } onPointerLeave?.(e); }; const opticalStyle: React.CSSProperties = { ...(radius !== undefined ? { borderRadius: radius >= 30 ? "9999px" : `${radius}px` } : {}), ...(blur !== undefined ? { backdropFilter: `blur(${blur}px) saturate(145%)`, WebkitBackdropFilter: `blur(${blur}px) saturate(145%)`, } : {}), ...style, }; return ( <button type={type} disabled={disabled} onPointerMove={handlePointerMove} onPointerLeave={handlePointerLeave} style={opticalStyle} className={cn( "lg-btn", VARIANT_CLASSES[variant], SIZE_CLASSES[size], beam && "btn-beam", !sheen && "[&::after]:opacity-0", className )} {...props} > {children} </button> );}