bitburner-src/src/ui/React/CopyableText.tsx

48 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-10-01 19:36:59 +02:00
import React, { useState } from "react";
import Typography from "@mui/material/Typography";
import Tooltip from "@mui/material/Tooltip";
type IProps = {
2021-09-05 01:09:30 +02:00
value: string;
2021-10-01 19:36:59 +02:00
variant?:
| "button"
| "caption"
| "h1"
| "h2"
| "h3"
| "h4"
| "h5"
| "h6"
| "subtitle1"
| "subtitle2"
| "body1"
| "body2"
| "overline"
| "inherit"
| undefined;
2021-09-05 01:09:30 +02:00
};
2021-10-01 19:36:59 +02:00
export function CopyableText(props: IProps): React.ReactElement {
const [open, setOpen] = useState(false);
2021-10-01 19:36:59 +02:00
function copy(): void {
2021-09-05 01:09:30 +02:00
const copyText = document.createElement("textarea");
2021-10-01 19:36:59 +02:00
copyText.value = props.value;
2021-09-05 01:09:30 +02:00
document.body.appendChild(copyText);
copyText.select();
copyText.setSelectionRange(0, 1e10);
document.execCommand("copy");
document.body.removeChild(copyText);
2021-10-01 19:36:59 +02:00
setOpen(true);
setTimeout(() => setOpen(false), 1000);
2021-09-05 01:09:30 +02:00
}
2021-10-01 19:36:59 +02:00
return (
<Tooltip open={open} title={<Typography>Copied!</Typography>}>
<Typography variant={props.variant} onClick={copy}>
{props.value}
</Typography>
</Tooltip>
);
2021-09-05 01:09:30 +02:00
}