<div class="share">
<div class="share__fallback" data-behavior="share-fallback">
<p>Share:</p>
<ul class="share__options">
<li>
<button data-behavior="copy-url" aria-label="Copy page link"><img src="/assets/images/share-copy-icon.svg" />
<span class="tooltip">Copy page link</span>
</button>
</li>
<li><a href="https://www.facebook.com/sharer.php?u=https://www.thefa.com/" aria-label="Facebook. Opens in a new tab" target="_blank" class="fb-button"><img src="/assets/images/share-facebook-icon.svg" alt="Facebook" /><span class="tooltip">Share on Facebook. Opens in a new tab</span></a></li>
<li><a href="https://twitter.com/intent/tweet?url=https://www.thefa.com/" aria-label="Twitter. Opens in a new tab" target="_blank" class="twitter-button"><img src="/assets/images/share-twitter-icon.svg" alt="Twitter" /><span class="tooltip">Share on Twitter. Opens in a new tab</span></a></li>
<li><a href="https://wa.me/?text=https://www.thefa.com/" aria-label="Whatsapp. Opens in new Tab" target="_blank" class="whatsapp-button"><img src="/assets/images/share-whatsapp-icon.svg" alt="WhatsApp" /><span class="tooltip">Share on Whatsapp</span></a></li>
</ul>
</div>
<button class="share__native" data-behavior="share-native"><span>Share:</span><img src="/assets/images/share-icon.svg" /></button>
</div>
No notes defined.
{
"url": "https://www.thefa.com/"
}
/* eslint-disable no-param-reassign */
const nativeShare = ({ nativeShareElement, fallbackShareElement }) => {
if (!nativeShareElement || !navigator.share || window.innerWidth >= 1200) {
return false;
}
if (fallbackShareElement) {
fallbackShareElement.style.display = 'none';
}
nativeShareElement.style.display = 'flex';
return nativeShareElement.addEventListener('click', () => {
navigator
.share({
title: document.title,
url: window.location.href,
})
.catch(console.error);
});
};
const fallbackShare = ({ nativeShareElement, fallbackShareElement }) => {
if (!fallbackShareElement) {
return false;
}
if (nativeShareElement) {
// eslint-disable-next-line no-param-reassign
nativeShareElement.style.display = 'none';
}
fallbackShareElement.style.display = 'flex';
const copyButton = document.querySelector('[data-behavior=copy-url]');
if (!copyButton) {
return false;
}
if (!navigator.clipboard) {
copyButton.style.display = 'none';
return false;
}
return copyButton.addEventListener('click', () =>
navigator.clipboard.writeText(document.URL).catch(console.error)
);
};
export default ({ nativeShareElement, fallbackShareElement }) => {
fallbackShare({ nativeShareElement, fallbackShareElement });
nativeShare({ nativeShareElement, fallbackShareElement });
};
// TOOLTIP:
// This tooltip can be reused, providing that, the parent:
// - has `position: relative;`
// - sets the visibility.
// If this is reused, it should be moved into it's own component
.tooltip {
@include text-xs;
background-color: $color-interface-light;
border-radius: $border-radius;
color: $white;
left: 50%;
margin-top: $spacing-s;
padding: $spacing-s;
position: absolute;
transform: translateX(-50%);
visibility: hidden;
white-space: nowrap;
&::before {
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid $color-interface-light;
content: '';
display: block;
height: 0;
left: 50%;
position: absolute;
top: -5px;
transform: translateX(-50%);
width: 0;
}
}
.share {
&__fallback {
align-items: center;
display: flex;
button {
appearance: none;
border: none;
}
> p {
@include text-xs;
color: $color-interface-light;
text-transform: uppercase;
margin-top: 0.7rem;
}
}
&__native {
@include text-xs;
align-items: center;
color: $color-interface-light;
background-color: rgba(196, 196, 196, 0.1);
display: flex;
border-radius: 2.5rem;
border-width: 0;
padding: $spacing-s $spacing-l;
text-transform: uppercase;
span {
margin-top: 5px;
}
img {
margin-left: $spacing-s;
}
}
&__options {
display: flex;
justify-content: space-between;
margin-left: $spacing-s;
a,
button {
background-color: transparent;
border-radius: $border-radius;
display: block;
margin: 0 $spacing-s;
padding: 0;
position: relative;
transition: background-color 0.2s ease;
&:hover,
&:focus {
cursor: pointer;
img {
filter: hue-rotate(-35deg) saturate(3);
}
.tooltip {
visibility: visible;
}
}
&:focus {
@include focusOutline;
}
}
}
}
<div class="share">
<div class="share__fallback" data-behavior="share-fallback">
<p>Share:</p>
<ul class="share__options">
<li>
<button data-behavior="copy-url" aria-label="Copy page link"><img src="/assets/images/share-copy-icon.svg" />
<span class="tooltip">Copy page link</span>
</button>
</li>
<li><a href="https://www.facebook.com/sharer.php?u={{url}}" aria-label="Facebook. Opens in a new tab" target="_blank" class="fb-button"><img
src="/assets/images/share-facebook-icon.svg" alt="Facebook" /><span class="tooltip">Share on Facebook. Opens in a new tab</span></a></li>
<li><a href="https://twitter.com/intent/tweet?url={{url}}" aria-label="Twitter. Opens in a new tab" target="_blank" class="twitter-button"><img
src="/assets/images/share-twitter-icon.svg" alt="Twitter" /><span class="tooltip">Share on Twitter. Opens in a new tab</span></a></li>
<li><a href="https://wa.me/?text={{url}}" aria-label="Whatsapp. Opens in new Tab" target="_blank" class="whatsapp-button"><img
src="/assets/images/share-whatsapp-icon.svg" alt="WhatsApp" /><span class="tooltip">Share on Whatsapp</span></a></li>
</ul>
</div>
<button class="share__native" data-behavior="share-native"><span>Share:</span><img src="/assets/images/share-icon.svg" /></button>
</div>