Share

<div class="share">
    <div class="share__inner">
        <div class="share__buttons"><button class="share__button" data-share-action="twitter" type="button"><svg class="icon icon--twitter" viewBox="0 0 200 200" role="presentation">
                    <use xlink:href="#icon-twitter"></use>
                </svg></button><button class="share__button" data-share-action="linkedin" type="button"><svg class="icon icon--linkedin" viewBox="0 0 200 200" role="presentation">
                    <use xlink:href="#icon-linkedin"></use>
                </svg></button></div>
    </div>
</div>
#{tag || 'div'}.share()&attributes(attr)
  .share__inner
    .share__buttons
      for service in services
        button.share__button(
          data-share-action=service.share,
          type='button'
        )
          +include('@icon', { icon: service.icon })
{
  "services": [
    {
      "icon": "twitter",
      "share": "twitter"
    },
    {
      "icon": "linkedin",
      "share": "linkedin"
    }
  ]
}
  • Content:
    const copyLink = (trigger) => {
      const id = 'copy-to-clipboard-hidden-textarea';
      let existsTextarea = document.getElementById(id);
    
      if (!existsTextarea) {
        const textarea = document.createElement('textarea');
        textarea.id = id;
    
        // Place in top-left corner of screen regardless of scroll position.
        textarea.style.position = 'fixed';
        textarea.style.top = 0;
        textarea.style.left = 0;
    
        // Ensure it has a small width and height. Setting to 1px / 1em
        // doesn't work as this gives a negative w/h on some browsers.
        textarea.style.width = '1px';
        textarea.style.height = '1px';
    
        // We don't need padding, reducing the size if it does flash render.
        textarea.style.padding = 0;
    
        // Clean up any borders.
        textarea.style.border = 'none';
        textarea.style.outline = 'none';
        textarea.style.boxShadow = 'none';
    
        // Avoid flash of white box if rendered for any reason.
        textarea.style.background = 'transparent';
        document.querySelector('body').appendChild(textarea);
        existsTextarea = document.getElementById(id);
      }
    
      existsTextarea.value = document.location.href;
      existsTextarea.select();
    
      const triggerParent = trigger && trigger.closest('.modal-share__item');
    
      try {
        const status = document.execCommand('copy');
        if (!status) {
          throw status;
        } else {
          window.console.log('[Share] The url is now on the clipboard');
          if (trigger) {
            trigger.removeAttribute('data-share-error');
            trigger.setAttribute('data-share-success', true);
            const text = trigger.getAttribute('data-share-success-text');
            if (text) {
              // eslint-disable-next-line no-param-reassign
              trigger.querySelector('.modal-share__item__text').innerText = text;
            }
          }
          if (triggerParent) {
            triggerParent.removeAttribute('data-share-error');
            triggerParent.setAttribute('data-share-success', true);
          }
        }
      } catch (err) {
        window.console.error('[Share] Unable to copy url to clipboard');
        if (trigger) {
          trigger.removeAttribute('data-share-success');
          trigger.setAttribute('data-share-error', true);
          const text = trigger.getAttribute('data-share-error-text');
          if (text) {
            // eslint-disable-next-line no-param-reassign
            trigger.querySelector('.modal-share__item__text').innerText = text;
          }
        }
        if (triggerParent) {
          triggerParent.removeAttribute('data-share-success');
          triggerParent.setAttribute('data-share-error', true);
        }
      }
    };
    
    const encodedURL = () => encodeURIComponent(document.location.href);
    
    const shareWithFacebook = () => {
      window.open(`https://www.facebook.com/sharer/sharer.php?u=${encodedURL()}`, '_blank');
    };
    
    const shareWithTwitter = () => {
      window.open(`https://twitter.com/intent/tweet?text=&url=${encodedURL()}`, '_blank');
    };
    
    const shareWithXing = () => {
      window.open(`https://www.xing-share.com/app/user?op=share;sc_p=xing-share;url=${encodedURL()}`, '_blank');
    };
    
    const shareWithLinkedIn = () => {
      window.open(`https://www.linkedin.com/shareArticle?mini=true&url=${encodedURL()}`, '_blank');
    };
    
    const shareWithWeChat = () => {
      // ...
    };
    
    const shareWithWhatsApp = () => {
      window.open(`whatsapp://send?text=${encodedURL()}`, '_self');
    };
    
    const shareWithMessenger = () => {
      window.open(`fb-messenger://share?link=${encodedURL()}&app_id=`, '_self');
    };
    
    const shareWithEmail = () => {
      window.open(`mailto:?body=${encodedURL()}`, '_self');
    };
    
    document.addEventListener('click', (event) => {
      const trigger = event.target.closest('[data-share-action]');
      if (!trigger) {
        return;
      }
      event.preventDefault();
      const action = trigger.getAttribute('data-share-action');
    
      switch (action) {
        case 'copy':
          copyLink(trigger);
          break;
        case 'facebook':
          shareWithFacebook();
          break;
        case 'twitter':
          shareWithTwitter();
          break;
        case 'xing':
          shareWithXing();
          break;
        case 'linkedin':
          shareWithLinkedIn();
          break;
        case 'wechat':
          shareWithWeChat();
          break;
        case 'whatsapp':
          shareWithWhatsApp();
          break;
        case 'email':
          shareWithEmail();
          break;
        case 'messenger':
          shareWithMessenger();
          break;
        default:
          break;
      }
    });
    
  • URL: /components/raw/share/share.js
  • Filesystem Path: src/components/molecules/share/share.js
  • Size: 4.3 KB
  • Content:
    .share {
      color: $color-dark;
      display: inline-block;
    }
    
    .share__inner {
      display: inline-flex;
      flex-direction: row;
      justify-content: center;
    }
    
    .share__button {
      border: 1px solid currentColor;
      border-radius: 50%;
      color: $color-dark;
      display: inline-block;
      height: 4rem;
      position: relative;
      transition: color 0.2s;
      width: 4rem;
    
      &:not(:last-child) {
        margin-right: 1rem;
      }
    
      @media(hover: hover) and (pointer: fine) {
        &:hover {
          color: $color-blue;
        }
      }
    
      .icon {
        height: 1.8rem;
        left: 50%;
        position: absolute;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 1.8rem;
      }
    }
    
  • URL: /components/raw/share/share.scss
  • Filesystem Path: src/components/molecules/share/share.scss
  • Size: 646 Bytes

There are no notes for this item.