index.tsx 6.3 KB

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