index.tsx 6.2 KB

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