A polyfill for button type=”share”
Tags: polyfill, code, gist, codepen
Published: 2020-10-09 16:19:09

---
title: A polyfill for button type=”share”
published: true
date: 2020-09-16 15:36:55 UTC
tags: polyfill,code,gist,codepen
canonical_url: https://adactio.com/journal/17405
---

After writing about [a declarative Web Share API](https://adactio.com/journal/17398) here yesterday I thought I’d better share the idea (see what I did there?).

[I opened an issue](https://github.com/w3c/web-share/issues/176) on the Github repo for the spec.

(I hope that’s the right place for this proposal. I know that in the past ideas were kicked around on [the Discourse site for Web platform Incubator Community Group](https://discourse.wicg.io/) but I can’t stand Discourse. It literally requires JavaScript to render anything to the screen even though the entire content is text. If it turns out that that is the place I should’ve posted, I guess I’ll hold my nose and do it using the most over-engineered reinvention of the browser I’ve ever seen. But I believe that the plan is for [WICG](https://www.w3.org/community/wicg/) to migrate proposals to Github anyway.)

I also realised that, as the JavaScript Web Share API already exists, I can use it to polyfill my suggestion for:

```
<button type="share">

```

The polyfill also demonstrates how feature detection could work. [Here’s the code](https://gist.github.com/adactio/092b11a74eded2701335ba27f94d2484).

This polyfill takes an Inception approach to feature detection. There are three nested levels:

1. This browser supports `button type="share"`. Great! Don’t do anything. Otherwise proceed to level two.
2. This browser supports the JavaScript Web Share API. Use that API to share the current page URL and title. Otherwise proceed to level three.
3. Use a `mailto:` link to prefill an email with the page title as the subject and the URL in the body. Ya basic!

The idea is that, as long as you include the 20 lines of polyfill code, you could start using `button type="share"` in your pages today.

I’ve made [a test page on Codepen](https://codepen.io/adactio/pen/rNerbPE). I’m just using plain text in the button but you could use a nice image or SVG or combination. You can use [the Codepen test page](https://codepen.io/adactio/pen/rNerbPE) to observe two of the three possible behaviours browsers could exhibit:

1. A browser supports `button type="share"`. Currently that’s none because I literally made this shit up yesterday.
2. A browser supports the JavaScript Web Share API. This is Safari on Mac, Edge on Windows, Safari on iOS, and Chrome, Samsung Internet, and Firefox on Android.
3. A browser supports neither `button type="share"` nor the existing JavaScript Web Share API. This is Firefox and Chrome on desktop (and Edge if you’re on a Mac).

See the Pen [Polyfill for button type=”share"](https://codepen.io/adactio/pen/rNerbPE) by Jeremy Keith ([@adactio](https://codepen.io/adactio)) on [CodePen](https://codepen.io).

<script async src="https://static.codepen.io/assets/embed/ei.js"></script>

The polyfill doesn’t support Internet Explorer 11 or lower because it uses the DOM `closest()` method. Feel free to fork and rewrite if you need to support old IE.

<script async src="https://gist.github.com/adactio/092b11a74eded2701335ba27f94d2484.js"></script>