-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add copy button for accounts (#7197)
* add copy button for accounts * move the logic into the hook * Update useCopyToClipboard.tsx
- Loading branch information
1 parent
cc0e5ac
commit f4d3afa
Showing
4 changed files
with
108 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { CheckIcon, CopyIcon } from '@chakra-ui/icons' | ||
import { Flex, IconButton } from '@chakra-ui/react' | ||
import type { PropsWithChildren } from 'react' | ||
import React, { useCallback } from 'react' | ||
import { useTranslate } from 'react-polyglot' | ||
import { useCopyToClipboard } from 'hooks/useCopyToClipboard' | ||
|
||
import { TooltipWithTouch } from './TooltipWithTouch' | ||
|
||
type InlineCopyButtonProps = { | ||
value: string | ||
isDisabled?: boolean | ||
} & PropsWithChildren | ||
|
||
const copyIcon = <CopyIcon /> | ||
const checkIcon = <CheckIcon /> | ||
|
||
export const InlineCopyButton: React.FC<InlineCopyButtonProps> = ({ | ||
value, | ||
children, | ||
isDisabled, | ||
}) => { | ||
const translate = useTranslate() | ||
const { isCopied, copyToClipboard } = useCopyToClipboard({ timeout: 2000 }) | ||
|
||
const handleCopyClick = useCallback(() => { | ||
copyToClipboard(value) | ||
}, [copyToClipboard, value]) | ||
|
||
// Hide the copy button if it is disabled | ||
if (isDisabled) return <>{children}</> | ||
|
||
return ( | ||
<Flex gap={2} alignItems='center'> | ||
{children} | ||
<TooltipWithTouch label={translate(isCopied ? 'common.copied' : 'common.copy')}> | ||
<IconButton | ||
icon={isCopied ? checkIcon : copyIcon} | ||
colorScheme={isCopied ? 'green' : 'gray'} | ||
size='sm' | ||
variant='ghost' | ||
fontSize='xl' | ||
aria-label='Copy value' | ||
onClick={handleCopyClick} | ||
/> | ||
</TooltipWithTouch> | ||
</Flex> | ||
) | ||
} |
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,36 @@ | ||
import { useState } from 'react' | ||
|
||
export interface useCopyToClipboardProps { | ||
timeout?: number | ||
} | ||
|
||
export function useCopyToClipboard({ timeout = 2000 }: useCopyToClipboardProps) { | ||
const [isCopied, setIsCopied] = useState<boolean>(false) | ||
const [isCopying, setIsCopying] = useState<boolean>(false) | ||
|
||
const copyToClipboard = (value: string) => { | ||
if (typeof window === 'undefined' || !navigator.clipboard?.writeText) { | ||
return | ||
} | ||
|
||
if (!value) { | ||
return | ||
} | ||
|
||
// Prevent race condition if the user clicks copy multiple times | ||
if (isCopying) return | ||
setIsCopying(true) | ||
|
||
void navigator.clipboard.writeText(value).then(() => { | ||
setIsCopied(true) | ||
|
||
setTimeout(() => { | ||
// Reset the state after the timeout | ||
setIsCopying(false) | ||
setIsCopied(false) | ||
}, timeout) | ||
}) | ||
} | ||
|
||
return { isCopied, copyToClipboard } | ||
} |
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