2025-02-20 18:10:34 +03:00
|
|
|
import { globalIDs } from '@/utils/constants';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
2025-02-22 14:03:13 +03:00
|
|
|
import { type Button as ButtonStyle, type Control } from '../props';
|
2025-04-12 21:47:46 +03:00
|
|
|
import { cn } from '../utils';
|
2025-02-10 01:32:16 +03:00
|
|
|
|
2025-02-22 14:03:13 +03:00
|
|
|
interface ButtonProps extends Control, ButtonStyle {
|
2024-09-12 16:41:48 +03:00
|
|
|
/** Icon to display first. */
|
2024-06-07 20:17:03 +03:00
|
|
|
icon?: React.ReactNode;
|
|
|
|
|
2024-09-12 16:41:48 +03:00
|
|
|
/** Text to display second. */
|
|
|
|
text?: string;
|
|
|
|
|
|
|
|
/** Indicates whether to render the button in a dense style. */
|
2024-06-07 20:17:03 +03:00
|
|
|
dense?: boolean;
|
2024-09-12 16:41:48 +03:00
|
|
|
|
|
|
|
/** Indicates loading state to prevent interactions and change visual style. */
|
2024-06-07 20:17:03 +03:00
|
|
|
loading?: boolean;
|
|
|
|
}
|
|
|
|
|
2024-09-12 16:41:48 +03:00
|
|
|
/**
|
2024-10-30 21:35:43 +03:00
|
|
|
* Customizable `button` with text, icon, tooltips and various styles.
|
2024-09-12 16:41:48 +03:00
|
|
|
*/
|
2025-02-07 10:53:49 +03:00
|
|
|
export function Button({
|
2024-06-07 20:17:03 +03:00
|
|
|
icon,
|
2024-09-12 16:41:48 +03:00
|
|
|
text,
|
2024-06-07 20:17:03 +03:00
|
|
|
title,
|
|
|
|
titleHtml,
|
|
|
|
hideTitle,
|
|
|
|
loading,
|
|
|
|
dense,
|
|
|
|
disabled,
|
|
|
|
noBorder,
|
|
|
|
noOutline,
|
|
|
|
className,
|
|
|
|
...restProps
|
|
|
|
}: ButtonProps) {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
type='button'
|
2025-04-12 21:47:46 +03:00
|
|
|
className={cn(
|
2024-06-07 20:17:03 +03:00
|
|
|
'inline-flex gap-2 items-center justify-center',
|
2025-04-12 21:47:46 +03:00
|
|
|
'font-medium select-none disabled:cursor-auto disabled:opacity-75',
|
|
|
|
'bg-secondary text-secondary-foreground cc-hover cc-animate-color',
|
2025-03-13 01:14:47 +03:00
|
|
|
dense ? 'px-1' : 'px-3 py-1',
|
|
|
|
loading ? 'cursor-progress' : 'cursor-pointer',
|
2025-03-20 18:24:07 +03:00
|
|
|
noOutline ? 'outline-hidden' : 'focus-outline',
|
2025-03-13 01:14:47 +03:00
|
|
|
!noBorder && 'border rounded-sm',
|
2025-02-22 14:03:13 +03:00
|
|
|
className
|
2024-06-07 20:17:03 +03:00
|
|
|
)}
|
2025-02-20 18:10:34 +03:00
|
|
|
data-tooltip-id={!!title || !!titleHtml ? globalIDs.tooltip : undefined}
|
2024-06-07 20:17:03 +03:00
|
|
|
data-tooltip-html={titleHtml}
|
|
|
|
data-tooltip-content={title}
|
|
|
|
data-tooltip-hidden={hideTitle}
|
2025-03-20 11:33:19 +03:00
|
|
|
disabled={disabled ?? loading}
|
|
|
|
aria-label={!text ? title : undefined}
|
2024-06-07 20:17:03 +03:00
|
|
|
{...restProps}
|
|
|
|
>
|
|
|
|
{icon ? icon : null}
|
2025-03-09 21:57:21 +03:00
|
|
|
{text ? <span>{text}</span> : null}
|
2024-06-07 20:17:03 +03:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|