JSFiddle - React, Tailwind, and code Playground

HTML

<div id="box"></div>

CSS

#box {
  background: green;
  width: 100px;
  height: 100px;
}

JavaScript

document.querySelector('#box').addEventListener('mousedown', copyFunc);
document.querySelector('#box').addEventListener('contextmenu', function(e) {
    e.preventDefault();
});

function copyFunc(e) {
    var str = e.button === 0 ? 'Left click' : (e.button === 2 ? 'Right click' : 'Any text');
    var el = document.createElement('textarea');
    el.textContent = str;
    el.value = str;
    document.body.appendChild(el);
		
    var selection = document.getSelection();
    var range = document.createRange();

    range.selectNode(el);
    selection.removeAllRanges();
    selection.addRange(range);

    console.log(document.execCommand('copy'));
    selection.removeAllRanges();
    document.body.removeChild(el);
}