index.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type { CSSProperties, ReactNode } from 'react'
  2. import React from 'react'
  3. import { type VariantProps, cva } from 'class-variance-authority'
  4. import classNames from '@/utils/classnames'
  5. import './index.css'
  6. enum BadgeState {
  7. Warning = 'warning',
  8. Accent = 'accent',
  9. Default = '',
  10. }
  11. const BadgeVariants = cva(
  12. 'badge',
  13. {
  14. variants: {
  15. size: {
  16. s: 'badge-s',
  17. m: 'badge-m',
  18. l: 'badge-l',
  19. },
  20. },
  21. defaultVariants: {
  22. size: 'm',
  23. },
  24. },
  25. )
  26. type BadgeProps = {
  27. size?: 's' | 'm' | 'l'
  28. iconOnly?: boolean
  29. uppercase?: boolean
  30. state?: BadgeState
  31. styleCss?: CSSProperties
  32. children?: ReactNode
  33. } & React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof BadgeVariants>
  34. function getBadgeState(state: BadgeState) {
  35. switch (state) {
  36. case BadgeState.Warning:
  37. return 'badge-warning'
  38. case BadgeState.Accent:
  39. return 'badge-accent'
  40. default:
  41. return ''
  42. }
  43. }
  44. const Badge: React.FC<BadgeProps> = ({
  45. className,
  46. size,
  47. state = BadgeState.Default,
  48. iconOnly = false,
  49. uppercase = false,
  50. styleCss,
  51. children,
  52. ...props
  53. }) => {
  54. return (
  55. <div
  56. className={classNames(
  57. BadgeVariants({ size, className }),
  58. getBadgeState(state),
  59. size === 's'
  60. ? (iconOnly ? 'p-[3px]' : 'px-[5px] py-[3px]')
  61. : size === 'l'
  62. ? (iconOnly ? 'p-1.5' : 'px-2 py-1')
  63. : (iconOnly ? 'p-1' : 'px-[5px] py-[2px]'),
  64. uppercase ? 'system-2xs-medium-uppercase' : 'system-2xs-medium',
  65. )}
  66. style={styleCss}
  67. {...props}
  68. >
  69. {children}
  70. </div>
  71. )
  72. }
  73. Badge.displayName = 'Badge'
  74. export default Badge
  75. export { Badge, BadgeState, BadgeVariants }