index.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use client'
  2. import { useRef, useState } from 'react'
  3. import { t } from 'i18next'
  4. import copy from 'copy-to-clipboard'
  5. import s from './style.module.css'
  6. import Tooltip from '@/app/components/base/tooltip'
  7. import { randomString } from '@/utils'
  8. type ICopyBtnProps = {
  9. value: string
  10. className?: string
  11. isPlain?: boolean
  12. }
  13. const CopyBtn = ({
  14. value,
  15. className,
  16. isPlain,
  17. }: ICopyBtnProps) => {
  18. const [isCopied, setIsCopied] = useState(false)
  19. const selector = useRef(`copy-tooltip-${randomString(4)}`)
  20. return (
  21. <div className={`${className}`}>
  22. <Tooltip
  23. selector={selector.current}
  24. content={(isCopied ? t('appApi.copied') : t('appApi.copy')) as string}
  25. className='z-10'
  26. >
  27. <div
  28. className={'box-border p-0.5 flex items-center justify-center rounded-md bg-white cursor-pointer'}
  29. style={!isPlain
  30. ? {
  31. boxShadow: '0px 4px 8px -2px rgba(16, 24, 40, 0.1), 0px 2px 4px -2px rgba(16, 24, 40, 0.06)',
  32. }
  33. : {}}
  34. onClick={() => {
  35. copy(value)
  36. setIsCopied(true)
  37. }}
  38. >
  39. <div className={`w-6 h-6 rounded-md hover:bg-gray-50 ${s.copyIcon} ${isCopied ? s.copied : ''}`}></div>
  40. </div>
  41. </Tooltip>
  42. </div>
  43. )
  44. }
  45. export default CopyBtn