At Envato, we use a Slack shortcut that returns the link to a Zip file containing the logos, but it’s cumbersome to find an exact variant and format of the logo in a Zip file since we have so many logos — multiple products, sub brands, and several variations of them. So, we decided to create a web page to make this process easier.
While designing the logo box with a download button, it occurred to me that, lately, I’ve never really downloaded any SVG files. I just copy the code from the browser dev tool and paste in Figma directly. Modern design tools are smart enough to import SVG code as a vector object instead of text. So we can basically make a ‘copy‘ button that copies the SVG code to the clipboard, then paste in the design tools without worrying about the file.
Checking SVG files, and organising file names
Before we start writing any code, we need to make sure that the SVG files are all optimised and compatible with the design tools without any glitch. Otherwise, we might get confused later in the debugging process. Additionally, the file naming convention should be consistent and clear, so they can be used within codes and are also comprehensible to people.
Injecting SVG to DOM
In order to allow SVG code to be copied, it should be injected to DOM as inline code, instead of using <img>
tag. And react-svg
is a library built exactly for that purpose.
Making file links downloadable
When an anchor tag has download
attribute, it tells the browser to download the resource that the anchor is linked to rather than navigate to it.
Most of the major browsers support the download
attribute.
Interacting with the system clipboard
There are different ways to interact with the system clipboard. We can simply use a third-party library like clipboard.js
, or use the modern asynchronous Clipboard API. But in this example, I’m just going to use the good old document.execCommand()
method, which is supported more widely, including older browsers.
We can also simply change the copy button label by using the State Hook.