overleaf/services/web/frontend/js/shared/components/copy-link.js
Alf Eaton 1be43911b4 Merge pull request #3942 from overleaf/prettier-trailing-comma
Set Prettier's "trailingComma" setting to "es5"

GitOrigin-RevId: 9f14150511929a855b27467ad17be6ab262fe5d5
2021-04-28 02:10:01 +00:00

49 lines
1.2 KiB
JavaScript

import React, { useCallback, useState } from 'react'
import { Button, OverlayTrigger, Tooltip } from 'react-bootstrap'
import PropTypes from 'prop-types'
import { Trans } from 'react-i18next'
import Icon from './icon'
export default function CopyLink({ link, tooltipId }) {
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="Copy"
>
{copied ? <Icon type="check" /> : <Icon type="clipboard" />}
</Button>
</OverlayTrigger>
)
}
CopyLink.propTypes = {
link: PropTypes.string.isRequired,
tooltipId: PropTypes.string.isRequired,
}