-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Progressively enhance leave calendar link clipboard copy
- Loading branch information
1 parent
1cc83c9
commit f5d341c
Showing
3 changed files
with
26 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,31 @@ | ||
// @ts-check | ||
|
||
export default class extends HTMLButtonElement { | ||
/** @type {HTMLInputElement} */ | ||
#sourceEl; | ||
|
||
export default class extends HTMLElement { | ||
connectedCallback() { | ||
const sourceSelector = this.dataset.clipboardButtonSource; | ||
const selector = this.dataset.source; | ||
|
||
if (!sourceSelector) { | ||
throw new Error('data-clipboard-button-source is missing'); | ||
if (!selector) { | ||
return; | ||
} | ||
|
||
const sourceEl = /** @type {HTMLInputElement|null} */ (document.querySelector( | ||
sourceSelector | ||
const input = /** @type {HTMLInputElement|null} */ (document.querySelector( | ||
selector | ||
)); | ||
|
||
if (!sourceEl) { | ||
throw new Error(`element '${sourceSelector}' was not found`); | ||
if (!input) { | ||
throw new Error(`input at '${selector}' was not found`); | ||
} | ||
|
||
this.#sourceEl = sourceEl; | ||
|
||
this.addEventListener('click', this.#handleClick); | ||
} | ||
const template = /** @type {HTMLTemplateElement} */ (this.querySelector( | ||
'template' | ||
)); | ||
this.appendChild(document.importNode(template.content, true)); | ||
|
||
#handleClick = () => { | ||
this.#sourceEl.select(); // Visual feedback | ||
navigator.clipboard.writeText(this.#sourceEl.value); | ||
}; | ||
const btn = /** @type {HTMLButtonElement} */ (this.querySelector('button')); | ||
|
||
disconnectedCallback() { | ||
this.removeEventListener('click', this.#handleClick); | ||
btn.addEventListener('click', () => { | ||
input.select(); // Visual feedback | ||
navigator.clipboard.writeText(input.value); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters