Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="//unpkg.com/@ungap/custom-elements"></script>

<h1>Native Sharing Web Component with is-Attribute and Polyfill</h1>
<p>This web component uses the <a href="https://web.dev/web-share/">Web Share API</a> to share a link. The sharing code was originally written for <a href="https://martinschneider.me/articles/creating-a-sharing-button-with-the-web-share-api/">my personal website</a>.</p>
<hr>
<p>In this solution the given CSS is applied to the custom button and it's elements. Unfortunately the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/is"><code>is</code>-Attribute</a> is not supported in Safari. <a href="https://github.com/ungap/custom-elements">This custom elements polyfill</a> can help us out.</p>
<hr>
<p>Please note: This works currently only in Safari (macOS and iOS) and Chrome on Windows and Android. Other browsers won't show a button beneath.</p>
<button type="button" is="msme-sharing-button" data-text="share" data-sharing-url="https://martinschneider.me" data-sharing-title="martinschneider.me" data-sharing-text="Martins Website" hidden></button>
              
            
!

CSS

              
                button {
  border: 1px solid black;
  color: black;
  background-color: white;
  border-radius: 0.25rem;
  padding: 0.5rem 0.75rem;

  &:hover {
    background-color: black;
    color: white;
  }
}

              
            
!

JS

              
                class sharingButton extends HTMLButtonElement {
  constructor() {
    super();

    if (!navigator.share) {
      console.log("msme-sharing-button: native sharing not supported.");
      return;
    }

    const androidIcon = `<svg aria-hidden="true" focusable="false" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><line x1="8.59" y1="13.51" x2="15.42" y2="17.49"/><line x1="15.41" y1="6.51" x2="8.59" y2="10.49"/></svg>`;
    const iOsIcon = `<svg aria-hidden="true" focusable="false" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8"/><polyline points="16 6 12 2 8 6"/><line x1="12" y1="2" x2="12" y2="15"/></svg>`;

    // Android uses a different icon for sharing buttons
    // from devices in Apples ecosystem
    // Thanks to: https://davidwalsh.name/detect-android
    const ua = navigator.userAgent.toLowerCase();
    const isAndroid = ua.indexOf("android") > -1;
    if (isAndroid) {
      this.innerHTML = androidIcon;
    } else {
      this.innerHTML = iOsIcon;
    }

    // the button gets some customizable text
    const text = document.createElement("span");
    text.textContent = this.getAttribute("data-text");
    this.appendChild(text);

    // and here's the sharing-action
    this.addEventListener("click", () => {
      const shareData = {
        title: this.getAttribute("data-sharing-title"),
        text: this.getAttribute("data-sharing-text"),
        url: this.getAttribute("data-sharing-url")
      };

      navigator
        .share(shareData)
        .then(() => console.log("msme-sharing-button: sharing successful"))
        .catch((error) =>
          console.log("msme-sharing-button: error while sharing", error)
        );
    });

    const style = document.createElement("style");

    style.textContent = `
      button {
        font-size: inherit;
        display: flex;
        align-items: center;
      }
      
      svg {
        height: 0.9em;
        width: 0.9em;
        margin-right: 0.5em;
      }
   
     `;

    this.appendChild(style);
    this.removeAttribute("hidden");
  }
}

customElements.define("msme-sharing-button", sharingButton, {
  extends: "button"
});

              
            
!
999px

Console