Inner.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. 'use client'
  2. import type { ChangeEvent, FC } from 'react'
  3. import React, { useState } from 'react'
  4. import data from '@emoji-mart/data'
  5. import type { EmojiMartData } from '@emoji-mart/data'
  6. import { init } from 'emoji-mart'
  7. import {
  8. MagnifyingGlassIcon,
  9. } from '@heroicons/react/24/outline'
  10. import cn from '@/utils/classnames'
  11. import Divider from '@/app/components/base/divider'
  12. import { searchEmoji } from '@/utils/emoji'
  13. declare global {
  14. namespace JSX {
  15. // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
  16. interface IntrinsicElements {
  17. 'em-emoji': React.DetailedHTMLProps< React.HTMLAttributes<HTMLElement>, HTMLElement >
  18. }
  19. }
  20. }
  21. init({ data })
  22. const backgroundColors = [
  23. '#FFEAD5',
  24. '#E4FBCC',
  25. '#D3F8DF',
  26. '#E0F2FE',
  27. '#E0EAFF',
  28. '#EFF1F5',
  29. '#FBE8FF',
  30. '#FCE7F6',
  31. '#FEF7C3',
  32. '#E6F4D7',
  33. '#D5F5F6',
  34. '#D1E9FF',
  35. '#D1E0FF',
  36. '#D5D9EB',
  37. '#ECE9FE',
  38. '#FFE4E8',
  39. ]
  40. type IEmojiPickerInnerProps = {
  41. emoji?: string
  42. background?: string
  43. onSelect?: (emoji: string, background: string) => void
  44. className?: string
  45. }
  46. const EmojiPickerInner: FC<IEmojiPickerInnerProps> = ({
  47. onSelect,
  48. className,
  49. }) => {
  50. const { categories } = data as EmojiMartData
  51. const [selectedEmoji, setSelectedEmoji] = useState('')
  52. const [selectedBackground, setSelectedBackground] = useState(backgroundColors[0])
  53. const [searchedEmojis, setSearchedEmojis] = useState<string[]>([])
  54. const [isSearching, setIsSearching] = useState(false)
  55. React.useEffect(() => {
  56. if (selectedEmoji && selectedBackground)
  57. onSelect?.(selectedEmoji, selectedBackground)
  58. }, [onSelect, selectedEmoji, selectedBackground])
  59. return <div className={cn(className)}>
  60. <div className='flex flex-col items-center w-full px-3'>
  61. <div className="relative w-full">
  62. <div className="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
  63. <MagnifyingGlassIcon className="w-5 h-5 text-gray-400" aria-hidden="true" />
  64. </div>
  65. <input
  66. type="search"
  67. id="search"
  68. className='block w-full h-10 px-3 pl-10 text-sm font-normal bg-gray-100 rounded-lg'
  69. placeholder="Search emojis..."
  70. onChange={async (e: ChangeEvent<HTMLInputElement>) => {
  71. if (e.target.value === '') {
  72. setIsSearching(false)
  73. }
  74. else {
  75. setIsSearching(true)
  76. const emojis = await searchEmoji(e.target.value)
  77. setSearchedEmojis(emojis)
  78. }
  79. }}
  80. />
  81. </div>
  82. </div>
  83. <Divider className='m-0 mb-3' />
  84. <div className="w-full max-h-[200px] overflow-x-hidden overflow-y-auto px-3">
  85. {isSearching && <>
  86. <div key={'category-search'} className='flex flex-col'>
  87. <p className='font-medium uppercase text-xs text-[#101828] mb-1'>Search</p>
  88. <div className='w-full h-full grid grid-cols-8 gap-1'>
  89. {searchedEmojis.map((emoji: string, index: number) => {
  90. return <div
  91. key={`emoji-search-${index}`}
  92. className='inline-flex w-10 h-10 rounded-lg items-center justify-center'
  93. onClick={() => {
  94. setSelectedEmoji(emoji)
  95. }}
  96. >
  97. <div className='cursor-pointer w-8 h-8 p-1 flex items-center justify-center rounded-lg hover:ring-1 ring-offset-1 ring-gray-300'>
  98. <em-emoji id={emoji} />
  99. </div>
  100. </div>
  101. })}
  102. </div>
  103. </div>
  104. </>}
  105. {categories.map((category, index: number) => {
  106. return <div key={`category-${index}`} className='flex flex-col'>
  107. <p className='font-medium uppercase text-xs text-[#101828] mb-1'>{category.id}</p>
  108. <div className='w-full h-full grid grid-cols-8 gap-1'>
  109. {category.emojis.map((emoji, index: number) => {
  110. return <div
  111. key={`emoji-${index}`}
  112. className='inline-flex w-10 h-10 rounded-lg items-center justify-center'
  113. onClick={() => {
  114. setSelectedEmoji(emoji)
  115. }}
  116. >
  117. <div className='cursor-pointer w-8 h-8 p-1 flex items-center justify-center rounded-lg hover:ring-1 ring-offset-1 ring-gray-300'>
  118. <em-emoji id={emoji} />
  119. </div>
  120. </div>
  121. })}
  122. </div>
  123. </div>
  124. })}
  125. </div>
  126. {/* Color Select */}
  127. <div className={cn('p-3 pb-0', selectedEmoji === '' ? 'opacity-25' : '')}>
  128. <p className='font-medium uppercase text-xs text-[#101828] mb-2'>Choose Style</p>
  129. <div className='w-full h-full grid grid-cols-8 gap-1'>
  130. {backgroundColors.map((color) => {
  131. return <div
  132. key={color}
  133. className={
  134. cn(
  135. 'cursor-pointer',
  136. 'hover:ring-1 ring-offset-1',
  137. 'inline-flex w-10 h-10 rounded-lg items-center justify-center',
  138. color === selectedBackground ? 'ring-1 ring-gray-300' : '',
  139. )}
  140. onClick={() => {
  141. setSelectedBackground(color)
  142. }}
  143. >
  144. <div className={cn(
  145. 'w-8 h-8 p-1 flex items-center justify-center rounded-lg',
  146. )
  147. } style={{ background: color }}>
  148. {selectedEmoji !== '' && <em-emoji id={selectedEmoji} />}
  149. </div>
  150. </div>
  151. })}
  152. </div>
  153. </div>
  154. </div>
  155. }
  156. export default EmojiPickerInner