import { ReactElement, ReactNode, forwardRef } from "react"; import classNames from "classnames"; import { Props as LinkProps } from "../link/Link"; import { Link } from "react-router-dom"; import { getTrackingProps } from "../../utils/getTrackingProps"; import { BUTTON_SIZE } from "./types"; const alignToClassMap = { left: "vuiBaseButton--alignLeft", center: "vuiBaseButton--alignCenter", right: "vuiBaseButton--alignRight" }; export type Props = { children?: ReactNode; icon?: ReactElement | null; iconSide?: "left" | "right"; align?: "left" | "center" | "right"; className?: string; size?: (typeof BUTTON_SIZE)[number]; fullWidth?: boolean; isSelected?: boolean; isInert?: boolean; isDisabled?: boolean; onClick?: (e: React.MouseEvent) => void; href?: LinkProps["href"]; target?: LinkProps["target"]; track?: LinkProps["track"]; htmlFor?: string; tabIndex?: number; title?: string; isSubmit?: boolean; }; export const BaseButton = forwardRef( ( { children, icon, iconSide = "left", align = "center", className, size, fullWidth, onClick, tabIndex, isInert, isDisabled, href, target, track, htmlFor, isSubmit, ...rest }: Props, ref ) => { const classes = classNames("vuiBaseButton", className, `vuiBaseButton--${size}`, alignToClassMap[align], { "vuiBaseButton-isInert": isInert, "vuiBaseButton-isDisabled": isDisabled, "vuiBaseButton--fullWidth": fullWidth, [`vuiBaseButton--${iconSide}`]: Boolean(icon) && Boolean(children) }); const iconContainer = icon ? {icon} : null; // This is useful for controlling other elements, e.g. creating an // for uploading files and adding a button to trigger it. if (htmlFor) { return ( ); } if (href) { const wrapperClasses = classNames("vuiBaseButtonLinkWrapper", { "vuiBaseButtonLinkWrapper--fullWidth": fullWidth }); return ( {/* Wrap a button otherwise the flex layout breaks */} ); } const props = { onClick, tabIndex, ["type"]: isSubmit ? "submit" : undefined, ...rest }; return ( // @ts-expect-error HTMLButtonElement conflict with HTMLAnchorElement ); } );