Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

2.4 KB
Raw Blame
import { cva } from "class-variance-authority";
import { type IconColor, type IconComponent } from "@langfuse/icons";
import { type ComponentProps, type Ref } from "react";
import { cn } from "@/src/utils/tailwind";

const buttonVariants = cva(
  "inline-flex items-center justify-center rounded-md ring-offset-background transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
  {
    variants: {
      variant: {
        ghost: "hover:bg-accent hover:text-accent-foreground",
        outline:
          "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
        outlineDestructive:
          "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
        subtle: "hover:bg-border aria-expanded:bg-border",
        toolbar:
          "text-muted-foreground hover:bg-accent hover:text-foreground aria-pressed:text-primary rounded-sm",
        destructive:
          "bg-destructive text-destructive-foreground hover:bg-destructive/90",
      },
      size: {
        xs: "size-4 rounded-sm",
        sm: "size-6",
        md: "size-8",
      },
    },
    defaultVariants: {
      variant: "ghost",
      size: "md",
    },
  },
);

type NativeButtonProps = Omit<
  ComponentProps<"button">,
  "aria-label" | "children" | "className" | "style"
>;

type IconButtonProps = NativeButtonProps & {
  icon: IconComponent;
  label: string;
  ref?: Ref<HTMLButtonElement>;
  size?: "xs" | "sm" | "md";
  variant?:
    | "ghost"
    | "outline"
    | "outlineDestructive"
    | "subtle"
    | "toolbar"
    | "destructive";
};

const iconColors = {
  ghost: "default",
  outline: "default",
  outlineDestructive: "destructive",
  subtle: "default",
  toolbar: "inherit",
  destructive: "inherit",
} as const satisfies Record<NonNullable<IconButtonProps["variant"]>, IconColor>;

export function IconButton({
  icon: Icon,
  label,
  ref,
  size,
  type = "button",
  variant,
  ...buttonProps
}: IconButtonProps) {
  return (
    <button
      {...buttonProps}
      aria-label={label}
      className={cn(buttonVariants({ size, variant }))}
      ref={ref}
      type={type}
    >
      <Icon
        size={variant === "toolbar" || size === "xs" ? "sm" : "base"}
        color={iconColors[variant ?? "ghost"]}
        aria-hidden
      />
    </button>
  );
}