File size: 1,973 Bytes
75fefa7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Children, ButtonHTMLAttributes } from "react";

import { cn } from "@/utils/cn";

interface Props extends ButtonHTMLAttributes<HTMLButtonElement> {
  variant?: "primary" | "secondary" | "tertiary" | "playground" | "destructive";
  size?: "default" | "large";
  disabled?: boolean;
}

export default function Button({
  variant = "primary",
  size = "default",
  disabled,
  ...attrs
}: Props) {
  const children = handleChildren(attrs.children);

  return (
    <button
      {...attrs}
      type={attrs.type ?? "button"}
      className={cn(
        attrs.className,
        "[&>span]:px-6 flex items-center justify-center button relative [&>*]:relative",
        "text-label-medium lg-max:[&_svg]:size-24",
        `button-${variant} group/button`,
        {
          "rounded-8 p-6": size === "default",
          "rounded-10 p-8 gap-2": size === "large",

          "text-accent-white active:[scale:0.995]": variant === "primary",
          "text-accent-black active:[scale:0.99] active:bg-black-alpha-7": [
            "secondary",
            "tertiary",
            "playground",
          ].includes(variant),
          "bg-black-alpha-4 hover:bg-black-alpha-6": variant === "secondary",
          "hover:bg-black-alpha-4": variant === "tertiary",
        },
        variant === "playground" && [
          "inside-border before:border-black-alpha-4",
          disabled
            ? "before:opacity-0 bg-black-alpha-4 text-black-alpha-24"
            : "hover:bg-black-alpha-4 hover:before:opacity-0 active:before:opacity-0",
        ],
      )}
      disabled={disabled}
    >
      {variant === "primary" && (
        <div className="overlay button-background !absolute" />
      )}

      {children}
    </button>
  );
}

const handleChildren = (children: React.ReactNode) => {
  return Children.toArray(children).map((child) => {
    if (typeof child === "string") {
      return <span key={child}>{child}</span>;
    }

    return child;
  });
};