Files
solana/explorer/src/components/Copyable.tsx

46 lines
1.1 KiB
TypeScript
Raw Normal View History

import React, { useState, ReactNode } from "react";
type CopyableProps = {
text: string;
children: ReactNode;
};
2020-04-22 22:01:56 +08:00
type State = "hide" | "copy" | "copied";
function Popover({ state }: { state: State }) {
if (state === "hide") return null;
const text = state === "copy" ? "Copy" : "Copied!";
return (
<div className="popover bs-popover-top show">
<div className="arrow" />
<div className="popover-body">{text}</div>
</div>
);
}
function Copyable({ text, children }: CopyableProps) {
2020-04-22 22:01:56 +08:00
const [state, setState] = useState<State>("hide");
const copyToClipboard = () => navigator.clipboard.writeText(text);
const handleClick = () =>
copyToClipboard().then(() => {
2020-04-22 22:01:56 +08:00
setState("copied");
setTimeout(() => setState("hide"), 1000);
});
return (
<div className="copyable">
2020-04-22 22:01:56 +08:00
<div
onClick={handleClick}
onMouseOver={() => setState("copy")}
onMouseOut={() => state === "copy" && setState("hide")}
>
{children}
</div>
<Popover state={state} />
</div>
);
}
export default Copyable;