Skip to content

Commit

Permalink
Replace execCommand for copying text with the modern clipboard API (
Browse files Browse the repository at this point in the history
  • Loading branch information
renchap authored Oct 21, 2024
1 parent 1fc165d commit fb6d7ed
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions app/javascript/entrypoints/public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -327,31 +327,24 @@ Rails.delegate(document, '.input-copy button', 'click', ({ target }) => {

if (!input) return;

const oldReadOnly = input.readOnly;

input.readOnly = false;
input.focus();
input.select();
input.setSelectionRange(0, input.value.length);

try {
if (document.execCommand('copy')) {
input.blur();

navigator.clipboard
.writeText(input.value)
.then(() => {
const parent = target.parentElement;

if (!parent) return;
parent.classList.add('copied');
if (parent) {
parent.classList.add('copied');

setTimeout(() => {
parent.classList.remove('copied');
}, 700);
}
} catch (err) {
console.error(err);
}
setTimeout(() => {
parent.classList.remove('copied');
}, 700);
}

input.readOnly = oldReadOnly;
return true;
})
.catch((error: unknown) => {
console.error(error);
});
});

const toggleSidebar = () => {
Expand Down

0 comments on commit fb6d7ed

Please sign in to comment.