Use copy email to clipboard as an action

Returns an event with the email to detect for value and timeouts
This commit is contained in:
2022-09-14 13:28:58 +02:00
parent f38a8fcdc7
commit 07060c8661
3 changed files with 40 additions and 23 deletions

View File

@@ -199,4 +199,34 @@ export const smoothScroll = async ({ hash, callback, changeHash = true, event }:
if (changeHash) location.hash = hash
callback && callback()
})
}
/**
* Copy mailto links to clipboard and show message
*/
export const mailtoClipboard = (node: HTMLElement) => {
const links = node.querySelectorAll('a[href^="mailto:"]')
const clickToCopy = (event) => {
let emailAddress = event.currentTarget.href.split('mailto:')[1].split('?')[0]
// Copy email to clipboard
navigator.clipboard.writeText(emailAddress)
// Send event
node.dispatchEvent(new CustomEvent('copied', {
detail: { email: emailAddress }
}))
event.preventDefault()
}
links && links.forEach(link => link.addEventListener('click', clickToCopy, true))
return {
destroy () {
links && links.forEach(link => link.removeEventListener('click', clickToCopy, true))
}
}
}