-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 parent
4c0734c
commit 89d6edd
Showing
7 changed files
with
201 additions
and
57 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
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,88 @@ | ||
import React, { useState } from "react"; | ||
import { | ||
Tooltip, | ||
TooltipContent, | ||
TooltipProvider, | ||
TooltipTrigger, | ||
} from "@/components/ui/tooltip"; | ||
|
||
interface TooltipWrapperProps { | ||
children: React.ReactNode; | ||
value?: string | number | readonly string[]; | ||
showTooltip?: boolean; | ||
tooltipLabel?: string; | ||
clickToCopy?: boolean; | ||
} | ||
|
||
export const TooltipWrapper = ({ | ||
children, | ||
value, | ||
showTooltip = false, | ||
tooltipLabel, | ||
clickToCopy, | ||
}: TooltipWrapperProps) => { | ||
const [showCopied, setShowCopied] = useState(false); | ||
const [isTooltipOpen, setIsTooltipOpen] = useState(false); | ||
|
||
if (!showTooltip) { | ||
return <>{children}</>; | ||
} | ||
|
||
const handleCopy = async () => { | ||
if (!clickToCopy || !value) return; | ||
|
||
try { | ||
await navigator.clipboard.writeText(value.toString()); | ||
setShowCopied(true); | ||
setIsTooltipOpen(true); | ||
|
||
setTimeout(() => { | ||
setShowCopied(false); | ||
setIsTooltipOpen(false); | ||
}, 2000); | ||
} catch (error) { | ||
console.error("Failed to copy:", error); | ||
} | ||
}; | ||
|
||
const getTooltipText = () => { | ||
if (showCopied) return tooltipLabel ? `${tooltipLabel} copied!` : "copied!"; | ||
if (clickToCopy) { | ||
return tooltipLabel ? `copy ${tooltipLabel}` : "click to copy"; | ||
} | ||
return tooltipLabel; | ||
}; | ||
|
||
return ( | ||
<TooltipProvider> | ||
<Tooltip open={isTooltipOpen} onOpenChange={setIsTooltipOpen}> | ||
<div className="relative w-full"> | ||
<TooltipTrigger | ||
className="w-full" | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
if (clickToCopy) handleCopy(); | ||
}} | ||
> | ||
<div | ||
className={`tooltip-wrapper ${ | ||
clickToCopy ? "cursor-pointer" : "" | ||
}`} | ||
> | ||
<div | ||
className={`tooltip-wrapper-inner ${ | ||
clickToCopy ? "copy-enabled" : "" | ||
}`} | ||
> | ||
{children} | ||
</div> | ||
</div> | ||
</TooltipTrigger> | ||
<TooltipContent sideOffset={5}> | ||
<p className="text-xs">{getTooltipText()}</p> | ||
</TooltipContent> | ||
</div> | ||
</Tooltip> | ||
</TooltipProvider> | ||
); | ||
}; |
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
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,20 +1,39 @@ | ||
import * as React from "react"; | ||
import { cn } from "@/lib/utils"; | ||
import { Textarea } from "./textarea"; | ||
import { Textarea } from "@/components/ui/textarea"; | ||
import { TooltipWrapper } from "@/components/TooltipWrapper"; | ||
|
||
const BaseTextarea = React.forwardRef< | ||
HTMLTextAreaElement, | ||
React.ComponentPropsWithoutRef<typeof Textarea> | ||
>(({ className, ...props }, ref) => { | ||
return ( | ||
<Textarea | ||
className={cn("common-component", "common-textarea", className)} | ||
ref={ref} | ||
{...props} | ||
/> | ||
); | ||
}); | ||
interface BaseTextareaProps | ||
extends React.ComponentPropsWithoutRef<typeof Textarea> { | ||
tooltipLabel?: string; | ||
} | ||
|
||
const BaseTextarea = React.forwardRef<HTMLTextAreaElement, BaseTextareaProps>( | ||
({ className, tooltipLabel, disabled, ...props }, ref) => { | ||
return ( | ||
<TooltipWrapper | ||
value={props.value} | ||
showTooltip={disabled} | ||
tooltipLabel={tooltipLabel} | ||
clickToCopy={disabled} | ||
> | ||
<Textarea | ||
className={cn( | ||
"common-component", | ||
"common-textarea", | ||
disabled && "pointer-events-none cursor-pointer", | ||
className | ||
)} | ||
ref={ref} | ||
disabled={disabled} | ||
{...props} | ||
/> | ||
</TooltipWrapper> | ||
); | ||
} | ||
); | ||
|
||
BaseTextarea.displayName = "BaseTextarea"; | ||
|
||
export { BaseTextarea }; | ||
export type { BaseTextareaProps }; |
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