Skip to content

Commit b485d19

Browse files
⚙️ Build
1 parent 63c7b55 commit b485d19

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

dist/class/HTMLCodeBlockElement.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

dist/utils/add-style.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ style.textContent = `
1515
position: absolute;
1616
top: 0;
1717
left: 0;
18+
z-index: 0;
1819
padding: 0 5px;
1920
max-width: 90%;
2021
color: #fff;
@@ -25,6 +26,25 @@ style.textContent = `
2526
background: #75758a;
2627
box-sizing: border-box;
2728
}
29+
30+
code-block button {
31+
all: unset;
32+
outline: revert;
33+
position: absolute;
34+
right: 10px;
35+
top: 10px;
36+
z-index: 1;
37+
display: block;
38+
color: #fff;
39+
opacity: 0;
40+
mix-blend-mode: exclusion;
41+
}
42+
43+
code-block:hover button,
44+
code-block:not(:hover) button:focus {
45+
opacity: 1;
46+
}
47+
2848
code-block pre,
2949
code-block code {
3050
font-family: inherit;

lib/html-code-block-element.all.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/html-code-block-element.common.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/html-code-block-element.core.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)