跳至正文
获取 Pro
基础原语U.01

按钮

FreeRegistry
components/linyi/button.tsx
@base-ui/reactclass-variance-authoritylucide-react
"use client";
 
import * as React from "react";
import { useRender } from "@base-ui/react/use-render";
import { mergeProps } from "@base-ui/react/merge-props";
import { cva, type VariantProps } from "class-variance-authority";
import { Loader2 } from "lucide-react";
import { cn } from "@/lib/cn";
 
export const buttonVariants = cva(
"ui-btn outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-canvas disabled:pointer-events-none disabled:opacity-45",
{
variants: {
variant: {
surface: "ui-btn-default",
dark: "ui-btn-solid",
accent: "ui-btn-accent",
clear: "ui-btn-default bg-transparent",
ghost: "ui-btn-ghost",
danger: "ui-btn-danger",
},
size: {
sm: "h-control-sm px-3 text-xs",
md: "h-control-md px-4 text-xs",
lg: "h-control-lg px-5 text-sm",
"icon-sm": "h-control-sm w-control-sm p-0",
icon: "h-control-md w-control-md p-0",
},
radius: {
rect: "rounded-[8px]",
pill: "rounded-pill",
},
},
defaultVariants: {
variant: "surface",
size: "md",
radius: "rect",
},
}
);
 
export interface ButtonProps
extends useRender.ComponentProps<"button">,
VariantProps<typeof buttonVariants> {
loading?: boolean;
}
 
export function Button({
className,
variant = "surface",
size = "md",
radius = "rect",
loading = false,
disabled,
type,
render,
ref,
children,
...props
}: ButtonProps) {
const isDisabled = Boolean(disabled || loading);
 
const content =
loading || children !== undefined ? (
<>
{loading && (
<Loader2 className="w-3.5 h-3.5 animate-spin flex-shrink-0" aria-hidden="true" />
)}
{children}
</>
) : undefined;
 
const defaultProps: useRender.ElementProps<"button"> = {
type: render ? type : (type ?? "button"),
disabled: render ? undefined : isDisabled,
"aria-disabled": isDisabled || undefined,
"aria-busy": loading || undefined,
className: cn(buttonVariants({ variant, size, radius }), className),
...(content !== undefined ? { children: content } : {}),
};
 
return useRender({
defaultTagName: "button",
render,
ref,
props: mergeProps<"button">(defaultProps, props),
});
}