@@ -47,6 +47,7 @@ class HTMLCodeBlockElement extends HTMLElement {
4747 } ;
4848 } ) ( ) ;
4949 #a11yName;
50+ #copyButton;
5051 #codeBlock;
5152 #codeWrap;
5253 /** Actual value of the accessor `value` */
@@ -70,9 +71,11 @@ class HTMLCodeBlockElement extends HTMLElement {
7071 this . textContent = '' ;
7172 this . #a11yName. textContent = this . #label;
7273 this . #slots. name . hidden = ! this . #label;
74+ this . #slots. copyButton . hidden = ! this . #controls;
7375 this . #codeBlock. textContent = '' ;
7476 this . #codeBlock. insertAdjacentHTML ( 'afterbegin' , markup ) ;
7577 this . append ( this . #a11yName) ;
78+ this . append ( this . #copyButton) ;
7679 this . append ( this . #codeWrap) ;
7780 } ;
7881 /** @returns - Syntax Highlighted Source Code */
@@ -167,26 +170,21 @@ class HTMLCodeBlockElement extends HTMLElement {
167170 constructor ( ) {
168171 super ( ) ;
169172 /* -------------------------------------------------------------------------
170- * Setup Shadow DOM contents
173+ * Setup DOM contents
171174 * ---------------------------------------------------------------------- */
172- /**
173- * The container of minimum text that will be read even
174- * if the accessible name (label attribute value) is omitted.
175- */
176- const a11yNamePrefix = ( ( ) => {
177- const span = document . createElement ( 'span' ) ;
178- span . id = 'semantics' ;
179- span . hidden = true ;
180- span . textContent = 'Code Block' ;
181- return span ;
182- } ) ( ) ;
183175 /** Container of accessible names (label attribute values). */
184176 const a11yName = ( ( ) => {
185177 const span = document . createElement ( 'span' ) ;
186178 span . slot = 'name' ;
187179 span . textContent = this . getAttribute ( 'label' ) || '' ;
188180 return span ;
189181 } ) ( ) ;
182+ const copyButton = ( ( ) => {
183+ const button = document . createElement ( 'button' ) ;
184+ button . slot = 'copy-button' ;
185+ button . textContent = 'Copy' ;
186+ return button ;
187+ } ) ( ) ;
190188 const codeElm = ( ( ) => {
191189 const code = document . createElement ( 'code' ) ;
192190 code . tabIndex = 0 ;
@@ -199,6 +197,20 @@ class HTMLCodeBlockElement extends HTMLElement {
199197 pre . append ( codeElm ) ;
200198 return pre ;
201199 } ) ( ) ;
200+ /* -------------------------------------------------------------------------
201+ * Setup Shadow DOM contents
202+ * ---------------------------------------------------------------------- */
203+ /**
204+ * The container of minimum text that will be read even
205+ * if the accessible name (label attribute value) is omitted.
206+ */
207+ const a11yNamePrefix = ( ( ) => {
208+ const span = document . createElement ( 'span' ) ;
209+ span . id = 'semantics' ;
210+ span . hidden = true ;
211+ span . textContent = 'Code Block' ;
212+ return span ;
213+ } ) ( ) ;
202214 const container = ( ( ) => {
203215 const div = document . createElement ( 'div' ) ;
204216 div . append ( ...Object . values ( this . #slots) ) ;
@@ -219,6 +231,7 @@ class HTMLCodeBlockElement extends HTMLElement {
219231 this . #language = this . getAttribute ( 'language' ) || '' ;
220232 this . #controls = this . getAttribute ( 'controls' ) !== null ;
221233 this . #a11yName = a11yName ;
234+ this . #copyButton = copyButton ;
222235 this . #codeBlock = codeElm ;
223236 this . #codeWrap = preElm ;
224237 }
0 commit comments