@@ -77,11 +77,18 @@ export default class HTMLCodeBlockElement extends HTMLElement {
77
77
/** Click event handler of copy button */
78
78
#onClickButton = ( ( ) => {
79
79
let key = - 1 ;
80
+ const textarea = document . createElement ( 'textarea' ) ;
80
81
81
82
return ( ) => {
82
83
clearTimeout ( key ) ;
83
84
85
+ textarea . value = this . #value. replace ( / \n $ / , '' ) ;
86
+ document . body . append ( textarea ) ;
87
+ textarea . select ( ) ;
88
+ document . execCommand ( 'copy' ) ;
89
+ textarea . remove ( ) ;
84
90
this . #copyButton. textContent = 'Copied!' ;
91
+
85
92
key = window . setTimeout ( ( ) => {
86
93
this . #copyButton. textContent = 'Copy' ;
87
94
} , 1500 ) ;
@@ -238,6 +245,7 @@ export default class HTMLCodeBlockElement extends HTMLElement {
238
245
button . type = 'button' ;
239
246
button . slot = 'copy-button' ;
240
247
button . textContent = 'Copy' ;
248
+ button . setAttribute ( 'aria-live' , 'polite' ) ;
241
249
button . addEventListener ( 'click' , this . #onClickButton) ;
242
250
243
251
return button ;
@@ -296,7 +304,7 @@ export default class HTMLCodeBlockElement extends HTMLElement {
296
304
/* -------------------------------------------------------------------------
297
305
* Hard private props initialize
298
306
* ---------------------------------------------------------------------- */
299
- this . #value = ( this . textContent || '' ) . replace ( / ^ \n / , '' ) . replace ( / \n $ / , '' ) ;
307
+ this . #value = ( this . textContent || '' ) . replace ( / ^ \n / , '' ) . replace ( / \n \n $ / , '' ) ;
300
308
this . #label = a11yName . textContent || '' ;
301
309
this . #language = this . getAttribute ( 'language' ) || '' ;
302
310
this . #controls = this . getAttribute ( 'controls' ) !== null ;
0 commit comments