overleaf/services/web/frontend/js/shared/components/copy-link.tsx
Timothée Alby 5731463d32 Merge pull request #7956 from overleaf/ii-refactor-toolitp-usage
Tooltip usage refactoring

GitOrigin-RevId: f4b2d4d57722172141a081dd60e4394ff7fff332
2022-05-19 08:04:07 +00:00

52 lines
1.2 KiB
TypeScript

import { useCallback, useState } from 'react'
import { Button } from 'react-bootstrap'
import { Trans, useTranslation } from 'react-i18next'
import Tooltip from './tooltip'
import Icon from './icon'
type CopyLinkProps = {
link: string
tooltipId: string
}
function CopyLink({ link, tooltipId }: CopyLinkProps) {
const { t } = useTranslation()
const [copied, setCopied] = useState(false)
const handleClick = useCallback(() => {
navigator.clipboard.writeText(link).then(() => {
setCopied(true)
window.setTimeout(() => {
setCopied(false)
}, 1500)
})
}, [link])
if (!navigator.clipboard?.writeText) {
return null
}
return (
<Tooltip
id={tooltipId}
description={copied ? 'Copied!' : <Trans i18nKey="copy" />}
overlayProps={{
delayHide: copied ? 1000 : 250,
shouldUpdatePosition: true,
}}
>
<Button
onClick={handleClick}
bsSize="xsmall"
bsStyle="link"
className="copy-button"
aria-label={t('copy')}
>
{copied ? <Icon type="check" /> : <Icon type="clipboard" />}
</Button>
</Tooltip>
)
}
export default CopyLink