Usar el cajón de compartir nativo si está disponible

This commit is contained in:
f 2021-09-17 20:28:39 -03:00
parent 5be70267cb
commit ea01cb0602
2 changed files with 41 additions and 1 deletions

View file

@ -1,4 +1,17 @@
<input type="checkbox" id="share" class="toggler" autocomplete="off" /> {%- assign title = include.title | escape -%}
{%- assign text = include.description | strip_html | escape -%}
{%- assign url = site.url | append: include.url | escape -%}
<input
data-controller="share"
data-action="share#share"
data-share-title-value="{{ title }}"
data-share-text-value="{{ text }}"
data-share-url-value="{{ url }}"
type="checkbox"
id="share"
class="toggler"
autocomplete="off" />
<label class="share btn border btn-block" for="share"> <label class="share btn border btn-block" for="share">
{{ site.i18n.share.text }} {{ site.i18n.share.text }}

View file

@ -0,0 +1,27 @@
import { Controller } from 'stimulus'
export default class extends Controller {
static values = {
title: String,
text: String,
url: String
}
async share (event = undefined) {
event?.preventDefault()
event?.stopPropagation()
const title = this.titleValue
const text = this.textValue
const url = this.urlValue
const data = { title, text, url }
if ('share' in navigator) {
if (navigator.canShare(data)) {
navigator.share(data)
} else {
console.error('No se puede compartir', data)
}
}
}
}