Repository navigation
Expand file tree
/
Copy pathExportButton.tsx
More file actions
144 lines (136 loc) · 4.62 KB
/
Copy pathExportButton.tsx
File metadata and controls
144 lines (136 loc) · 4.62 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use client';
import { useState } from 'react';
import { useExportImage, ExportFormat } from '@/hooks/useExportImage';
interface ExportButtonProps {
targetSelector?: string;
filename?: string;
scale?: number;
className?: string;
}
export default function ExportButton({
targetSelector = '[data-export-target]',
filename = 'commitpulse-comparison',
scale = 2,
className = '',
}: ExportButtonProps) {
const { exportImage, isExporting, error } = useExportImage({
targetSelector,
filename,
scale,
});
const [dropdownOpen, setDropdownOpen] = useState(false);
const handleExport = async (format: ExportFormat) => {
setDropdownOpen(false);
await exportImage(format);
};
return (
<div className={`relative inline-block ${className}`}>
<button
onClick={() => setDropdownOpen((v) => !v)}
disabled={isExporting}
aria-haspopup="true"
aria-expanded={dropdownOpen}
className="inline-flex items-center gap-2 px-4 py-2 rounded-lg border border-white/10 border-black/20 bg-black text-white hover:bg-black/80 text-sm font-medium text-white transition-all duration-150 disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-500"
>
{isExporting ? (
<>
<svg
className="animate-spin h-4 w-4 text-white"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
/>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8v4l3-3-3-3v4a8 8 0 00-8 8h4z"
/>
</svg>
Exporting…
</>
) : (
<>
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M4 16v2a2 2 0 002 2h12a2 2 0 002-2v-2M7 10l5 5m0 0l5-5m-5 5V4"
/>
</svg>
Export
<svg
xmlns="http://www.w3.org/2000/svg"
className={`h-3 w-3 transition-transform duration-150 ${dropdownOpen ? 'rotate-180' : ''}`}
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2.5}
>
<path strokeLinecap="round" strokeLinejoin="round" d="M19 9l-7 7-7-7" />
</svg>
</>
)}
</button>
{dropdownOpen && !isExporting && (
<>
<div className="fixed inset-0 z-10" onClick={() => setDropdownOpen(false)} />
<ul
role="menu"
className="absolute right-0 z-20 mt-1 w-40 rounded-lg border border-white/10 bg-[#0d1117] shadow-xl overflow-hidden"
>
<li role="none">
<button
role="menuitem"
onClick={() => handleExport('png')}
className="w-full flex items-center gap-2 px-4 py-2.5 text-sm text-left text-white/80 hover:text-white hover:bg-white/10 transition-colors duration-100"
>
Download PNG
</button>
</li>
<li role="none">
<button
role="menuitem"
onClick={() => handleExport('pdf')}
className="w-full flex items-center gap-2 px-4 py-2.5 text-sm text-left text-white/80 hover:text-white hover:bg-white/10 transition-colors duration-100"
>
Download PDF
</button>
</li>
<li role="none">
<button
role="menuitem"
onClick={() => handleExport('svg')}
className="w-full flex items-center gap-2 px-4 py-2.5 text-sm text-left text-white/80 hover:text-white hover:bg-white/10 transition-colors duration-100"
>
Download SVG
</button>
</li>
</ul>
</>
)}
{error && (
<p
role="alert"
className="absolute top-full mt-2 right-0 z-30 w-64 p-2 rounded-lg bg-red-950/80 border border-red-500/30 text-xs text-red-300"
>
{error}
</p>
)}
</div>
);
}