Skip to content

Commit 7e3994d

Browse files
⚡ Add Event
1 parent b485d19 commit 7e3994d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/class/HTMLCodeBlockElement.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ export default class HTMLCodeBlockElement extends HTMLElement {
7474
#language: string = '';
7575
/** Actual value of the accessor `controls` */
7676
#controls: boolean = false;
77+
/** Click event handler of copy button */
78+
#onClickButton = (() => {
79+
let key = -1;
80+
81+
return () => {
82+
clearTimeout(key);
83+
84+
this.#copyButton.textContent = 'Copied!';
85+
key = window.setTimeout(() => {
86+
this.#copyButton.textContent = 'Copy';
87+
}, 1500);
88+
};
89+
})();
7790
/** Outputs the resulting syntax-highlighted markup to the DOM. */
7891
#render = function (this: HTMLCodeBlockElement) {
7992
if (!this.parentNode) {
@@ -156,7 +169,6 @@ export default class HTMLCodeBlockElement extends HTMLElement {
156169
}
157170

158171
set controls(value: boolean) {
159-
// TODO: コピーボタンの表示切り替え
160172
this.#controls = value;
161173

162174
if (this.#controls) {
@@ -223,8 +235,10 @@ export default class HTMLCodeBlockElement extends HTMLElement {
223235
const copyButton = (() => {
224236
const button = document.createElement('button');
225237

238+
button.type = 'button';
226239
button.slot = 'copy-button';
227240
button.textContent = 'Copy';
241+
button.addEventListener('click', this.#onClickButton);
228242

229243
return button;
230244
})();

0 commit comments

Comments
 (0)