next.js/examples/cms-payload/components/Button/index.tsx
index.tsx61 lines1.3 KB
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>
  );
};
Quest for Codev2.0.0
/
SIGN IN