button.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Button as TaroButton, ButtonProps as TaroButtonProps } from '@tarojs/components'
  2. import { cn } from '@/utils/cn'
  3. import { cva, type VariantProps } from 'class-variance-authority'
  4. const buttonVariants = cva(
  5. 'inline-flex items-center justify-center rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:opacity-50 disabled:pointer-events-none ring-offset-background',
  6. {
  7. variants: {
  8. variant: {
  9. default: 'bg-primary text-primary-foreground hover:bg-primary/90',
  10. destructive: 'bg-destructive text-destructive-foreground hover:bg-destructive/90',
  11. outline: 'border border-input hover:bg-accent hover:text-accent-foreground',
  12. secondary: 'bg-secondary text-secondary-foreground hover:bg-secondary/80',
  13. ghost: 'hover:bg-accent hover:text-accent-foreground',
  14. link: 'underline-offset-4 hover:underline text-primary',
  15. },
  16. size: {
  17. default: 'h-10 py-2 px-4',
  18. sm: 'h-9 px-3 rounded-md text-xs',
  19. lg: 'h-11 px-8 rounded-md',
  20. icon: 'h-10 w-10',
  21. },
  22. },
  23. defaultVariants: {
  24. variant: 'default',
  25. size: 'default',
  26. },
  27. }
  28. )
  29. interface ButtonProps extends Omit<TaroButtonProps, 'size'>, VariantProps<typeof buttonVariants> {
  30. className?: string
  31. children?: React.ReactNode
  32. }
  33. export function Button({ className, variant, size, ...props }: ButtonProps) {
  34. return (
  35. <TaroButton
  36. className={cn(buttonVariants({ variant, size, className }))}
  37. {...props}
  38. />
  39. )
  40. }
  41. // 预定义的按钮样式导出
  42. export { buttonVariants }