Spaces:
Sleeping
Sleeping
File size: 1,265 Bytes
7b0525c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import React, { useState } from 'react';
import { Download, Check } from 'lucide-react';
export const DownloadButton = ({ text, filename = 'download.txt', className = '', ...props }: { text: string, filename?: string, className?: string } & React.ButtonHTMLAttributes<HTMLButtonElement>) => {
const [downloaded, setDownloaded] = useState(false);
const handleDownload = () => {
const element = document.createElement("a");
const file = new Blob([text], { type: 'text/plain' });
element.href = URL.createObjectURL(file);
element.download = filename;
document.body.appendChild(element); // Required for this to work in FireFox
element.click();
document.body.removeChild(element);
setDownloaded(true);
setTimeout(() => setDownloaded(false), 2000);
};
return (
<button
onClick={handleDownload}
className={`p-1.5 bg-[#2d2d2d] border border-white/10 rounded-md text-gray-400 hover:text-white hover:bg-primary/20 transition-all z-10 ${className}`}
title="Descargar archivo"
{...props}
>
{downloaded ? <Check size={14} /> : <Download size={14} />}
</button>
);
};
|