function getSvgMarkup(el: SVGSVGElement): string { return [ ``, "", '', el.outerHTML, ].join("\n"); } /** * Initiates a download on the user's browser which downloads the given * SVG element under the given filename. */ export function exportSvg( filename: string, svgRef: React.RefObject ) { const svgEl = svgRef.current; if (!svgEl) { alert("Oops, an error occurred! Please try again later."); return; } const dataURL = `data:image/svg+xml;utf8,${encodeURIComponent( getSvgMarkup(svgEl) )}`; const anchor = document.createElement("a"); anchor.href = dataURL; anchor.download = filename; document.body.append(anchor); anchor.click(); document.body.removeChild(anchor); }