86 lines
4.5 KiB
TypeScript
86 lines
4.5 KiB
TypeScript
import { type AnchorHTMLAttributes, type ButtonHTMLAttributes, type ReactNode, type Ref, forwardRef } from 'react'
|
||
import { cn } from '../../lib/cn'
|
||
|
||
/**
|
||
* 全站唯一的按钮。
|
||
*
|
||
* 为什么只有这一个组件:旧站同一颗「取消」在四个页面分别是 .secondary / .outline /
|
||
* .ghost / 裸 button,padding 各自写死,同一行里量出 31/34/36/38 四种高度。
|
||
* 现在高度只有两档,且**由所在容器决定**:容器用 [--ctl-h:var(--ctl-h-md)]
|
||
* 声明自己是紧凑档(工具条、筛选条、卡片动作行),按钮写 h-(--ctl-h) 自动跟随。
|
||
* 页面要更矮不要加 padding,把外层换成 <Ctlbar> / <ChipBar> 就行。
|
||
*
|
||
* 变体语义(不要凭手感挑):
|
||
* primary 一个区域里唯一的主动作(保存 / 开始生成)
|
||
* secondary 有底的次动作
|
||
* outline 中性动作、与主按钮并排的「取消」
|
||
* ghost 紧凑行里的轻动作(抽屉、灯箱、卡片角)
|
||
* add / del 成组列表里的「加一行 / 删一行」,不是页面级主操作
|
||
* link 文字链样式的动作(表格行内),仍然是控件、与同行等高
|
||
*/
|
||
export type ButtonVariant = 'primary' | 'secondary' | 'outline' | 'ghost' | 'add' | 'del' | 'link'
|
||
export type ButtonSize = 'sm' | 'md' | 'lg'
|
||
|
||
const BASE = 'inline-flex items-center justify-center gap-1.5 rounded-control border border-transparent '
|
||
+ 'font-semibold whitespace-nowrap shrink-0 select-none h-(--ctl-h) px-[13px] text-[11px] leading-[1.2] '
|
||
+ 'transition-[background,border-color,color,filter] duration-150 '
|
||
+ 'disabled:opacity-35 disabled:cursor-not-allowed'
|
||
|
||
const VARIANTS: Record<ButtonVariant, string> = {
|
||
primary: 'bg-accent-fill text-on-accent hover:not-disabled:brightness-[1.09] active:not-disabled:translate-y-px',
|
||
secondary: 'bg-line text-ink border-line-strong hover:not-disabled:bg-raise',
|
||
outline: 'bg-transparent text-ink-2 border-line-strong hover:not-disabled:bg-hover',
|
||
ghost: 'bg-transparent text-ink-2 border-line-strong rounded-[7px] px-[9px] text-tiny h-(--ctl-h-md) '
|
||
+ 'hover:not-disabled:border-line-bright hover:not-disabled:bg-hover',
|
||
add: 'bg-accent-fill text-on-accent rounded-[8px] px-[10px] text-[11px] h-(--ctl-h-md) min-w-(--ctl-h-md) shadow-raise '
|
||
+ 'hover:not-disabled:brightness-[1.09] active:not-disabled:translate-y-px',
|
||
del: 'bg-danger-tint-2 text-danger-bright border-danger-line rounded-[8px] px-[10px] text-[11px] h-(--ctl-h-md) min-w-(--ctl-h-md) '
|
||
+ 'hover:not-disabled:bg-danger hover:not-disabled:border-danger hover:not-disabled:text-danger-ink',
|
||
link: 'border-transparent bg-transparent text-success text-body px-0 h-auto min-h-(--ctl-h-md) '
|
||
+ 'hover:not-disabled:underline disabled:opacity-40'
|
||
}
|
||
|
||
const SIZES: Record<ButtonSize, string> = {
|
||
sm: 'h-(--ctl-h-md) px-[9px] text-tiny gap-[5px]',
|
||
md: '',
|
||
lg: 'h-(--ctl-h-main) text-body'
|
||
}
|
||
|
||
type CommonProps = {
|
||
variant?: ButtonVariant
|
||
size?: ButtonSize
|
||
/** 只有一个符号(× / +)时保持正方形,热区不小于控件高度 */
|
||
square?: boolean
|
||
/** 危险语义的描边/幽灵按钮 */
|
||
danger?: boolean
|
||
/** 选中态(筛选、分段开关) */
|
||
on?: boolean
|
||
children?: ReactNode
|
||
className?: string
|
||
}
|
||
|
||
type ButtonProps = CommonProps & ButtonHTMLAttributes<HTMLButtonElement> & { href?: undefined }
|
||
type AnchorProps = CommonProps & AnchorHTMLAttributes<HTMLAnchorElement> & { href: string }
|
||
|
||
export const Button = forwardRef<HTMLButtonElement | HTMLAnchorElement, ButtonProps | AnchorProps>(
|
||
function Button({ variant = 'secondary', size = 'md', square, danger, on, className, children, ...rest }: any, ref) {
|
||
const classes = cn(
|
||
BASE,
|
||
VARIANTS[variant as ButtonVariant],
|
||
size !== 'md' && SIZES[size as ButtonSize],
|
||
square && 'w-(--ctl-h) px-0 text-ui',
|
||
danger && variant === 'outline' && 'text-danger-bright border-danger-line hover:not-disabled:bg-danger-tint-2',
|
||
danger && variant === 'ghost' && 'text-danger-bright border-danger-line',
|
||
danger && variant === 'del' && 'bg-danger border-danger text-danger-ink shadow-raise',
|
||
on && 'border-accent-line bg-accent-tint text-accent-bright',
|
||
className
|
||
)
|
||
/* ref 是「按钮或链接」的联合类型,到这里没法收窄,交给 DOM 元素自己判断 */
|
||
const nodeRef = ref as Ref<any> | undefined
|
||
if (rest.href !== undefined) {
|
||
return <a ref={nodeRef} className={classes} {...(rest as AnchorProps)}>{children}</a>
|
||
}
|
||
return <button ref={nodeRef} type="button" className={classes} {...(rest as ButtonProps)}>{children}</button>
|
||
}
|
||
)
|