JSFiddle - React, Tailwind, and code Playground
by Nam CuPeo
HTML
<pre><code>
Code
1
2
3
</code></pre>
CSS
#clpmsg,
#clpmsg::before,
.clpbrd {
position: absolute
}
pre {
position: relative;
border-color: #f5f5f5;
border-style: solid;
border-width: 1px 1px 1px 0;
padding: 10px;
border-left: solid 5px #4CAF50;
font-size: 15px;
max-height: 500px;
overflow-y: scroll;
white-space: pre-wrap
}
.clpbrd {
background: #fff;
border: 1px solid #d5d5d5;
padding: 2px;
border-radius: 3px;
width: 50px;
height: 25px;
cursor: pointer;
top: 3px;
right: 3px;
-webkit-transition: opacity .3s ease-in-out;
-o-transition: opacity .3s ease-in-out;
transition: opacity .3s ease-in-out;
}
pre .clpbrd:hover #clpmsg,
pre:hover .clpbrd {
opacity: 1
}
pre .clpbrd:active,
pre .clpbrd:hover {
background-color: #ddd;
background-image: none;
border-color: #b5b5b5;
transition: .3s;
box-shadow: inset 0 2px 4px rgba(0, 0, 0, .15)
}
pre .clpbrd:focus {
text-decoration: none;
border-color: #51a7e8;
outline: 0;
box-shadow: 0 0 5px rgba(81, 167, 232, .5)
}
#clpmsg {
background-color: #000;
color: #fff;
padding: 3px 5px;
border-radius: 3px;
word-break: initial;
height: 17px;
width:60px;
top: -1px;
right: 31px;
opacity: 0;
-webkit-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out
}
#clpmsg::before {
content: "";
border: 10px solid;
top: 2px;
border-color: transparent transparent transparent #000;
right: 0;
margin-right: -15px;
width: 0;
height: 0
}
.nutcopy {width:100%; padding:0 5px;}
JavaScript
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.1/clipboard.min.js'></script>
<script>function showTooltip(elem){$("<span id='clpmsg'>Đã copy 😆</span>").prependTo(elem),$(elem).children().first().delay(1e3).fadeOut(1e3,function(){$(this).remove()})}$("<button class='clpbrd' data-clipboard-action='copy' data-clipboard-target='pre code'>Copy</button>").insertBefore("pre code");var clipboard=new ClipboardJS(".clpbrd",{target:function(trigger){return trigger.nextElementSibling}});clipboard.on("success",function(e){e.clearSelection(),showTooltip(e.trigger)});var TopOffset=parseInt($(".clpbrd").css("top"));$("pre").scroll(function(){$(".clpbrd").css({top:$(this).scrollTop()+TopOffset})});</script>