Skip to content

Now using clipboard API + Added feedback #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions clientRootMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,21 @@ export default {
copyElement.className = 'code-copy'
copyElement.title = 'Click to Copy to Clipboard'
copyElement.addEventListener( 'click', () => {
this.copyToClipboard( parent.innerText )
this.copyToClipboard( parent.innerText, copyElement )
} )
parent.appendChild( copyElement )
parent.classList.add( 'codecopy-enabled' )
},
copyToClipboard: function( str ) {
const el = document.createElement( 'textarea' )
el.value = str
el.setAttribute( 'readonly', '' )
el.style.position = 'absolute'
el.style.left = '-9999px'
document.body.appendChild( el )
const selected =
document.getSelection().rangeCount > 0
? document.getSelection().getRangeAt( 0 )
: false
el.select()
document.execCommand( 'copy' )
document.body.removeChild( el )
if ( selected ) {
document.getSelection().removeAllRanges()
document.getSelection().addRange( selected )
}
copyToClipboard: function( str, copyElement ) {
navigator.clipboard.writeText(str)
.then(() => {
copyElement.classList.add( '_suc' );
console.log('success');
})
.catch(() => {
copyElement.classList.add( '_err' );
console.log('error');
});
}
}
}
32 changes: 30 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,33 @@
display: inline-block;
cursor: pointer;
}

.code-copy::after {
position: absolute;
right: calc(100% + 8px);
background: #424242e5;
}

.code-copy._suc::after {
content: "Content Copied!";
opacity: 0; /* for animation to work */
animation: fadeout 3s;
}

.code-copy._err::after {
content: "Oops, an error occurred!";
opacity: 0; /* for animation to work */
animation: fadeout 3s;
}

div[class*="language-"] pre .code-copy {
position: absolute;
z-index: 1000;
top: 35px;
right: 5px;
width: 20px;
width: 24px;
height: 30px;
content: url('./copy.svg');
background: no-repeat url('./copy.svg');
opacity: 0;
}

Expand All @@ -21,3 +40,12 @@ div[class*="language-"] pre:hover .code-copy {
.content pre, .content pre[class*=language-] {
overflow-y: hidden;
}

@keyframes fadeout {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}