Skip to content

Commit 2620216

Browse files
✨ Copy button is available
1 parent 7e3994d commit 2620216

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/class/HTMLCodeBlockElement.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,18 @@ export default class HTMLCodeBlockElement extends HTMLElement {
7777
/** Click event handler of copy button */
7878
#onClickButton = (() => {
7979
let key = -1;
80+
const textarea = document.createElement('textarea');
8081

8182
return () => {
8283
clearTimeout(key);
8384

85+
textarea.value = this.#value.replace(/\n$/, '');
86+
document.body.append(textarea);
87+
textarea.select();
88+
document.execCommand('copy');
89+
textarea.remove();
8490
this.#copyButton.textContent = 'Copied!';
91+
8592
key = window.setTimeout(() => {
8693
this.#copyButton.textContent = 'Copy';
8794
}, 1500);
@@ -238,6 +245,7 @@ export default class HTMLCodeBlockElement extends HTMLElement {
238245
button.type = 'button';
239246
button.slot = 'copy-button';
240247
button.textContent = 'Copy';
248+
button.setAttribute('aria-live', 'polite');
241249
button.addEventListener('click', this.#onClickButton);
242250

243251
return button;
@@ -296,7 +304,7 @@ export default class HTMLCodeBlockElement extends HTMLElement {
296304
/* -------------------------------------------------------------------------
297305
* Hard private props initialize
298306
* ---------------------------------------------------------------------- */
299-
this.#value = (this.textContent || '').replace(/^\n/, '').replace(/\n$/, '');
307+
this.#value = (this.textContent || '').replace(/^\n/, '').replace(/\n\n$/, '');
300308
this.#label = a11yName.textContent || '';
301309
this.#language = this.getAttribute('language') || '';
302310
this.#controls = this.getAttribute('controls') !== null;

0 commit comments

Comments
 (0)