|
@@ -1,7 +1,6 @@
|
|
|
'use client'
|
|
|
-import React, { useState } from 'react'
|
|
|
+import React, { useEffect, useRef, useState } from 'react'
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
-import { debounce } from 'lodash-es'
|
|
|
import QRCode from 'qrcode.react'
|
|
|
import Tooltip from '../tooltip'
|
|
|
import QrcodeStyle from './style.module.css'
|
|
@@ -16,10 +15,27 @@ const prefixEmbedded = 'appOverview.overview.appInfo.qrcode.title'
|
|
|
|
|
|
const ShareQRCode = ({ content, selectorId, className }: Props) => {
|
|
|
const { t } = useTranslation()
|
|
|
- const [isShow, setisShow] = useState<boolean>(false)
|
|
|
- const onClickShow = debounce(() => {
|
|
|
- setisShow(true)
|
|
|
- }, 100)
|
|
|
+ const [isShow, setIsShow] = useState<boolean>(false)
|
|
|
+ const qrCodeRef = useRef<HTMLDivElement>(null)
|
|
|
+
|
|
|
+ const toggleQRCode = (event: React.MouseEvent) => {
|
|
|
+ event.stopPropagation()
|
|
|
+ setIsShow(prev => !prev)
|
|
|
+ }
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ const handleClickOutside = (event: MouseEvent) => {
|
|
|
+ if (qrCodeRef.current && !qrCodeRef.current.contains(event.target as Node))
|
|
|
+ setIsShow(false)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isShow)
|
|
|
+ document.addEventListener('click', handleClickOutside)
|
|
|
+
|
|
|
+ return () => {
|
|
|
+ document.removeEventListener('click', handleClickOutside)
|
|
|
+ }
|
|
|
+ }, [isShow])
|
|
|
|
|
|
const downloadQR = () => {
|
|
|
const canvas = document.getElementsByTagName('canvas')[0]
|
|
@@ -29,9 +45,9 @@ const ShareQRCode = ({ content, selectorId, className }: Props) => {
|
|
|
link.click()
|
|
|
}
|
|
|
|
|
|
- const onMouseLeave = debounce(() => {
|
|
|
- setisShow(false)
|
|
|
- }, 500)
|
|
|
+ const handlePanelClick = (event: React.MouseEvent) => {
|
|
|
+ event.stopPropagation()
|
|
|
+ }
|
|
|
|
|
|
return (
|
|
|
<Tooltip
|
|
@@ -40,19 +56,23 @@ const ShareQRCode = ({ content, selectorId, className }: Props) => {
|
|
|
>
|
|
|
<div
|
|
|
className={`w-8 h-8 cursor-pointer rounded-lg ${className ?? ''}`}
|
|
|
- onMouseLeave={onMouseLeave}
|
|
|
- onClick={onClickShow}
|
|
|
+ onClick={toggleQRCode}
|
|
|
>
|
|
|
<div className={`w-full h-full ${QrcodeStyle.QrcodeIcon} ${isShow ? QrcodeStyle.show : ''}`} />
|
|
|
- {isShow && <div className={QrcodeStyle.qrcodeform}>
|
|
|
- <QRCode size={160} value={content} className={QrcodeStyle.qrcodeimage}/>
|
|
|
- <div className={QrcodeStyle.text}>
|
|
|
- <div className={`text-gray-500 ${QrcodeStyle.scan}`}>{t('appOverview.overview.appInfo.qrcode.scan')}</div>
|
|
|
- <div className={`text-gray-500 ${QrcodeStyle.scan}`}>·</div>
|
|
|
- <div className={QrcodeStyle.download} onClick={downloadQR}>{t('appOverview.overview.appInfo.qrcode.download')}</div>
|
|
|
+ {isShow && (
|
|
|
+ <div
|
|
|
+ ref={qrCodeRef}
|
|
|
+ className={QrcodeStyle.qrcodeform}
|
|
|
+ onClick={handlePanelClick}
|
|
|
+ >
|
|
|
+ <QRCode size={160} value={content} className={QrcodeStyle.qrcodeimage}/>
|
|
|
+ <div className={QrcodeStyle.text}>
|
|
|
+ <div className={`text-gray-500 ${QrcodeStyle.scan}`}>{t('appOverview.overview.appInfo.qrcode.scan')}</div>
|
|
|
+ <div className={`text-gray-500 ${QrcodeStyle.scan}`}>·</div>
|
|
|
+ <div className={QrcodeStyle.download} onClick={downloadQR}>{t('appOverview.overview.appInfo.qrcode.download')}</div>
|
|
|
+ </div>
|
|
|
</div>
|
|
|
- </div>
|
|
|
- }
|
|
|
+ )}
|
|
|
</div>
|
|
|
</Tooltip>
|
|
|
)
|