sutty-base-jekyll-theme/_packs/controllers/share_controller.js

28 lines
586 B
JavaScript
Raw Normal View History

2021-11-22 17:51:50 +00:00
import { Controller } from "stimulus";
export default class extends Controller {
static values = {
title: String,
text: String,
2021-11-22 17:51:50 +00:00
url: String,
};
2021-11-22 17:51:50 +00:00
async share(event = undefined) {
event?.preventDefault();
event?.stopPropagation();
2021-11-22 17:51:50 +00:00
const title = this.titleValue;
const text = this.textValue;
const url = this.urlValue;
const data = { title, text, url };
2021-11-22 17:51:50 +00:00
if ("share" in navigator) {
if (navigator.canShare(data)) {
2021-11-22 17:51:50 +00:00
navigator.share(data);
} else {
2021-11-22 17:51:50 +00:00
console.error("No se puede compartir", data);
}
}
}
}