index.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. 'use client'
  2. import data from '@emoji-mart/data'
  3. import { init, SearchIndex } from 'emoji-mart'
  4. // import AppIcon from '@/app/components/base/app-icon'
  5. import cn from 'classnames'
  6. import Divider from '@/app/components/base/divider'
  7. import Button from '@/app/components/base/button'
  8. import s from './style.module.css'
  9. import { useState, FC, ChangeEvent } from 'react'
  10. import {
  11. MagnifyingGlassIcon
  12. } from '@heroicons/react/24/outline'
  13. import React from 'react'
  14. import Modal from '@/app/components/base/modal'
  15. declare global {
  16. namespace JSX {
  17. interface IntrinsicElements {
  18. 'em-emoji': React.DetailedHTMLProps<
  19. React.HTMLAttributes<HTMLElement>,
  20. HTMLElement
  21. >;
  22. }
  23. }
  24. }
  25. init({ data })
  26. async function search(value: string) {
  27. const emojis = await SearchIndex.search(value) || []
  28. const results = emojis.map((emoji: any) => {
  29. return emoji.skins[0].native
  30. })
  31. return results
  32. }
  33. const backgroundColors = [
  34. '#FFEAD5',
  35. '#E4FBCC',
  36. '#D3F8DF',
  37. '#E0F2FE',
  38. '#E0EAFF',
  39. '#EFF1F5',
  40. '#FBE8FF',
  41. '#FCE7F6',
  42. '#FEF7C3',
  43. '#E6F4D7',
  44. '#D5F5F6',
  45. '#D1E9FF',
  46. '#D1E0FF',
  47. '#D5D9EB',
  48. '#ECE9FE',
  49. '#FFE4E8',
  50. ]
  51. interface IEmojiPickerProps {
  52. isModal?: boolean
  53. onSelect?: (emoji: string, background: string) => void
  54. onClose?: () => void
  55. }
  56. const EmojiPicker: FC<IEmojiPickerProps> = ({
  57. isModal = true,
  58. onSelect,
  59. onClose
  60. }) => {
  61. const { categories } = data as any
  62. const [selectedEmoji, setSelectedEmoji] = useState('')
  63. const [selectedBackground, setSelectedBackground] = useState(backgroundColors[0])
  64. const [searchedEmojis, setSearchedEmojis] = useState([])
  65. const [isSearching, setIsSearching] = useState(false)
  66. return isModal ? <Modal
  67. onClose={() => { }}
  68. isShow
  69. closable={false}
  70. wrapperClassName='!z-40'
  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. return
  87. } else {
  88. setIsSearching(true)
  89. const emojis = await search(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: any, 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: string, 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('flex flex-col 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 type="default" className='w-full' onClick={() => {
  170. onClose && onClose()
  171. }}>
  172. Cancel
  173. </Button>
  174. <Button
  175. disabled={selectedEmoji == ''}
  176. type="primary"
  177. className='w-full'
  178. onClick={() => {
  179. onSelect && onSelect(selectedEmoji, selectedBackground)
  180. }}>
  181. OK
  182. </Button>
  183. </div>
  184. </Modal> : <>
  185. </>
  186. }
  187. export default EmojiPicker