create easy to copy paste svg in javascript

index.tsx
const svgCode = `
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
  <rect width="100" height="100" style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)" />
</svg>
`;

// Copy to clipboard
function copyToClipboard(text) {
  const el = document.createElement("textarea");
  el.value = text;
  document.body.appendChild(el);
  el.select();
  document.execCommand("copy");
  document.body.removeChild(el);
}

copyToClipboard(svgCode);
console.log("SVG code has been copied to clipboard");
508 chars
19 lines

Make sure to adjust the SVG code within the svgCode variable to generate the desired SVG image.

related categories

gistlibby LogSnag