-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add Copy & Tooltip component from tzsafe-ui
- Loading branch information
Quentin Burg
committed
Sep 4, 2023
1 parent
224b6b2
commit a74d673
Showing
5 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,47 @@ | ||
import { ReactNode, useEffect, useState } from 'react'; | ||
import Tooltip from './Tooltip'; | ||
|
||
export type copyProps = { | ||
children: ReactNode; | ||
value: string; | ||
disabled?: boolean; | ||
text?: string; | ||
}; | ||
|
||
const Copy = ({ children, value, disabled = false, text }: copyProps) => { | ||
const [isCopying, setIsCopying] = useState(false); | ||
|
||
useEffect(() => { | ||
if (!isCopying) return; | ||
|
||
setTimeout(() => { | ||
setIsCopying(false); | ||
}, 1500); | ||
}); | ||
|
||
return ( | ||
<Tooltip | ||
text={isCopying ? 'Copied' : text ?? 'Copy'} | ||
disabled={disabled} | ||
visible={isCopying ? true : disabled ? false : undefined}> | ||
<a | ||
href="#" | ||
onClick={async () => { | ||
if (disabled) return; | ||
|
||
setIsCopying(true); | ||
|
||
try { | ||
await navigator.clipboard.writeText(value); | ||
} catch (err) { | ||
console.error('Failed to copy: ', err); | ||
} | ||
}} | ||
data-name="copy"> | ||
{children} | ||
</a> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
export default Copy; |
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 * as TooltipPrimitive from '@radix-ui/react-tooltip'; | ||
import React, { ReactNode } from 'react'; | ||
|
||
type tooltipProps = { | ||
children: ReactNode; | ||
text: string; | ||
disabled?: boolean; | ||
visible?: boolean; | ||
}; | ||
|
||
const Tooltip = ({ | ||
children, | ||
text, | ||
disabled = false, | ||
visible, | ||
}: tooltipProps) => { | ||
return ( | ||
<TooltipPrimitive.Provider delayDuration={0}> | ||
<TooltipPrimitive.Root open={visible}> | ||
<TooltipPrimitive.Trigger asChild disabled={disabled}> | ||
{children} | ||
</TooltipPrimitive.Trigger> | ||
<TooltipPrimitive.Content | ||
sideOffset={4} | ||
className="radix-side-top:animate-slide-down-fade radix-side-right:animate-slide-left-fade radix-side-bottom:animate-slide-up-fade radix-side-left:animate-slide-right-fade inline-flex items-center rounded-md bg-zinc-800 px-4 py-2.5 shadow-lg"> | ||
<TooltipPrimitive.Arrow className="fill-current text-zinc-800" /> | ||
<span className="block max-w-xs text-xs leading-none text-zinc-100"> | ||
{text} | ||
</span> | ||
</TooltipPrimitive.Content> | ||
</TooltipPrimitive.Root> | ||
</TooltipPrimitive.Provider> | ||
); | ||
}; | ||
|
||
export default Tooltip; |