- Notifications
You must be signed in to change notification settings - Fork1.1k
refactor: migrate CopyableValue from MUI to Radix UI#20261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
75bf770e199c543c90fd26a2a508e6a3bc91c700a6589a27aFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,91 @@ | ||
| import { | ||
| Tooltip, | ||
| TooltipContent, | ||
| TooltipProvider, | ||
| TooltipTrigger, | ||
| } from "components/Tooltip/Tooltip"; | ||
| import { useClickable } from "hooks/useClickable"; | ||
| import { useClipboard } from "hooks/useClipboard"; | ||
| import { type FC, type HTMLAttributes, useState } from "react"; | ||
| import { cn } from "utils/cn"; | ||
| type TooltipSide = "top" | "right" | "bottom" | "left"; | ||
| interface CopyableValueProps extends HTMLAttributes<HTMLSpanElement> { | ||
| value: string; | ||
| side?: TooltipSide; | ||
| } | ||
| export const CopyableValue: FC<CopyableValueProps> = ({ | ||
| value, | ||
| side = "bottom", | ||
| children, | ||
| className, | ||
| role, | ||
| tabIndex, | ||
| onClick, | ||
| onKeyDown, | ||
| onKeyUp, | ||
| ...attrs | ||
| }) => { | ||
| const { showCopiedSuccess, copyToClipboard } = useClipboard(); | ||
| const [tooltipOpen, setTooltipOpen] = useState(false); | ||
| const [isFocused, setIsFocused] = useState(false); | ||
Contributor
| ||
| const clickableProps = useClickable<HTMLSpanElement>(() => { | ||
| copyToClipboard(value); | ||
| setTooltipOpen(true); | ||
| }); | ||
| return ( | ||
| <TooltipProvider delayDuration={100}> | ||
| <Tooltip | ||
| open={tooltipOpen} | ||
| onOpenChange={(shouldBeOpen) => { | ||
| // Always keep the tooltip open when in focus to handle issues when onOpenChange is unexpectedly false | ||
| if (!shouldBeOpen && isFocused) return; | ||
| setTooltipOpen(shouldBeOpen); | ||
| }} | ||
| > | ||
| <TooltipTrigger asChild> | ||
| <span | ||
| ref={clickableProps.ref} | ||
| {...attrs} | ||
| className={cn("cursor-pointer", className)} | ||
| role={role ?? clickableProps.role} | ||
| tabIndex={tabIndex ?? clickableProps.tabIndex} | ||
| onClick={(event) => { | ||
| clickableProps.onClick(event); | ||
| onClick?.(event); | ||
| }} | ||
| onKeyDown={(event) => { | ||
| clickableProps.onKeyDown(event); | ||
| onKeyDown?.(event); | ||
| }} | ||
| onKeyUp={(event) => { | ||
| clickableProps.onKeyUp(event); | ||
| onKeyUp?.(event); | ||
| }} | ||
| onMouseEnter={() => { | ||
| setIsFocused(true); | ||
| setTooltipOpen(true); | ||
| }} | ||
| onMouseLeave={() => { | ||
| setTooltipOpen(false); | ||
| }} | ||
| onFocus={() => { | ||
| setIsFocused(true); | ||
| }} | ||
Comment on lines +75 to +77 Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Do we also want to open the tooltip here? | ||
| onBlur={() => { | ||
| setTooltipOpen(false); | ||
| }} | ||
| > | ||
| {children} | ||
| </span> | ||
| </TooltipTrigger> | ||
| <TooltipContent side={side}> | ||
| {showCopiedSuccess ? "Copied!" : "Click to copy"} | ||
| </TooltipContent> | ||
| </Tooltip> | ||
| </TooltipProvider> | ||
| ); | ||
| }; | ||
Uh oh!
There was an error while loading.Please reload this page.