index.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use client'
  2. import type { FC } from 'react'
  3. import { init } from 'emoji-mart'
  4. import data from '@emoji-mart/data'
  5. import Image from 'next/image'
  6. import { cva } from 'class-variance-authority'
  7. import type { AppIconType } from '@/types/app'
  8. import classNames from '@/utils/classnames'
  9. init({ data })
  10. export type AppIconProps = {
  11. size?: 'xs' | 'tiny' | 'small' | 'medium' | 'large' | 'xl' | 'xxl'
  12. rounded?: boolean
  13. iconType?: AppIconType | null
  14. icon?: string
  15. background?: string | null
  16. imageUrl?: string | null
  17. className?: string
  18. innerIcon?: React.ReactNode
  19. onClick?: () => void
  20. }
  21. const appIconVariants = cva(
  22. 'flex items-center justify-center relative text-lg rounded-lg grow-0 shrink-0 overflow-hidden leading-none',
  23. {
  24. variants: {
  25. size: {
  26. xs: 'w-4 h-4 text-xs',
  27. tiny: 'w-6 h-6 text-base',
  28. small: 'w-8 h-8 text-xl',
  29. medium: 'w-9 h-9 text-[22px]',
  30. large: 'w-10 h-10 text-[24px]',
  31. xl: 'w-12 h-12 text-[28px]',
  32. xxl: 'w-14 h-14 text-[32px]',
  33. },
  34. rounded: {
  35. true: 'rounded-full',
  36. },
  37. },
  38. defaultVariants: {
  39. size: 'medium',
  40. rounded: false,
  41. },
  42. })
  43. const AppIcon: FC<AppIconProps> = ({
  44. size = 'medium',
  45. rounded = false,
  46. iconType,
  47. icon,
  48. background,
  49. imageUrl,
  50. className,
  51. innerIcon,
  52. onClick,
  53. }) => {
  54. const isValidImageIcon = iconType === 'image' && imageUrl
  55. return <span
  56. className={classNames(appIconVariants({ size, rounded }), className)}
  57. style={{ background: isValidImageIcon ? undefined : (background || '#FFEAD5') }}
  58. onClick={onClick}
  59. >
  60. {isValidImageIcon
  61. ? <Image src={imageUrl} className="w-full h-full" alt="app icon" />
  62. : (innerIcon || ((icon && icon !== '') ? <em-emoji id={icon} /> : <em-emoji id='🤖' />))
  63. }
  64. </span>
  65. }
  66. export default AppIcon