Added copy functionality in solutions page (#32)

Added copy button to solution code blocks

- Implement copy to clipboard functionality in SolutionSection component
- Add visual feedback when code is copied ("Copy" changes to "Copied!")
- Auto-reset copy state after 2 seconds
- Position button in top-right corner with hover effects
- Maintain existing component functionality

credits to @bhaumikmaan
main
Bhaumik Maan 2 years ago committed by GitHub
parent 62dcb536ac
commit 62082f38e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -48,41 +48,60 @@ const SolutionSection = ({
content: React.ReactNode
isLoading: boolean
currentLanguage: string
}) => (
<div className="space-y-2">
<h2 className="text-[13px] font-medium text-white tracking-wide">
{title}
</h2>
{isLoading ? (
<div className="space-y-1.5">
<div className="mt-4 flex">
<p className="text-xs bg-gradient-to-r from-gray-300 via-gray-100 to-gray-300 bg-clip-text text-transparent animate-pulse">
Loading solutions...
</p>
}) => {
const [copied, setCopied] = useState(false)
const copyToClipboard = () => {
if (typeof content === "string") {
navigator.clipboard.writeText(content).then(() => {
setCopied(true)
setTimeout(() => setCopied(false), 2000)
})
}
}
return (
<div className="space-y-2 relative">
<h2 className="text-[13px] font-medium text-white tracking-wide">
{title}
</h2>
{isLoading ? (
<div className="space-y-1.5">
<div className="mt-4 flex">
<p className="text-xs bg-gradient-to-r from-gray-300 via-gray-100 to-gray-300 bg-clip-text text-transparent animate-pulse">
Loading solutions...
</p>
</div>
</div>
</div>
) : (
<div className="w-full">
<SyntaxHighlighter
showLineNumbers
language={currentLanguage == "golang" ? "go" : currentLanguage}
style={dracula}
customStyle={{
maxWidth: "100%",
margin: 0,
padding: "1rem",
whiteSpace: "pre-wrap",
wordBreak: "break-all",
backgroundColor: "rgba(22, 27, 34, 0.5)"
}}
wrapLongLines={true}
>
{content as string}
</SyntaxHighlighter>
</div>
)}
</div>
)
) : (
<div className="w-full relative">
<button
onClick={copyToClipboard}
className="absolute top-2 right-2 text-xs text-white bg-white/10 hover:bg-white/20 rounded px-2 py-1 transition"
>
{copied ? "Copied!" : "Copy"}
</button>
<SyntaxHighlighter
showLineNumbers
language={currentLanguage == "golang" ? "go" : currentLanguage}
style={dracula}
customStyle={{
maxWidth: "100%",
margin: 0,
padding: "1rem",
whiteSpace: "pre-wrap",
wordBreak: "break-all",
backgroundColor: "rgba(22, 27, 34, 0.5)"
}}
wrapLongLines={true}
>
{content as string}
</SyntaxHighlighter>
</div>
)}
</div>
)
}
export const ComplexitySection = ({
timeComplexity,

Loading…
Cancel
Save