跳至正文
获取 Pro
东方雅集E.18

墨键

FreeRegistry
components/linyi/ink-button.tsx
"use client";
 
import * as React from "react";
import { cn } from "@/lib/cn";
 
export type InkButtonVariant = "dense" | "wash" | "wipe";
export type InkButtonSize = "sm" | "md" | "lg";
 
/* Brush-edged mask for the wipe stroke: solid body, ragged leading edge with a few dry-brush hairs. */
const BRUSH_WIPE =
"url(\"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 100' preserveAspectRatio='none'><path d='M0 0H178C181 8 176 13 180 21C184 29 177 35 181 43C185 51 178 58 182 66C186 74 179 81 183 88C186 95 181 98 184 100H0Z' fill='white'/><path d='M178 10H190M180 27H196M179 41H186M181 56H199M180 71H189M182 84H197' stroke='white' stroke-width='2.4' stroke-linecap='round'/></svg>\")";
 
/* Dense ink with edge deposition: lighter core, darker rim — the way ink dries on paper. */
const DENSE_BG = "radial-gradient(130% 170% at 50% 42%, var(--ink-3) 0%, var(--ink-2) 50%, var(--ink-1) 100%)";
 
const SIZE: Record<InkButtonSize, string> = {
sm: "h-8 px-3.5 text-[11px] gap-1.5",
md: "h-9 px-4.5 text-xs gap-1.5",
lg: "h-11 px-6 text-sm gap-2",
};
 
export interface InkButtonProps extends React.ComponentPropsWithRef<"button"> {
/** dense 浓墨 · wash 淡墨晕染 · wipe 描边,悬停时一笔墨扫过 */
variant?: InkButtonVariant;
size?: InkButtonSize;
/** Edge deposition (边缘积墨). Turn off for a flat fill. */
deposition?: boolean;
}
 
/**
* 墨键 —— buttons drawn in ink. Colours come from the 墨分五色 theme tokens, so they invert in dark mode.
*/
export function InkButton({
variant = "dense",
size = "md",
deposition = true,
className,
style,
children,
type = "button",
...props
}: InkButtonProps) {
const surface: React.CSSProperties =
variant === "dense"
? deposition
? {
background: DENSE_BG,
boxShadow: "inset 0 0 0 1px var(--ink-1), inset 0 0 7px rgba(0,0,0,0.45), 0 1px 2px rgba(0,0,0,0.18)",
}
: { background: "var(--ink-2)" }
: variant === "wash"
? deposition
? {
background:
"radial-gradient(130% 180% at 50% 45%, transparent 38%, color-mix(in srgb, var(--ink-4) 34%, transparent) 100%), color-mix(in srgb, var(--ink-4) 16%, transparent)",
boxShadow: "inset 0 0 0 1px color-mix(in srgb, var(--ink-3) 55%, transparent)",
}
: { background: "color-mix(in srgb, var(--ink-4) 22%, transparent)" }
: { boxShadow: "inset 0 0 0 1px var(--ink-3)" };
 
return (
<button
type={type}
className={cn(
"group relative isolate inline-flex items-center justify-center overflow-hidden rounded-full font-medium select-none cursor-pointer whitespace-nowrap",
"transition-[transform,color,box-shadow] duration-300 ease-[cubic-bezier(0.34,1.56,0.64,1)]",
"hover:-translate-y-px active:translate-y-px active:scale-[0.975] active:duration-75",
"disabled:opacity-45 disabled:pointer-events-none",
"focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-seal",
variant === "dense" ? "text-ink-paper" : "text-ink-1",
variant === "wipe" && "hover:text-ink-paper focus-visible:text-ink-paper",
SIZE[size],
className
)}
style={{ ...surface, ...style }}
{...props}
>
{variant === "wipe" && (
<span
aria-hidden="true"
className="absolute inset-y-0 left-0 -right-[14%] -z-10 -translate-x-[104%] transition-transform duration-500 ease-[cubic-bezier(0.22,1,0.36,1)] group-hover:translate-x-0 group-focus-visible:translate-x-0 motion-reduce:transition-none"
style={{
background: deposition ? DENSE_BG : "var(--ink-2)",
WebkitMaskImage: BRUSH_WIPE,
maskImage: BRUSH_WIPE,
WebkitMaskSize: "100% 100%",
maskSize: "100% 100%",
WebkitMaskRepeat: "no-repeat",
maskRepeat: "no-repeat",
}}
/>
)}
{children}
</button>
);
}