File size: 1,360 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Link from "next/link";
import React, { ElementType } from "react";
import { useBackgroundColor } from "../BackgroundColor";
import { Chevron } from "../icons/Chevron";
import classes from "./index.module.scss";

export type Props = {
  label?: string;
  appearance?: "default" | "primary" | "secondary";
  el?: "button" | "link" | "a";
  onClick?: () => void;
  href?: string;
  newTab?: boolean;
  className?: string;
};

export const Button: React.FC<Props> = ({
  el = "button",
  label,
  newTab,
  href,
  appearance,
  className: classNameFromProps,
}) => {
  const backgroundColor = useBackgroundColor();
  const newTabProps = newTab
    ? { target: "_blank", rel: "noopener noreferrer" }
    : {};
  const className = [
    classNameFromProps,
    classes[`appearance--${appearance}`],
    classes[`${appearance}--${backgroundColor}`],
    classes.button,
  ]
    .filter(Boolean)
    .join(" ");

  const content = (
    <div className={classes.content}>
      <Chevron />
      <span className={classes.label}>{label}</span>
    </div>
  );

  if (el === "link") {
    return (
      <Link {...newTabProps} href={href || ""} className={className}>
        {content}
      </Link>
    );
  }

  const Element: ElementType = el;

  return (
    <Element href={href} className={className} {...newTabProps}>
      {content}
    </Element>
  );
};