Skip to content

Commit d764f80

Browse files
authored
Merge pull request #704 from akkarn1689/main
fixed copy button functionality: issue #698
2 parents ee34118 + ec95573 commit d764f80

File tree

2 files changed

+75
-41
lines changed

2 files changed

+75
-41
lines changed
+65-27
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,80 @@
1+
import { useState } from 'preact/hooks';
12
import CircleButton from "../CircleButton";
23

34
interface CopyCodeButtonProps {
45
textToCopy: string;
56
}
67

78
export const CopyCodeButton = ({ textToCopy }: CopyCodeButtonProps) => {
8-
const copyTextToClipboard = () => {
9-
navigator.clipboard.writeText(textToCopy).catch((err) => {
10-
console.error("Failed to copy text: ", err);
11-
});
9+
const [isCopied, setIsCopied] = useState(false);
10+
11+
const copyTextToClipboard = async () => {
12+
console.log('Copy button clicked');
13+
console.log('Text to copy:', textToCopy);
14+
15+
try {
16+
console.log('Using Clipboard API');
17+
await navigator.clipboard.writeText(textToCopy);
18+
console.log('Text copied successfully');
19+
setIsCopied(true);
20+
setTimeout(() => {
21+
setIsCopied(false);
22+
console.log('Copy state reset');
23+
}, 2000);
24+
} catch (err) {
25+
console.error('Clipboard API copy failed:', err);
26+
}
1227
};
1328

29+
console.log('Component rendered, isCopied:', isCopied);
30+
1431
return (
1532
<CircleButton
16-
onClick={copyTextToClipboard}
33+
onClick={() => {
34+
console.log('CircleButton clicked');
35+
copyTextToClipboard();
36+
}}
1737
ariaLabel="Copy code to clipboard"
18-
className="bg-white text-black"
38+
className={`bg-white ${isCopied ? 'text-green-600' : 'text-black'} transition-colors duration-200`}
1939
>
20-
<svg
21-
width="18"
22-
height="22"
23-
viewBox="4 7 18 23"
24-
fill="none"
25-
xmlns="http://www.w3.org/2000/svg"
26-
>
27-
<path
28-
fill-rule="evenodd"
29-
clip-rule="evenodd"
30-
d="M 4.054 12.141 C 4.054 11.865 4.877 11.877 5.153 11.877 L 9.073 11.953 C 9.2 11.953 8.791 22.207 9.006 23.531 C 11.73 24.182 17.631 24.022 17.631 24.171 L 17.638 28.083 C 17.638 28.359 17.414 28.583 17.138 28.583 L 4.554 28.583 C 4.278 28.583 4.054 28.359 4.054 28.083 L 4.054 12.141 Z M 5.054 12.641 L 5.054 27.583 L 16.638 27.583 L 16.735 24.024 L 8.623 24.051 L 8.195 12.679 L 5.054 12.641 Z"
31-
fill="currentColor"
32-
/>
33-
<path
34-
fill-rule="evenodd"
35-
clip-rule="evenodd"
36-
d="M 8.14 8.083 C 8.14 7.807 8.364 7.583 8.64 7.583 L 21.224 7.583 C 21.5 7.583 21.724 7.807 21.724 8.083 L 21.724 24.025 C 21.724 24.301 21.5 24.525 21.224 24.525 L 8.64 24.525 C 8.364 24.525 8.14 24.301 8.14 24.025 L 8.14 8.083 Z M 9.14 8.583 L 9.14 23.525 L 20.724 23.525 L 20.724 8.583 L 9.14 8.583 Z"
37-
fill="currentColor"
38-
/>
39-
</svg>
40+
{isCopied ? (
41+
<svg
42+
width="18"
43+
height="22"
44+
viewBox="0 0 24 24"
45+
fill="none"
46+
xmlns="http://www.w3.org/2000/svg"
47+
>
48+
<path
49+
d="M20 6L9 17L4 12"
50+
stroke="currentColor"
51+
strokeWidth="2"
52+
strokeLinecap="round"
53+
strokeLinejoin="round"
54+
/>
55+
</svg>
56+
) : (
57+
<svg
58+
width="18"
59+
height="22"
60+
viewBox="4 7 18 23"
61+
fill="none"
62+
xmlns="http://www.w3.org/2000/svg"
63+
>
64+
<path
65+
fillRule="evenodd"
66+
clipRule="evenodd"
67+
d="M 4.054 12.141 C 4.054 11.865 4.877 11.877 5.153 11.877 L 9.073 11.953 C 9.2 11.953 8.791 22.207 9.006 23.531 C 11.73 24.182 17.631 24.022 17.631 24.171 L 17.638 28.083 C 17.638 28.359 17.414 28.583 17.138 28.583 L 4.554 28.583 C 4.278 28.583 4.054 28.359 4.054 28.083 L 4.054 12.141 Z M 5.054 12.641 L 5.054 27.583 L 16.638 27.583 L 16.735 24.024 L 8.623 24.051 L 8.195 12.679 L 5.054 12.641 Z"
68+
fill="currentColor"
69+
/>
70+
<path
71+
fillRule="evenodd"
72+
clipRule="evenodd"
73+
d="M 8.14 8.083 C 8.14 7.807 8.364 7.583 8.64 7.583 L 21.224 7.583 C 21.5 7.583 21.724 7.807 21.724 8.083 L 21.724 24.025 C 21.724 24.301 21.5 24.525 21.224 24.525 L 8.64 24.525 C 8.364 24.525 8.14 24.301 8.14 24.025 L 8.14 8.083 Z M 9.14 8.583 L 9.14 23.525 L 20.724 23.525 L 20.724 8.583 L 9.14 8.583 Z"
74+
fill="currentColor"
75+
/>
76+
</svg>
77+
)}
4078
</CircleButton>
4179
);
42-
};
80+
};

src/components/MethodSignature/index.astro

+10-14
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,27 @@ import {
1111
} from "@/types/parsers.interface.ts";
1212
import CodeContainer from "@components/CodeContainer/index.astro";
1313
import { CopyCodeButton } from "../CopyCodeButton";
14+
1415
const { params, name, overloads } = Astro.props;
1516
1617
const formatParam = (param: ReferenceParam) =>
1718
param.optional ? `[${param.name}]` : `${param.name}`;
1819
19-
let formattedParams = [];
20-
21-
if (params) {
22-
formattedParams.push(params.map(formatParam).join(", "));
23-
} else if (overloads) {
24-
for (const overload of overloads) {
25-
formattedParams.push(overload.params.map(formatParam).join(", "));
26-
}
27-
}
28-
29-
const signature = `${name}(${formattedParams})`;
20+
const signatures = params
21+
? [`${name}(${params.map(formatParam).join(", ")})`]
22+
: overloads
23+
? overloads.map(overload =>
24+
`${name}(${overload.params.map(formatParam).join(", ")})`)
25+
: [];
3026
---
3127

3228
{
33-
formattedParams.map((params) => (
29+
signatures.map((signature) => (
3430
<CodeContainer>
35-
<span class="text-black">{`${name}(${params})`}</span>
31+
<span class="text-black">{signature}</span>
3632
<div class="absolute top-xs right-xs">
3733
<CopyCodeButton client:load textToCopy={signature} />
3834
</div>
3935
</CodeContainer>
4036
))
41-
}
37+
}

0 commit comments

Comments
 (0)