-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathvcard-element.ts
More file actions
29 lines (23 loc) · 805 Bytes
/
vcard-element.ts
File metadata and controls
29 lines (23 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { downloadVcard } from '../lib/vcard'
class VcardDownloadElement extends HTMLElement {
private anchor: HTMLAnchorElement | null = null
connectedCallback(): void {
const candidate = this.querySelector('a')
if (!candidate) return
this.anchor = candidate
this.anchor.addEventListener('click', this.handleClick, { passive: false })
}
disconnectedCallback(): void {
this.anchor?.removeEventListener('click', this.handleClick)
this.anchor = null
}
private handleClick = (event: MouseEvent): void => {
event.preventDefault()
downloadVcard().catch((error: unknown) => {
console.error('Failed to create vCard download', error)
})
}
}
if (!customElements.get('vcard-download')) {
customElements.define('vcard-download', VcardDownloadElement)
}