Skip to content

Commit

Permalink
Fixing merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncid committed Dec 17, 2024
1 parent ad44607 commit 6775041
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions sparkle/src/components/CardButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { cva } from "class-variance-authority";
import React, { ReactNode } from "react";

import {
noHrefLink,
SparkleContext,
SparkleContextLinkType,
} from "@sparkle/context";
import { cn } from "@sparkle/lib/utils";

export const CARD_BUTTON_VARIANTS = [
"primary",
"secondary",
"tertiary",
] as const;

export type CardButtonVariantType = (typeof CARD_BUTTON_VARIANTS)[number];

const variantClasses: Record<CardButtonVariantType, string> = {
primary: "s-bg-primary-50 s-border-border-dark/0",
secondary: "s-bg-background s-border-border-dark",
tertiary: "s-bg-background s-border-border-dark/0",
};

const CARD_BUTTON_SIZES = ["sm", "md", "lg"] as const;

type CardButtonSizeType = (typeof CARD_BUTTON_SIZES)[number];

const sizeVariants: Record<CardButtonSizeType, string> = {
sm: "s-p-3 s-rounded-xl",
md: "s-p-4 s-rounded-2xl",
lg: "s-p-5 s-rounded-3xl",
};

const cardButtonVariants = cva(
"s-flex s-text-left s-justify-center s-group s-border s-overflow-hidden s-text-foreground",
{
variants: {
variant: variantClasses,
size: sizeVariants,
},
defaultVariants: {
variant: "primary",
size: "md",
},
}
);

interface CommonProps {
variant?: CardButtonVariantType;
size?: CardButtonSizeType;
className?: string;
}

interface LinkProps extends CommonProps {
children?: ReactNode;
href: string;
target?: string;
rel?: string;
replace?: boolean;
shallow?: boolean;
onClick?: never;
onMouseEnter?: never;
onMouseLeave?: never;
}

interface ButtonProps
extends CommonProps,
React.ButtonHTMLAttributes<HTMLDivElement> {
href?: never;
target?: never;
rel?: never;
replace?: never;
shallow?: never;
}

export type CardButtonProps = LinkProps | ButtonProps;

export const CardButton = React.forwardRef<HTMLDivElement, CardButtonProps>(
(
{
children,
variant,
size,
className,
onClick,
onMouseEnter,
onMouseLeave,
href,
target = "_blank",
rel = "",
replace,
shallow,
...props
},
ref
) => {
const { components } = React.useContext(SparkleContext);
const Link: SparkleContextLinkType = href ? components.link : noHrefLink;

const cardButtonClassNames = cn(
cardButtonVariants({ variant, size }),
(onClick || onMouseEnter) &&
"s-cursor-pointer disabled:s-text-primary-muted disabled:s-border-structure-100 disabled:s-pointer-events-none s-transition s-duration-200",
"[&:not(:has(button:hover))]:hover:s-bg-primary-100", // apply hover style when no button children are hovered
"[&:not(:has(button:active))]:active:s-bg-primary-200", // apply hover style when no button children are hovered
className
);

if (href) {
return (
<Link
href={href}
className={cardButtonClassNames}
replace={replace}
shallow={shallow}
target={target}
rel={rel}
>
{children}
</Link>
);
}

return (
<div
ref={ref}
className={cardButtonClassNames}
onClick={onClick}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
{...props}
>
{children}
</div>
);
}
);

CardButton.displayName = "CardButton";

0 comments on commit 6775041

Please sign in to comment.