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:
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user