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

              
                <h1>Native Sharing Web Component</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>The markup is generated without Shadow DOM and can be styled from outside the component.</p>
<p>See also: <a href="https://codepen.io/schneyra/pen/xxPxvow">Native Sharing Web Component with is-Attribute and Polyfill</a> and <a href="https://codepen.io/schneyra/pen/qBVWpBB">Native Sharing Web Component with Shadow DOM</a>.</p>
<p>Thanks to <a href="https://twitter.com/mariovhamann/status/1486683160893083654">Mario</a> who pointed me to this article <a href="https://discourse.world/h/2020/02/07/Web-components-without-Shadow-DOM">Web components without Shadow DOM</a>.
<p>I also wrote an <a href="https://martinschneider.me/articles/tinkering-with-web-components/">article on my blog</a>.</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>
<msme-sharing-button data-buttontext="share this page" data-sharing-url="https://martinschneider.me" data-buttonclass="button" data-sharing-title="martinschneider.me" data-sharing-text="Martins Website"></msme-sharing-button>
              
            
!

CSS

              
                .button {
  border: 1px solid black;
  color: black;
  background-color: white;
  border-radius: 1rem;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  display: flex;
  align-items: center;

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

  svg {
    width: 1em;
    height: 1em;
    margin-right: 0.5em;
  }
}

              
            
!

JS

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

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

    if (!this.getAttribute("data-sharing-url")) {
      console.error("msme-sharing-button: no url provided.");
      return;
    }

    // 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;
    let icon = null;
    if (isAndroid) {
      icon = `<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>`;
    } else {
      icon = `<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>`;
    }

    let text = this.getAttribute("data-buttontext") ?? "share";
    let cssClass = this.getAttribute("data-buttonclass")
      ? `class="${this.getAttribute("data-buttonclass")}"`
      : "";

    let template = document.createElement("template");
    template.innerHTML = `<button type="button" ${cssClass}>${icon} ${text}</button>`;

    this._contents = new DocumentFragment();
    this._contents.appendChild(template.content.cloneNode(true));

    // and here's the sharing-action
    let button = this._contents.querySelector("button");
    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)
        );
    });

    this.appendChild(this._contents);
  }
}

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

              
            
!
999px

Console