Spaces:
Build error
Build error
add copy tag
Browse files
components/editor/main/snippet/index.tsx
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
-
import { ApiRoute } from "@/utils/type";
|
2 |
-
import classNames from "classnames";
|
3 |
import { useState } from "react";
|
|
|
|
|
4 |
import Highlight from "react-highlight";
|
5 |
import { Options } from "redaxios";
|
|
|
|
|
|
|
6 |
|
7 |
const LANGUAGES = ["curl", "javascript", "python"];
|
8 |
|
@@ -16,6 +19,8 @@ export const Snippet = ({
|
|
16 |
body?: Options | undefined;
|
17 |
}) => {
|
18 |
const [language, setLanguage] = useState<string>(LANGUAGES[0]);
|
|
|
|
|
19 |
|
20 |
const generateRequestFromEndpoint = () => {
|
21 |
const { method, path } = endpoint;
|
@@ -89,6 +94,14 @@ export const Snippet = ({
|
|
89 |
return "";
|
90 |
};
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
return (
|
93 |
<div className="mt-8 grid grid-cols-1 gap-4 w-full">
|
94 |
<p className="text-slate-200 uppercase text-xs font-semibold">Snippet</p>
|
@@ -115,9 +128,22 @@ export const Snippet = ({
|
|
115 |
>
|
116 |
{generateRequestFromEndpoint()}
|
117 |
</Highlight>
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
</main>
|
122 |
</div>
|
123 |
</div>
|
|
|
|
|
|
|
1 |
import { useState } from "react";
|
2 |
+
import { useCopyToClipboard } from "react-use";
|
3 |
+
import classNames from "classnames";
|
4 |
import Highlight from "react-highlight";
|
5 |
import { Options } from "redaxios";
|
6 |
+
import { BiSolidCopy } from "react-icons/bi";
|
7 |
+
|
8 |
+
import { ApiRoute } from "@/utils/type";
|
9 |
|
10 |
const LANGUAGES = ["curl", "javascript", "python"];
|
11 |
|
|
|
19 |
body?: Options | undefined;
|
20 |
}) => {
|
21 |
const [language, setLanguage] = useState<string>(LANGUAGES[0]);
|
22 |
+
const [_, copyToClipboard] = useCopyToClipboard();
|
23 |
+
const [isCopied, setIsCopied] = useState<boolean>(false);
|
24 |
|
25 |
const generateRequestFromEndpoint = () => {
|
26 |
const { method, path } = endpoint;
|
|
|
94 |
return "";
|
95 |
};
|
96 |
|
97 |
+
const handleCopyToClipboard = () => {
|
98 |
+
copyToClipboard(generateRequestFromEndpoint());
|
99 |
+
setIsCopied(true);
|
100 |
+
setTimeout(() => {
|
101 |
+
setIsCopied(false);
|
102 |
+
}, 2000);
|
103 |
+
};
|
104 |
+
|
105 |
return (
|
106 |
<div className="mt-8 grid grid-cols-1 gap-4 w-full">
|
107 |
<p className="text-slate-200 uppercase text-xs font-semibold">Snippet</p>
|
|
|
128 |
>
|
129 |
{generateRequestFromEndpoint()}
|
130 |
</Highlight>
|
131 |
+
<div
|
132 |
+
className="flex justify-end relative"
|
133 |
+
onClick={handleCopyToClipboard}
|
134 |
+
>
|
135 |
+
<BiSolidCopy className="text-slate-500 cursor-pointer hover:text-slate-300 transition-all duration-75" />
|
136 |
+
<div
|
137 |
+
className={classNames(
|
138 |
+
"bg-indigo-500/60 text-slate-100 text-xs font-semibold absolute bottom-0 right-0 z-10 rounded-lg px-2 py-1 pointer-events-none -translate-y-full transition-all duration-200",
|
139 |
+
{
|
140 |
+
"opacity-0": !isCopied,
|
141 |
+
}
|
142 |
+
)}
|
143 |
+
>
|
144 |
+
Copied!
|
145 |
+
</div>
|
146 |
+
</div>
|
147 |
</main>
|
148 |
</div>
|
149 |
</div>
|