-
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.
feat: added clipboard copy component (#121)
- Loading branch information
Fran McDade
authored and
Fran McDade
committed
Oct 17, 2024
1 parent
9a86469
commit 9c7836a
Showing
6 changed files
with
68 additions
and
11 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
app/components/common/CopyText/components/ClipboardCopy/clipboardCopy.tsx
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,39 @@ | ||
import ContentCopyRoundedIcon from "@mui/icons-material/ContentCopyRounded"; | ||
import { IconButton, Tooltip, TooltipProps } from "@mui/material"; | ||
import copy from "copy-to-clipboard"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { ICON_BUTTON_PROPS, ICON_PROPS, TOOLTIP_PROPS } from "./constants"; | ||
|
||
export interface CopyToClipboardProps { | ||
timeoutDelay?: number; | ||
tooltipProps?: Partial<TooltipProps>; | ||
value: boolean | number | string; | ||
} | ||
|
||
export const ClipboardCopy = ({ | ||
timeoutDelay = 2000, | ||
tooltipProps, | ||
value, | ||
}: CopyToClipboardProps): JSX.Element => { | ||
const [open, setOpen] = useState<boolean>(false); | ||
|
||
const onCopy = useCallback((): void => { | ||
copy(value.toString()); | ||
setOpen(true); | ||
}, [value]); | ||
|
||
useEffect(() => { | ||
if (open) { | ||
const timeout = setTimeout(() => setOpen(false), timeoutDelay); | ||
return (): void => clearTimeout(timeout); | ||
} | ||
}, [open, timeoutDelay]); | ||
|
||
return ( | ||
<Tooltip {...TOOLTIP_PROPS} open={open} {...tooltipProps}> | ||
<IconButton {...ICON_BUTTON_PROPS} onClick={onCopy}> | ||
<ContentCopyRoundedIcon {...ICON_PROPS} /> | ||
</IconButton> | ||
</Tooltip> | ||
); | ||
}; |
18 changes: 18 additions & 0 deletions
18
app/components/common/CopyText/components/ClipboardCopy/constants.ts
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,18 @@ | ||
import { IconButtonProps, IconProps, TooltipProps } from "@mui/material"; | ||
|
||
export const ICON_BUTTON_PROPS: Partial<IconButtonProps> = { | ||
size: "xxsmall", | ||
}; | ||
|
||
export const ICON_PROPS: Pick<IconProps, "color" | "fontSize"> = { | ||
color: "primary", | ||
fontSize: "small", | ||
}; | ||
|
||
export const TOOLTIP_PROPS: Required<Pick<TooltipProps, "title">> & | ||
Partial<Omit<TooltipProps, "title">> = { | ||
arrow: true, | ||
disableHoverListener: true, | ||
placement: "top", | ||
title: "Copied", | ||
}; |
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,24 +1,24 @@ | ||
import { CopyToClipboard } from "@databiosphere/findable-ui/lib/components/common/CopyToClipboard/copyToClipboard"; | ||
import { Grid2, Grid2Props, Typography } from "@mui/material"; | ||
import { ReactNode } from "react"; | ||
import { ClipboardCopy } from "./components/ClipboardCopy/clipboardCopy"; | ||
|
||
export interface CopyTextProps { | ||
children: ReactNode; | ||
gridProps?: Partial<Grid2Props>; | ||
text: string; | ||
value: boolean | number | string; | ||
} | ||
|
||
export const CopyText = ({ | ||
children, | ||
gridProps, | ||
text, | ||
value, | ||
}: CopyTextProps): JSX.Element => { | ||
return ( | ||
<Grid2 container columnSpacing={1} wrap="nowrap" {...gridProps}> | ||
<Typography component="span" noWrap> | ||
{children} | ||
</Typography> | ||
<CopyToClipboard copyStr={text} /> | ||
<ClipboardCopy value={value} /> | ||
</Grid2> | ||
); | ||
}; |
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
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