-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
79 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
import { type ReactElement } from 'react' | ||
|
||
import type { ReactNode, ReactElement } from 'react' | ||
import CopyButton from '../CopyButton' | ||
|
||
const CopyAddressButton = ({ | ||
prefix, | ||
address, | ||
copyPrefix, | ||
children, | ||
}: { | ||
prefix?: string | ||
address: string | ||
copyPrefix?: boolean | ||
children?: ReactNode | ||
}): ReactElement => { | ||
const addressText = copyPrefix && prefix ? `${prefix}:${address}` : address | ||
|
||
return <CopyButton text={addressText} /> | ||
return <CopyButton text={addressText}>{children}</CopyButton> | ||
} | ||
|
||
export default CopyAddressButton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { ReactNode } from 'react' | ||
import React, { type ReactElement, type SyntheticEvent, useCallback, useState } from 'react' | ||
import { Tooltip } from '@mui/material' | ||
|
||
const cursorPointer = { cursor: 'pointer' } | ||
|
||
const CopyTooltip = ({ | ||
text, | ||
children, | ||
initialToolTipText = 'Copy to clipboard', | ||
onCopy, | ||
}: { | ||
text: string | ||
children?: ReactNode | ||
initialToolTipText?: string | ||
onCopy?: () => void | ||
}): ReactElement => { | ||
const [tooltipText, setTooltipText] = useState(initialToolTipText) | ||
const [isCopyEnabled, setIsCopyEnabled] = useState(true) | ||
|
||
const handleCopy = useCallback( | ||
(e: SyntheticEvent) => { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
try { | ||
navigator.clipboard.writeText(text).then(() => setTooltipText('Copied')) | ||
onCopy?.() | ||
} catch (err) { | ||
setIsCopyEnabled(false) | ||
setTooltipText('Copying is disabled in your browser') | ||
} | ||
}, | ||
[text, onCopy], | ||
) | ||
|
||
const handleMouseLeave = useCallback(() => { | ||
setTimeout(() => { | ||
if (isCopyEnabled) { | ||
setTooltipText(initialToolTipText) | ||
} | ||
}, 500) | ||
}, [initialToolTipText, isCopyEnabled]) | ||
|
||
return ( | ||
<Tooltip title={tooltipText} placement="top" onMouseLeave={handleMouseLeave}> | ||
<span onClick={handleCopy} style={cursorPointer}> | ||
{children} | ||
</span> | ||
</Tooltip> | ||
) | ||
} | ||
|
||
export default CopyTooltip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters