index.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useRef, useState } from 'react'
  4. import { useBoolean } from 'ahooks'
  5. import type { OffsetOptions, Placement } from '@floating-ui/react'
  6. import { RiQuestionLine } from '@remixicon/react'
  7. import cn from '@/utils/classnames'
  8. import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger } from '@/app/components/base/portal-to-follow-elem'
  9. export type TooltipProps = {
  10. position?: Placement
  11. triggerMethod?: 'hover' | 'click'
  12. triggerClassName?: string
  13. disabled?: boolean
  14. popupContent?: React.ReactNode
  15. children?: React.ReactNode
  16. popupClassName?: string
  17. offset?: OffsetOptions
  18. needsDelay?: boolean
  19. asChild?: boolean
  20. }
  21. const Tooltip: FC<TooltipProps> = ({
  22. position = 'top',
  23. triggerMethod = 'hover',
  24. triggerClassName,
  25. disabled = false,
  26. popupContent,
  27. children,
  28. popupClassName,
  29. offset,
  30. asChild = true,
  31. needsDelay = false,
  32. }) => {
  33. const [open, setOpen] = useState(false)
  34. const [isHoverPopup, {
  35. setTrue: setHoverPopup,
  36. setFalse: setNotHoverPopup,
  37. }] = useBoolean(false)
  38. const isHoverPopupRef = useRef(isHoverPopup)
  39. useEffect(() => {
  40. isHoverPopupRef.current = isHoverPopup
  41. }, [isHoverPopup])
  42. const [isHoverTrigger, {
  43. setTrue: setHoverTrigger,
  44. setFalse: setNotHoverTrigger,
  45. }] = useBoolean(false)
  46. const isHoverTriggerRef = useRef(isHoverTrigger)
  47. useEffect(() => {
  48. isHoverTriggerRef.current = isHoverTrigger
  49. }, [isHoverTrigger])
  50. const handleLeave = (isTrigger: boolean) => {
  51. if (isTrigger)
  52. setNotHoverTrigger()
  53. else
  54. setNotHoverPopup()
  55. // give time to move to the popup
  56. if (needsDelay) {
  57. setTimeout(() => {
  58. if (!isHoverPopupRef.current && !isHoverTriggerRef.current)
  59. setOpen(false)
  60. }, 500)
  61. }
  62. else {
  63. setOpen(false)
  64. }
  65. }
  66. return (
  67. <PortalToFollowElem
  68. open={disabled ? false : open}
  69. onOpenChange={setOpen}
  70. placement={position}
  71. offset={offset ?? 8}
  72. >
  73. <PortalToFollowElemTrigger
  74. onClick={() => triggerMethod === 'click' && setOpen(v => !v)}
  75. onMouseEnter={() => {
  76. if (triggerMethod === 'hover') {
  77. setHoverTrigger()
  78. setOpen(true)
  79. }
  80. }}
  81. onMouseLeave={() => triggerMethod === 'hover' && handleLeave(true)}
  82. asChild={asChild}
  83. >
  84. {children || <div className={triggerClassName || 'p-[1px] w-3.5 h-3.5 shrink-0'}><RiQuestionLine className='text-text-quaternary hover:text-text-tertiary w-full h-full' /></div>}
  85. </PortalToFollowElemTrigger>
  86. <PortalToFollowElemContent
  87. className="z-[9999]"
  88. >
  89. {popupContent && (<div
  90. className={cn(
  91. 'relative px-3 py-2 text-xs font-normal text-gray-700 bg-white rounded-md shadow-lg break-words',
  92. popupClassName,
  93. )}
  94. onMouseEnter={() => triggerMethod === 'hover' && setHoverPopup()}
  95. onMouseLeave={() => triggerMethod === 'hover' && handleLeave(false)}
  96. >
  97. {popupContent}
  98. </div>)}
  99. </PortalToFollowElemContent>
  100. </PortalToFollowElem>
  101. )
  102. }
  103. export default React.memo(Tooltip)