import { BsmIcon, BsmIconType } from "../svgs/bsm-icon.component"; import { useRef, CSSProperties, MouseEvent, forwardRef, useCallback, ComponentProps } from "react"; import { BsmImage } from "./bsm-image.component"; import { useTranslation } from "renderer/hooks/use-translation.hook"; import { useClickOutside } from "renderer/hooks/use-click-outside.hook"; import { useThemeColor } from "renderer/hooks/use-theme-color.hook"; import { getCorrectTextColor } from "renderer/helpers/correct-text-color"; export type BsmButtonType = "primary" | "secondary" | "success" | "cancel" | "error" | "none"; type Props = { className?: string; style?: CSSProperties; iconStyle?: CSSProperties; imgClassName?: string; iconClassName?: string; icon?: BsmIconType; image?: string; text?: string; type?: string; active?: boolean; withBar?: boolean; disabled?: boolean; onClickOutside?: ComponentProps<"div">["onClick"]; onClick?: ComponentProps<"div">["onClick"]; typeColor?: BsmButtonType; color?: string; title?: string; iconColor?: string; textClassName?: string; }; export const BsmButton = forwardRef(({ className, style, iconStyle, imgClassName, iconClassName, icon, image, text, type, active, withBar = true, disabled, onClickOutside, onClick, typeColor, color, title, iconColor, textClassName }, forwardedRef) => { const t = useTranslation(); const { firstColor, secondColor } = useThemeColor(); const ref = useRef(null); const setRef = useCallback((node: HTMLDivElement) => { if (ref.current) { ref.current = node; } if (typeof forwardedRef === 'function') { forwardedRef(node); } else if (forwardedRef) { forwardedRef.current = node; } }, [forwardedRef]); useClickOutside(ref, onClickOutside); const primaryColor = (() => { if (typeColor === "primary") { return firstColor; } if (typeColor === "secondary") { return secondColor; } return undefined; })(); const textColor = (() => { if (primaryColor) { return getCorrectTextColor(primaryColor); } return typeColor && typeColor !== "none" ? "white" : undefined; })(); const renderTypeColor = (() => { if (!typeColor) { return `bg-light-main-color-2 dark:bg-main-color-2 ${!withBar && !disabled && "hover:brightness-125"}`; } if (typeColor === "cancel") { return "bg-gray-500"; } if (typeColor === "error") { return "bg-red-500"; } if (typeColor === "success") { return "bg-green-500"; } return ""; })(); const handleClick = (e: MouseEvent) => !disabled && onClick?.(e); return (
{image && } {icon && } {text && (type === "submit" ? ( ) : ( {t(text)} ))} {withBar && (
)}
); });