Adds native share button
Replaces the existing share buttons with a native share button implemented using the Web Share API. This simplifies the code, removes dependency on external libraries, and provides a more consistent user experience across different platforms. The old share buttons are removed from the config file.
This commit is contained in:
29
static/js/share-button.js
Normal file
29
static/js/share-button.js
Normal file
@@ -0,0 +1,29 @@
|
||||
class ShareButton extends HTMLElement {
|
||||
static register(tagName) {
|
||||
if ("customElements" in window && window.navigator.share) {
|
||||
customElements.define(tagName || "share-button", ShareButton);
|
||||
}
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.button.addEventListener("click", this.share);
|
||||
}
|
||||
|
||||
get button() {
|
||||
return this.querySelector("button");
|
||||
}
|
||||
|
||||
share = () => {
|
||||
const root = this.getRootNode();
|
||||
window.navigator
|
||||
.share({
|
||||
title: root.title,
|
||||
text: root.title,
|
||||
url: window.location.href,
|
||||
})
|
||||
.then(() => console.log("Page was succesffuly shared"))
|
||||
.catch((error) => console.log(error));
|
||||
};
|
||||
}
|
||||
|
||||
ShareButton.register();
|
||||
Reference in New Issue
Block a user