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

Loading…
Cancel
Save