Home > Software engineering >  How to generate a copyable link to clipboard that is clickable in gmail
How to generate a copyable link to clipboard that is clickable in gmail

Time:08-03

I have to generate a url to the clipboard when user click the button. The question is, I have no idea how can I make this url clickable when I paste it into email. The result turns out to plain text. And I am using ReactJs as my frontend framework. I have tried using Clipboard.writeText(), but it not works. I also tried to paste html code to email, but all I get is plain text. I have seen other website implemented this function, but I really don't know how to do this.

CodePudding user response:

You can copy a string to the clipboard by triggering the following:

navigator.clipboard.writeText('text');

Complete example

const [text, setText] = useState("");

  const handleChange = (e) => {
    setText(e.target.value);
  };

  const handleCopy = () => {
    navigator.clipboard.writeText(text);
  };

  return (
    <div>
      <input type="text" value={text} onChange={handleChange} />
      <button onClick={handleCopy}>Copy</button>
    </div>
  );

If that doesn't work, I'd check the console for any other errors.

More in docs

CodePudding user response:

I think I have solved this by myself. Just put html code as [text/html] type into clipboard api and it can be copied as it looks like in a webpage.

  • Related