Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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

              
                <h1>Native Sharing Web Component with Shadow DOM</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>
<p>Because the button is added via the Shadow DOM, the given CSS does not apply to it. Here is an alternative <a href="https://codepen.io/schneyra/pen/xxPxvow">solution that solves this problem</a>.</p>
<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>

<msme-sharing-button data-text="share" data-sharing-url="https://martinschneider.me" data-sharing-title="martinschneider.me" data-sharing-text="Martins Website" />
              
            
!

CSS

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

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

              
            
!

JS

              
                class sharingButton extends HTMLElement {
  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>`;

    this.attachShadow({ mode: "open" });

    // the button itself
    const button = document.createElement("button");
    button.setAttribute("type", "button");
    button.setAttribute("class", this.getAttribute("class"));

    // 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) {
      button.innerHTML = androidIcon;
    } else {
      button.innerHTML = iOsIcon;
    }

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

    // and here's the sharing-action
    button.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.25em;
      }
   
     `;

    button.appendChild(style);
    this.shadowRoot.append(button);
  }
}

customElements.define("msme-sharing-button", sharingButton);

              
            
!
999px

Console