Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Web Share API button #67

Merged
merged 3 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ <h2><code>&lt;a&gt;</code></h2>
<a class="vkontakte"></a>
<a class="whatsapp"></a>
<a class="copy-url"></a>
<a class="web-share"></a>
</div>
</section>
<section>
Expand All @@ -64,6 +65,7 @@ <h2><code>&lt;button&gt;</code></h2>
<button class="vkontakte"></button>
<button class="whatsapp"></button>
<button class="copy-url"></button>
<button class="web-share"></button>
</div>
</section>
<section>
Expand All @@ -89,27 +91,30 @@ <h2><code>&lt;a&gt;</code> with custom params</h2>
<a class="vkontakte"></a>
<a class="whatsapp"></a>
<a class="copy-url"></a>
<a class="web-share"></a>
</div>
</section>
<section>
<h2>Specimen</h2>
<div class="shareon specimen">
<a class="facebook">Share</a>
<a class="facebook"></a>
<a class="linkedin"></a>
<a class="mastodon">Toot</a>
<a class="messenger" data-fb-app-id="3619024578167617"></a>
<a class="odnoklassniki"></a>
<a class="pinterest">Pin</a>
<a class="pocket">Save</a>
<a class="reddit"></a>
<br />
<a class="pocket"></a>
<a class="reddit"></a>
<a class="teams"></a>
<a class="telegram"></a>
<a class="twitter">Tweet</a>
<a class="viber"></a>
<a class="vkontakte">Отправить</a>
<br />
<a class="vkontakte">Поделиться</a>
<a class="whatsapp"></a>
<a class="copy-url">Copy URL</a>
<a class="copy-url"></a>
<a class="web-share">Share</a>
</div>
</section>
</main>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"path": "./dist/shareon.min.css"
},
{
"limit": "1 KB",
"limit": "1069 B",
"path": "./dist/shareon.es.js"
}
],
Expand Down
4 changes: 4 additions & 0 deletions src/shareon.css
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@
background-image: url("data:image/svg+xml,%3Csvg fill='%23fff' viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M4.199 4.841H.11c.194 9.312 4.85 14.907 13.012 14.907h.462v-5.327c3 .299 5.268 2.492 6.178 5.327H24c-1.164-4.237-4.223-6.58-6.133-7.475 1.91-1.105 4.596-3.79 5.238-7.432h-3.85c-.836 2.955-3.313 5.641-5.67 5.895V4.84h-3.85v10.326C7.347 14.57 4.333 11.675 4.199 4.84Z'/%3E%3C/svg%3E");
}

.shareon > .web-share:before {
background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' stroke='%23fff' stroke-width='2'%3E%3Ccircle cx='18' cy='5' r='3'/%3E%3Ccircle cx='6' cy='12' r='3'/%3E%3Ccircle cx='18' cy='19' r='3'/%3E%3Cpath d='m8.59 13.51l6.83 3.98m-.01-10.98l-6.82 3.98'/%3E%3C/g%3E%3C/svg%3E");
}

.shareon > .whatsapp {
background-color: #25d366;
}
Expand Down
23 changes: 23 additions & 0 deletions src/shareon.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ const init = () => {
});
}

// if it's "Web Share"
if (cls === "web-share") {
const data = {
title:
child.dataset.title ||
container.dataset.title ||
document.title,
text: child.dataset.text || container.dataset.text || "",
url:
child.dataset.url ||
container.dataset.url ||
window.location.href,
};

if (navigator.canShare && navigator.canShare(data)) {
child.addEventListener("click", () => {
navigator.share(data);
});
} else {
child.style.display = "none";
}
}

// if it's one of the networks
if (Object.prototype.hasOwnProperty.call(urlBuilderMap, cls)) {
const preset = {
Expand Down