ldc-ovoteam/resources/ts/front/Components/Theme/Button.tsx

149 lines
3.7 KiB
TypeScript
Raw Permalink Normal View History

2026-04-08 13:59:30 +02:00
import { Button as ShadcnButton, ButtonProps } from "@/components/ui/button";
import {
FontName,
fonts,
iconMap,
sidePlacements,
} from "@/front/lib/constants";
2026-03-18 17:14:23 +01:00
import { cn } from "@/lib/utils";
2026-03-30 13:52:00 +02:00
import { CustomButtonVariant, IconConfig } from "@/types/front";
import { Link } from "@inertiajs/react";
2026-03-18 17:14:23 +01:00
2026-04-15 09:31:43 +02:00
type CustomSize = "xs" | "md";
2026-04-08 13:59:30 +02:00
2026-03-18 17:14:23 +01:00
type ThemeButtonProps = Omit<ButtonProps, "variant" | "size"> & {
variant?: ButtonProps["variant"] | CustomButtonVariant;
2026-04-08 13:59:30 +02:00
size?: ButtonProps["size"] | CustomSize;
icon?: IconConfig;
isBold?: boolean;
2026-03-30 13:52:00 +02:00
font?: FontName;
link?: {
2026-04-08 13:59:30 +02:00
href: string;
label: string;
};
};
2026-03-18 17:14:23 +01:00
const customVariantSizes: Record<CustomSize, string> = {
xs: "h-7 px-3 text-sm",
2026-04-15 09:31:43 +02:00
md: "h-10 px-6 py-2 text-md",
2026-04-08 13:59:30 +02:00
};
2026-03-18 17:14:23 +01:00
const customVariantStyles: Record<CustomButtonVariant, string> = {
2026-04-08 13:59:30 +02:00
tertiary:
"bg-primary-foreground text-background hover:bg-primary-foreground/90",
inverted: "bg-background hover:bg-secondary/90",
};
2026-03-18 17:14:23 +01:00
2026-04-08 13:59:30 +02:00
export default function Button({
2026-03-18 17:14:23 +01:00
icon,
isBold = true,
font = fonts.GOTHAM,
size,
variant,
children,
className,
asChild,
2026-03-30 13:52:00 +02:00
link,
2026-03-18 17:14:23 +01:00
...props
2026-04-08 13:59:30 +02:00
}: ThemeButtonProps) {
const Icon = icon ? iconMap[icon.icon] : undefined;
2026-03-18 17:14:23 +01:00
const customVariant =
variant && variant in customVariantStyles
? customVariantStyles[variant as CustomButtonVariant]
2026-04-08 13:59:30 +02:00
: "";
2026-03-18 17:14:23 +01:00
2026-04-08 13:59:30 +02:00
const getSizeClasses = () => {
2026-04-15 09:31:43 +02:00
if (size && size in customVariantSizes) {
2026-04-08 13:59:30 +02:00
return customVariantSizes[size as CustomSize];
2026-04-15 09:31:43 +02:00
}
return "h-9 px-5 text-md";
2026-04-08 13:59:30 +02:00
};
2026-03-18 17:14:23 +01:00
const iconClassName = icon?.className ?? "";
2026-03-30 13:52:00 +02:00
if (link && link.href && link.label) {
return (
<ShadcnButton
variant={variant as ButtonProps["variant"]}
className={cn(
2026-04-08 13:59:30 +02:00
"rounded-full w-fit",
2026-03-30 13:52:00 +02:00
isBold ? "font-bold" : "font-normal",
font,
className,
2026-04-08 13:59:30 +02:00
getSizeClasses(),
customVariant,
2026-03-30 13:52:00 +02:00
)}
asChild
2026-04-08 13:59:30 +02:00
/* eslint-disable react/jsx-props-no-spreading */
2026-03-30 13:52:00 +02:00
{...props}
>
2026-04-08 13:59:30 +02:00
<Link
href={link.href}
className="flex items-center justify-center gap-2"
>
{icon?.placement !== sidePlacements.RIGHT && Icon && (
<Icon className={cn(iconClassName)} />
)}
2026-03-30 13:52:00 +02:00
{link.label}
2026-04-08 13:59:30 +02:00
{icon?.placement === sidePlacements.RIGHT && Icon && (
<Icon className={cn(iconClassName)} />
)}
2026-03-30 13:52:00 +02:00
</Link>
</ShadcnButton>
2026-04-08 13:59:30 +02:00
);
2026-03-30 13:52:00 +02:00
}
2026-03-18 17:14:23 +01:00
if (asChild) {
return (
<ShadcnButton
variant={variant as ButtonProps["variant"]}
className={cn(
2026-04-08 13:59:30 +02:00
"rounded-full w-fit",
2026-03-18 17:14:23 +01:00
isBold ? "font-bold" : "font-normal",
font,
className,
2026-04-08 13:59:30 +02:00
getSizeClasses(),
customVariant,
2026-03-18 17:14:23 +01:00
)}
asChild
2026-04-08 13:59:30 +02:00
/* eslint-disable react/jsx-props-no-spreading */
2026-03-18 17:14:23 +01:00
{...props}
>
2026-03-30 13:52:00 +02:00
<div>
2026-04-08 13:59:30 +02:00
{icon?.placement !== sidePlacements.RIGHT && Icon && (
<Icon className={cn(iconClassName)} />
)}
2026-03-30 13:52:00 +02:00
{children}
2026-04-08 13:59:30 +02:00
{icon?.placement === sidePlacements.RIGHT && Icon && (
<Icon className={cn(iconClassName)} />
)}
2026-03-30 13:52:00 +02:00
</div>
2026-03-18 17:14:23 +01:00
</ShadcnButton>
);
}
return (
<ShadcnButton
variant={variant as ButtonProps["variant"]}
2026-04-08 13:59:30 +02:00
className={cn(
"rounded-full w-fit",
2026-03-18 17:14:23 +01:00
isBold ? "font-bold" : "font-normal",
font,
className,
2026-04-08 13:59:30 +02:00
getSizeClasses(),
2026-03-18 17:14:23 +01:00
customVariant,
)}
2026-04-08 13:59:30 +02:00
/* eslint-disable react/jsx-props-no-spreading */
2026-03-18 17:14:23 +01:00
{...props}
>
2026-04-08 13:59:30 +02:00
{icon?.placement !== sidePlacements.RIGHT && Icon && (
<Icon className={cn(iconClassName)} />
)}
2026-03-18 17:14:23 +01:00
{children}
2026-04-08 13:59:30 +02:00
{icon?.placement === sidePlacements.RIGHT && Icon && (
<Icon className={cn(iconClassName)} />
)}
2026-03-18 17:14:23 +01:00
</ShadcnButton>
2026-04-08 13:59:30 +02:00
);
2026-03-18 17:14:23 +01:00
}