mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
7c97f8ab6e
* Use new JSX runtime and update Babel Node target * Update .eslintrc * Remove React imports GitOrigin-RevId: 559de0267f8f2934c56a860ea8701bb522aa861a
51 lines
1.2 KiB
JavaScript
51 lines
1.2 KiB
JavaScript
import { useCallback, useState } from 'react'
|
|
import { Button, OverlayTrigger, Tooltip } from 'react-bootstrap'
|
|
import PropTypes from 'prop-types'
|
|
import { Trans, useTranslation } from 'react-i18next'
|
|
import Icon from './icon'
|
|
|
|
export default function CopyLink({ link, tooltipId }) {
|
|
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 (
|
|
<OverlayTrigger
|
|
placement="top"
|
|
delayHide={copied ? 1000 : 250}
|
|
shouldUpdatePosition
|
|
overlay={
|
|
<Tooltip id={tooltipId}>
|
|
{copied ? 'Copied!' : <Trans i18nKey="copy" />}
|
|
</Tooltip>
|
|
}
|
|
>
|
|
<Button
|
|
onClick={handleClick}
|
|
bsSize="xsmall"
|
|
bsStyle="link"
|
|
className="copy-button"
|
|
aria-label={t('copy')}
|
|
>
|
|
{copied ? <Icon type="check" /> : <Icon type="clipboard" />}
|
|
</Button>
|
|
</OverlayTrigger>
|
|
)
|
|
}
|
|
CopyLink.propTypes = {
|
|
link: PropTypes.string.isRequired,
|
|
tooltipId: PropTypes.string.isRequired,
|
|
}
|