index.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 cn from 'classnames'
  9. import {
  10. MagnifyingGlassIcon,
  11. } from '@heroicons/react/24/outline'
  12. import { useTranslation } from 'react-i18next'
  13. import s from './style.module.css'
  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. }
  59. const EmojiPicker: FC<IEmojiPickerProps> = ({
  60. isModal = true,
  61. onSelect,
  62. onClose,
  63. }) => {
  64. const { t } = useTranslation()
  65. const { categories } = data as EmojiMartData
  66. const [selectedEmoji, setSelectedEmoji] = useState('')
  67. const [selectedBackground, setSelectedBackground] = useState(backgroundColors[0])
  68. const [searchedEmojis, setSearchedEmojis] = useState<string[]>([])
  69. const [isSearching, setIsSearching] = useState(false)
  70. return isModal ? <Modal
  71. onClose={() => { }}
  72. isShow
  73. closable={false}
  74. wrapperClassName='!z-40'
  75. className={cn(s.container, '!w-[362px] !p-0')}
  76. >
  77. <div className='flex flex-col items-center w-full p-3'>
  78. <div className="relative w-full">
  79. <div className="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
  80. <MagnifyingGlassIcon className="w-5 h-5 text-gray-400" aria-hidden="true" />
  81. </div>
  82. <input
  83. type="search"
  84. id="search"
  85. className='block w-full h-10 px-3 pl-10 text-sm font-normal bg-gray-100 rounded-lg'
  86. placeholder="Search emojis..."
  87. onChange={async (e: ChangeEvent<HTMLInputElement>) => {
  88. if (e.target.value === '') {
  89. setIsSearching(false)
  90. }
  91. else {
  92. setIsSearching(true)
  93. const emojis = await search(e.target.value)
  94. setSearchedEmojis(emojis)
  95. }
  96. }}
  97. />
  98. </div>
  99. </div>
  100. <Divider className='m-0 mb-3' />
  101. <div className="w-full max-h-[200px] overflow-x-hidden overflow-y-auto px-3">
  102. {isSearching && <>
  103. <div key={'category-search'} className='flex flex-col'>
  104. <p className='font-medium uppercase text-xs text-[#101828] mb-1'>Search</p>
  105. <div className='w-full h-full grid grid-cols-8 gap-1'>
  106. {searchedEmojis.map((emoji: string, index: number) => {
  107. return <div
  108. key={`emoji-search-${index}`}
  109. className='inline-flex w-10 h-10 rounded-lg items-center justify-center'
  110. onClick={() => {
  111. setSelectedEmoji(emoji)
  112. }}
  113. >
  114. <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'>
  115. <em-emoji id={emoji} />
  116. </div>
  117. </div>
  118. })}
  119. </div>
  120. </div>
  121. </>}
  122. {categories.map((category, index: number) => {
  123. return <div key={`category-${index}`} className='flex flex-col'>
  124. <p className='font-medium uppercase text-xs text-[#101828] mb-1'>{category.id}</p>
  125. <div className='w-full h-full grid grid-cols-8 gap-1'>
  126. {category.emojis.map((emoji, index: number) => {
  127. return <div
  128. key={`emoji-${index}`}
  129. className='inline-flex w-10 h-10 rounded-lg items-center justify-center'
  130. onClick={() => {
  131. setSelectedEmoji(emoji)
  132. }}
  133. >
  134. <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'>
  135. <em-emoji id={emoji} />
  136. </div>
  137. </div>
  138. })}
  139. </div>
  140. </div>
  141. })}
  142. </div>
  143. {/* Color Select */}
  144. <div className={cn('p-3 ', selectedEmoji === '' ? 'opacity-25' : '')}>
  145. <p className='font-medium uppercase text-xs text-[#101828] mb-2'>Choose Style</p>
  146. <div className='w-full h-full grid grid-cols-8 gap-1'>
  147. {backgroundColors.map((color) => {
  148. return <div
  149. key={color}
  150. className={
  151. cn(
  152. 'cursor-pointer',
  153. 'hover:ring-1 ring-offset-1',
  154. 'inline-flex w-10 h-10 rounded-lg items-center justify-center',
  155. color === selectedBackground ? 'ring-1 ring-gray-300' : '',
  156. )}
  157. onClick={() => {
  158. setSelectedBackground(color)
  159. }}
  160. >
  161. <div className={cn(
  162. 'w-8 h-8 p-1 flex items-center justify-center rounded-lg',
  163. )
  164. } style={{ background: color }}>
  165. {selectedEmoji !== '' && <em-emoji id={selectedEmoji} />}
  166. </div>
  167. </div>
  168. })}
  169. </div>
  170. </div>
  171. <Divider className='m-0' />
  172. <div className='w-full flex items-center justify-center p-3 gap-2'>
  173. <Button type="default" className='w-full' onClick={() => {
  174. onClose && onClose()
  175. }}>
  176. {t('app.emoji.cancel')}
  177. </Button>
  178. <Button
  179. disabled={selectedEmoji === ''}
  180. type="primary"
  181. className='w-full'
  182. onClick={() => {
  183. onSelect && onSelect(selectedEmoji, selectedBackground)
  184. }}>
  185. {t('app.emoji.ok')}
  186. </Button>
  187. </div>
  188. </Modal> : <>
  189. </>
  190. }
  191. export default EmojiPicker