index.tsx 6.1 KB

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