mapcomplete/UI/BigComponents/ShareButton.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import BaseUIElement from "../BaseUIElement"
2020-11-22 03:16:56 +01:00
export default class ShareButton extends BaseUIElement {
2022-09-08 21:40:48 +02:00
private _embedded: BaseUIElement
private _shareData: () => { text: string; title: string; url: string }
2022-09-08 21:40:48 +02:00
constructor(
embedded: BaseUIElement,
generateShareData: () => {
text: string
title: string
url: string
}
) {
super()
this._embedded = embedded
this._shareData = generateShareData
2021-06-12 02:58:32 +02:00
this.SetClass("share-button")
2020-11-22 03:16:56 +01:00
}
2021-06-12 02:58:32 +02:00
protected InnerConstructElement(): HTMLElement {
const e = document.createElement("button")
e.type = "button"
e.appendChild(this._embedded.ConstructElement())
2022-09-08 21:40:48 +02:00
e.addEventListener("click", () => {
2020-11-22 03:16:56 +01:00
if (navigator.share) {
2022-09-08 21:40:48 +02:00
navigator
.share(this._shareData())
.then(() => {
console.log("Thanks for sharing!")
})
.catch((err) => {
console.log(`Couldn't share because of`, err.message)
})
2020-11-22 03:16:56 +01:00
} else {
2022-09-08 21:40:48 +02:00
console.log("web share not supported")
2020-11-22 03:16:56 +01:00
}
2022-09-08 21:40:48 +02:00
})
2022-09-08 21:40:48 +02:00
return e
2020-11-22 03:16:56 +01:00
}
2022-09-08 21:40:48 +02:00
}