- 完整实现Apple HIG 2025设计系统 - 添加Modal弹窗组件(毛玻璃效果、流畅动画) - 添加FileUpload文件上传组件(拖拽、批量上传、进度显示) - 实现产品上传功能(支持DWG/GLB/GLTF文件) - 优化产品列表展示(可折叠的产品+SKU结构) - 实现Tab切换功能(3D模型管理、五金件、BOM、文档) - 修复主题系统hydration错误 - 优化按钮和间距,符合Apple设计规范 - 添加分享页面的Apple风格重设计 - 创建测试数据脚本
183 lines
5.3 KiB
TypeScript
183 lines
5.3 KiB
TypeScript
/**
|
||
* Apple 风格 Button 组件
|
||
*
|
||
* 基于苹果设计系统的按钮组件
|
||
* 支持四种苹果标准样式:Filled、Tinted、Gray、Plain
|
||
*
|
||
* @created 2025-12-11
|
||
* @updated 2025-12-11 - 添加性能优化和可访问性支持
|
||
* @component
|
||
*/
|
||
|
||
'use client'
|
||
|
||
import React from 'react'
|
||
import { cn } from '@/lib/utils'
|
||
import { createKeyboardHandler, generateAriaProps, APPLE_TOUCH_TARGETS } from '@/lib/accessibility'
|
||
import { withMemo, shallowEqual } from '@/lib/performance'
|
||
|
||
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
||
variant?: 'filled' | 'tinted' | 'gray' | 'plain'
|
||
size?: 'small' | 'regular' | 'large'
|
||
loading?: boolean
|
||
children: React.ReactNode
|
||
/** ARIA 标签 */
|
||
ariaLabel?: string
|
||
/** ARIA 描述 */
|
||
ariaDescribedBy?: string
|
||
/** 是否为主要操作 */
|
||
primary?: boolean
|
||
/** 键盘快捷键 */
|
||
shortcut?: string
|
||
}
|
||
|
||
const ButtonComponent = React.forwardRef<HTMLButtonElement, ButtonProps>(
|
||
({
|
||
variant = 'filled',
|
||
size = 'regular',
|
||
loading = false,
|
||
disabled,
|
||
className,
|
||
children,
|
||
ariaLabel,
|
||
ariaDescribedBy,
|
||
primary = false,
|
||
shortcut,
|
||
onClick,
|
||
onKeyDown,
|
||
...props
|
||
}, ref) => {
|
||
|
||
// 优化的事件处理器
|
||
const handleClick = React.useCallback((event: React.MouseEvent<HTMLButtonElement>) => {
|
||
if (loading || disabled) return
|
||
onClick?.(event)
|
||
}, [loading, disabled, onClick])
|
||
|
||
const handleKeyDown = React.useCallback((event: React.KeyboardEvent<HTMLButtonElement>) => {
|
||
const keyboardHandler = createKeyboardHandler({
|
||
onEnter: () => {
|
||
if (loading || disabled) return
|
||
onClick?.(event as any)
|
||
},
|
||
onSpace: () => {
|
||
if (loading || disabled) return
|
||
onClick?.(event as any)
|
||
},
|
||
})
|
||
|
||
keyboardHandler(event)
|
||
onKeyDown?.(event)
|
||
}, [loading, disabled, onClick, onKeyDown])
|
||
|
||
// ARIA 属性
|
||
const ariaProps = React.useMemo(() => generateAriaProps({
|
||
label: ariaLabel,
|
||
describedBy: ariaDescribedBy,
|
||
disabled: disabled || loading,
|
||
role: 'button',
|
||
}), [ariaLabel, ariaDescribedBy, disabled, loading])
|
||
|
||
// 基础样式
|
||
const baseStyles = [
|
||
'inline-flex items-center justify-center',
|
||
'font-medium transition-all duration-apple-fast ease-apple-ease-out',
|
||
'focus:outline-none focus:ring-2 focus:ring-offset-2',
|
||
'focus-visible:ring-2 focus-visible:ring-offset-2',
|
||
'disabled:opacity-50 disabled:cursor-not-allowed',
|
||
'active:scale-95 active:transition-transform active:duration-apple-fast active:ease-apple-ease-in',
|
||
'will-change-transform',
|
||
'select-none', // 防止文本选择
|
||
]
|
||
|
||
// 尺寸样式(苹果标准触摸目标)
|
||
const sizeStyles = {
|
||
small: ['px-4 py-2 text-sm', 'rounded-apple-sm', `min-h-[${APPLE_TOUCH_TARGETS.minimum}px]`],
|
||
regular: ['px-6 py-3 text-base', 'rounded-apple-md', `min-h-[${APPLE_TOUCH_TARGETS.minimum}px]`],
|
||
large: ['px-8 py-4 text-lg', 'rounded-apple-lg', `min-h-[${APPLE_TOUCH_TARGETS.comfortable}px]`],
|
||
}
|
||
|
||
// 变体样式(使用 CSS 变量自动适配主题)
|
||
const variantStyles = {
|
||
filled: [
|
||
'text-white',
|
||
'bg-apple-blue shadow-apple-md',
|
||
'hover:opacity-90 hover:shadow-apple-lg',
|
||
'focus:ring-apple-blue/50',
|
||
],
|
||
tinted: [
|
||
'text-apple-blue bg-apple-blue/10',
|
||
'hover:bg-apple-blue/20',
|
||
'focus:ring-apple-blue/50',
|
||
],
|
||
gray: [
|
||
'text-apple-label-primary bg-apple-bg-tertiary',
|
||
'border border-apple-separator',
|
||
'hover:bg-apple-bg-secondary',
|
||
'focus:ring-apple-separator',
|
||
],
|
||
plain: [
|
||
'text-apple-blue bg-transparent',
|
||
'hover:bg-apple-blue/10',
|
||
'focus:ring-apple-blue/50',
|
||
],
|
||
}
|
||
|
||
return (
|
||
<button
|
||
ref={ref}
|
||
disabled={disabled || loading}
|
||
className={cn(
|
||
baseStyles,
|
||
sizeStyles[size],
|
||
variantStyles[variant],
|
||
primary && 'ring-2 ring-apple-blue-light/20',
|
||
className
|
||
)}
|
||
onClick={handleClick}
|
||
onKeyDown={handleKeyDown}
|
||
title={shortcut ? `快捷键: ${shortcut}` : undefined}
|
||
{...ariaProps}
|
||
{...props}
|
||
>
|
||
{loading && (
|
||
<svg
|
||
className="animate-spin -ml-1 mr-2 h-4 w-4"
|
||
fill="none"
|
||
viewBox="0 0 24 24"
|
||
aria-hidden="true"
|
||
>
|
||
<circle
|
||
className="opacity-25"
|
||
cx="12"
|
||
cy="12"
|
||
r="10"
|
||
stroke="currentColor"
|
||
strokeWidth="4"
|
||
/>
|
||
<path
|
||
className="opacity-75"
|
||
fill="currentColor"
|
||
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||
/>
|
||
</svg>
|
||
)}
|
||
<span className={loading ? 'opacity-75' : undefined}>
|
||
{children}
|
||
</span>
|
||
{shortcut && (
|
||
<kbd className="ml-2 px-1.5 py-0.5 text-xs bg-black/10 rounded opacity-60">
|
||
{shortcut}
|
||
</kbd>
|
||
)}
|
||
</button>
|
||
)
|
||
}
|
||
)
|
||
|
||
ButtonComponent.displayName = 'Button'
|
||
|
||
// 使用 React.memo 优化性能
|
||
export const Button = React.memo(ButtonComponent)
|
||
|
||
export default Button |