62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import { forwardRef, type ButtonHTMLAttributes } from 'react'
|
|
|
|
import {
|
|
TactileButton,
|
|
type TactileButtonProps,
|
|
type TactileButtonSize,
|
|
type TactileButtonVariant,
|
|
type TactileControlOptions,
|
|
type TactileIconValue,
|
|
} from '../../../components/tactile-ui'
|
|
|
|
type AdminButtonVariant = 'default' | 'primary' | 'ghost' | 'subtle' | 'danger' | 'link'
|
|
type AdminButtonSize = 'sm' | 'md' | 'icon'
|
|
|
|
function mapVariant(variant?: AdminButtonVariant): TactileButtonVariant {
|
|
if (variant === 'primary') return 'primary'
|
|
if (variant === 'danger') return 'danger'
|
|
if (variant === 'ghost' || variant === 'link') return 'ghost'
|
|
if (variant === 'subtle') return 'subtle'
|
|
return 'neutral'
|
|
}
|
|
|
|
function mapSize(size?: AdminButtonSize): TactileButtonSize {
|
|
if (size === 'sm') return 'sm'
|
|
if (size === 'icon') return 'icon'
|
|
return 'md'
|
|
}
|
|
|
|
export interface ButtonProps
|
|
extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'size'> {
|
|
asChild?: boolean
|
|
loading?: boolean
|
|
icon?: TactileIconValue
|
|
iconSize?: number
|
|
size?: AdminButtonSize
|
|
tactile?: boolean | TactileControlOptions
|
|
variant?: AdminButtonVariant
|
|
}
|
|
|
|
export const Button = forwardRef<HTMLElement, ButtonProps>(function Button(
|
|
{ asChild = false, loading = false, size, title, variant, ...props },
|
|
ref,
|
|
) {
|
|
const tooltip = typeof title === 'string' ? title : undefined
|
|
const searchText = [tooltip, typeof props['aria-label'] === 'string' ? props['aria-label'] : undefined]
|
|
.filter(Boolean)
|
|
.join(' ')
|
|
const buttonProps = {
|
|
...props,
|
|
asChild,
|
|
disabled: props.disabled || loading,
|
|
loading,
|
|
size: mapSize(size),
|
|
title: tooltip,
|
|
tooltip,
|
|
variant: mapVariant(variant),
|
|
'data-admin-search-text': searchText || undefined,
|
|
} satisfies TactileButtonProps & { 'data-admin-search-text'?: string }
|
|
if (variant === 'link') buttonProps.tactile = false
|
|
return <TactileButton ref={ref} {...buttonProps} />
|
|
})
|