Async Clipboard API Demo

by juberti

HTML

<h1>User Gesture Demo</h1>
<button onclick="doCopy()">
Copy
</button>
<h3 id="indicator" hidden>User Gesture Received</h3>

SCSS

html, body {
  font: 16px/1.3 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-weight: 300;
  height: 100%;
  padding: 0;
  margin: 0;
  background: #eee;
  overflow: hidden;
  text-align: center;
}
h1 {
  margin: 10px;
  font: inherit;
  font-size: 200%;
  color: #634884;
  text-shadow: 0 1px 0 #fff;
}
h3 {
  margin: 0;
  font: inherit;
  font-size: 140%;
  color: #666;
  text-shadow: 0 1px 0 #fff;
}
strong {
  font-weight: 500;
}
em {
  font-size: 80%;
}

JavaScript

doCopy = function() {
	navigator.clipboard.writeText("hi")
  	.then(() => {
      indicator.hidden = false;
    })
  	.catch((e) => {
      //log(e);
      indicator.hidden = true;
    });
};

function log(value) {
  console.log(value);
}

setInterval(doCopy, 500);