Skip to content

Commit

Permalink
chore: Update dependencies and fix dark mode styles
Browse files Browse the repository at this point in the history
  • Loading branch information
royalfig committed May 18, 2024
1 parent 2ff3bfc commit b36f2fc
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 133 deletions.
3 changes: 2 additions & 1 deletion docs/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import "./style.css";
import "highlight.js/styles/nord.css";
import hljs from "highlight.js/lib/common";
import "@royalfig/share-button";
// import "@royalfig/share-button";
import "../../src/main";

// create a copy button as a plugin for highlight js
hljs.addPlugin({
Expand Down
7 changes: 6 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue",
sans-serif;
}
* {
box-sizing: border-box;
padding: 0;
margin:0;
}
</style>
</head>
<body>
<share-button></share-button>
<share-button circle></share-button>
</body>
</html>
1 change: 1 addition & 0 deletions src/dark-mode-styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
0 1.6px 5.3px rgb(0 0 0 / 12.8%), 0 3px 10px rgb(0 0 0 / 13.5%),
0 5.4px 17.9px rgb(0 0 0 / 14.2%), 0 10px 33.4px hsl(0deg 0 9% / 15.1%),
0 24px 80px hsl(0deg 0 9% / 17.1%);
--filter: drop-shadow(2px 2px 8px rgb(0 0 0 / 35%))
}
}
137 changes: 94 additions & 43 deletions src/dialog.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,105 @@
import { xIcon, fbIcon, liIcon, copyIcon, copiedIcon } from "./icons";
import { renderPreview } from "./preview";
import {
xIcon,
fbIcon,
liIcon,
copyIcon,
copiedIcon,
whatsappIcon,
telegramIcon,
emailIcon,
} from "./icons";

function createSocialMediaLink(
URL: string,
icon: string,
network: string,
shareURL: string,
text?: string,
) {
let finalUrl = shareURL.replace(/{{url}}/g, URL);

if (text) {
finalUrl = finalUrl.replace(/{{text}}/g, text);
}

return `<a
class='social-media ${network.toLowerCase()}'
href='${encodeURI(finalUrl)}'
aria-label='Share on ${network}'
target='_blank'
rel='noopener noreferrer'
part='share-link'
>${icon}
${network}</a>`;
}

export function createDialogEl({
url,
title,
img,
shareText,
}: Record<string, string>) {
return `
<header>
<p>${shareText ? shareText : "Share"}</p>
<button class="close-button">
<svg viewBox="0 0 24 24"><path d="M11.9997 10.5865L16.9495 5.63672L18.3637 7.05093L13.4139 12.0007L18.3637 16.9504L16.9495 18.3646L11.9997 13.4149L7.04996 18.3646L5.63574 16.9504L10.5855 12.0007L5.63574 7.05093L7.04996 5.63672L11.9997 10.5865Z" fill="currentColor"></path></svg>
</button>
</header>
<div class="dialog-body">
<div class="share-links">
<a
class='social-media twitter'
href='https://twitter.com/intent/tweet?text=${encodeURI(url)}'
aria-label='Share on Twitter'
target='_blank'
rel='noopener noreferrer'
>${xIcon}
Twitter</a>
<a
class='social-media linkedin'
href='https://www.linkedin.com/feed/?shareActive=true&text=${encodeURI(
url,
)}'
<div class="arrow"></div>
<div class="popover-inner">
<header part="header">
<p>${shareText ? shareText : "Share this link"}</p>
</header>
<div class="dialog-body" part="dialog-body">
<div class="share-links" part="share-links">
${createSocialMediaLink(
url,
xIcon,
"X",
"https://twitter.com/intent/tweet?url={{url}}&text={{text}}",
title,
)}
${createSocialMediaLink(
url,
liIcon,
"LinkedIn",
"https://www.linkedin.com/feed/?shareActive=true&text={{text}} {{url}}",
title,
)}
${createSocialMediaLink(
url,
fbIcon,
"Facebook",
"http://www.facebook.com/sharer.php?u={{url}}",
)}
aria-label='Share on LinkedIn'
target='_blank'
rel='noopener noreferrer'
>${liIcon}
Linkedin</a>
<a
class='social-media facebook'
href='http://www.facebook.com/sharer.php?u=${encodeURI(url)}'
aria-label='Share on Facebook'
target='_blank'
rel='noopener noreferrer'
>${fbIcon}
Facebook</a>
<button class='social-media copy-button' aria-label='Copy link'>
${copyIcon}
${copiedIcon}
<span>Copy link</span>
</button>
${createSocialMediaLink(
url,
emailIcon,
"Email",
"mailto:?subject=Check out {{text}}&body={{text}} {{url}}",
title,
)}
${createSocialMediaLink(
url,
telegramIcon,
"Telegram",
"https://t.me/share/url?url={{url}}&text={{text}}",
title,
)}
${createSocialMediaLink(
url,
whatsappIcon,
"WhatsApp",
"https://api.whatsapp.com/send?text={{text}} {{url}}",
title,
)}
<button class='social-media copy-button' aria-label='Copy link' part="share-link">
${copyIcon}
${copiedIcon}
<span>Copy link</span>
</button>
</div>
</div>
${renderPreview({ title, img })}
</div>`;
}
18 changes: 17 additions & 1 deletion src/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@ const copyIcon = `<svg viewBox="0 0 24 24"><path d="M7 4V2H17V4H20.0066C20.5552

const copiedIcon = `<svg viewBox="0 0 24 24"><path d="M9.9997 15.1709L19.1921 5.97852L20.6063 7.39273L9.9997 17.9993L3.63574 11.6354L5.04996 10.2212L9.9997 15.1709Z" fill="currentColor"></path></svg>`;

export { icons, xIcon, fbIcon, liIcon, copyIcon, copiedIcon };
const whatsappIcon = `<svg viewBox="0 0 24 24"><path d="M12.001 2C17.5238 2 22.001 6.47715 22.001 12C22.001 17.5228 17.5238 22 12.001 22C10.1671 22 8.44851 21.5064 6.97086 20.6447L2.00516 22L3.35712 17.0315C2.49494 15.5536 2.00098 13.8345 2.00098 12C2.00098 6.47715 6.47813 2 12.001 2ZM8.59339 7.30019L8.39232 7.30833C8.26293 7.31742 8.13607 7.34902 8.02057 7.40811C7.93392 7.45244 7.85348 7.51651 7.72709 7.63586C7.60774 7.74855 7.53857 7.84697 7.46569 7.94186C7.09599 8.4232 6.89729 9.01405 6.90098 9.62098C6.90299 10.1116 7.03043 10.5884 7.23169 11.0336C7.63982 11.9364 8.31288 12.8908 9.20194 13.7759C9.4155 13.9885 9.62473 14.2034 9.85034 14.402C10.9538 15.3736 12.2688 16.0742 13.6907 16.4482C13.6907 16.4482 14.2507 16.5342 14.2589 16.5347C14.4444 16.5447 14.6296 16.5313 14.8153 16.5218C15.1066 16.5068 15.391 16.428 15.6484 16.2909C15.8139 16.2028 15.8922 16.159 16.0311 16.0714C16.0311 16.0714 16.0737 16.0426 16.1559 15.9814C16.2909 15.8808 16.3743 15.81 16.4866 15.6934C16.5694 15.6074 16.6406 15.5058 16.6956 15.3913C16.7738 15.2281 16.8525 14.9166 16.8838 14.6579C16.9077 14.4603 16.9005 14.3523 16.8979 14.2854C16.8936 14.1778 16.8047 14.0671 16.7073 14.0201L16.1258 13.7587C16.1258 13.7587 15.2563 13.3803 14.7245 13.1377C14.6691 13.1124 14.6085 13.1007 14.5476 13.097C14.4142 13.0888 14.2647 13.1236 14.1696 13.2238C14.1646 13.2218 14.0984 13.279 13.3749 14.1555C13.335 14.2032 13.2415 14.3069 13.0798 14.2972C13.0554 14.2955 13.0311 14.292 13.0074 14.2858C12.9419 14.2685 12.8781 14.2457 12.8157 14.2193C12.692 14.1668 12.6486 14.1469 12.5641 14.1105C11.9868 13.8583 11.457 13.5209 10.9887 13.108C10.8631 12.9974 10.7463 12.8783 10.6259 12.7616C10.2057 12.3543 9.86169 11.9211 9.60577 11.4938C9.5918 11.4705 9.57027 11.4368 9.54708 11.3991C9.50521 11.331 9.45903 11.25 9.44455 11.1944C9.40738 11.0473 9.50599 10.9291 9.50599 10.9291C9.50599 10.9291 9.74939 10.663 9.86248 10.5183C9.97128 10.379 10.0652 10.2428 10.125 10.1457C10.2428 9.95633 10.2801 9.76062 10.2182 9.60963C9.93764 8.92565 9.64818 8.24536 9.34986 7.56894C9.29098 7.43545 9.11585 7.33846 8.95659 7.32007C8.90265 7.31384 8.84875 7.30758 8.79459 7.30402C8.66053 7.29748 8.5262 7.29892 8.39232 7.30833L8.59339 7.30019Z"></path></svg>`;

const telegramIcon = `<svg viewBox="0 0 24 24"><path d="M12.001 22C6.47813 22 2.00098 17.5228 2.00098 12C2.00098 6.47715 6.47813 2 12.001 2C17.5238 2 22.001 6.47715 22.001 12C22.001 17.5228 17.5238 22 12.001 22ZM8.89113 13.1708L8.90378 13.1628C9.48351 15.0767 9.77337 16.0337 9.77337 16.0337C9.88564 16.3442 10.04 16.3996 10.2273 16.3743C10.4145 16.3489 10.5139 16.2476 10.6361 16.1297C10.6361 16.1297 11.0324 15.7472 11.825 14.9823L14.3759 16.8698C14.8407 17.1266 15.1763 16.9941 15.2917 16.4377L16.9495 8.61641C17.1325 7.88842 16.8115 7.59644 16.247 7.82754L6.51397 11.5871C5.84996 11.854 5.85317 12.2255 6.39308 12.3911L8.89113 13.1708Z"></path></svg>`;

const emailIcon = `<svg viewBox="0 0 24 24"><path d="M3 3H21C21.5523 3 22 3.44772 22 4V20C22 20.5523 21.5523 21 21 21H3C2.44772 21 2 20.5523 2 20V4C2 3.44772 2.44772 3 3 3ZM12.0606 11.6829L5.64722 6.2377L4.35278 7.7623L12.0731 14.3171L19.6544 7.75616L18.3456 6.24384L12.0606 11.6829Z"></path></svg>`;

export {
copiedIcon,
copyIcon,
emailIcon,
fbIcon,
icons,
liIcon,
telegramIcon,
whatsappIcon,
xIcon,
};
66 changes: 54 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class ShareButton extends HTMLElement {
}

const button = document.createElement("share-button");
button.setAttribute("part", "share-button");
for (const key of userCustomProps) {
const value = isInjected.getAttribute(`data-${key}`);
value && button.setAttribute(key, value);
Expand Down Expand Up @@ -54,31 +55,40 @@ export class ShareButton extends HTMLElement {

// button
const button = document.createElement("button");
const buttonText = this.getAttribute("button-text") ?? "Share";
button.setAttribute("part", "share-button");
button.setAttribute("class", "share-button");
button.setAttribute("class", "share-button");
button.setAttribute("popovertarget", "share-popover");
const buttonText = this.textContent ?? "Share";
const isCircle = this.hasAttribute("circle");

if (!buttonText) {
if (isCircle) {
button.setAttribute("aria-label", "Share");
button.setAttribute("style", "border-radius: 50%; padding: 0.5rem;");
button.innerHTML = icon;
} else {
button.innerHTML = `${
this.textContent ? "<slot></slot>" : "Share"
}${icon}`;
}

button.setAttribute("class", "share-button");
button.innerHTML = buttonText + icon;

// dialog
const dialog = document.createElement("dialog");
const dialog = document.createElement("div");
dialog.setAttribute("id", "share-popover");
dialog.setAttribute("part", "share-popover");
dialog.setAttribute("popover", "auto");

const dialogContent = createDialogEl({
url: window.location.href,
title,
img: ogImage,
shareText: buttonText,
});
dialog.innerHTML = dialogContent;
const closeButton = dialog.querySelector(".close-button");
closeButton?.addEventListener("click", () => {
dialog.close();
});
button.addEventListener("click", () => {

button.addEventListener("click", (e) => {
const userAgent = navigator.userAgent;
const target = e.currentTarget as Element;

if (
(/android/i.test(userAgent) || /iPhone|iPad|iPod/i.test(userAgent)) &&
Expand All @@ -89,11 +99,42 @@ export class ShareButton extends HTMLElement {
title,
url: window.location.href,
});
target.removeAttribute("popover");
} catch (err) {
console.log(err);
}
} else {
dialog.showModal();
const target = e.currentTarget as Element;

const buttonCoords = target.getBoundingClientRect();

const scrollY = window.scrollY;
const popover = shadow.querySelector("[popover]") as HTMLElement;
popover?.style.setProperty(
"left",
`calc(${
buttonCoords.left + buttonCoords.width / 2
}px - 1em - .75em/2)`,
);

console.log(
scrollY,
document.documentElement.clientHeight / 2 > buttonCoords.y,
popover,
);

if (document.documentElement.clientHeight / 2 > buttonCoords.y) {
// PUT below
popover.style.top = `${
scrollY + buttonCoords.top + buttonCoords.height + 10
}px`;
popover.style.translate = "initial";
} else {
// PUT above

popover.style.top = `${scrollY + buttonCoords.top - buttonCoords.height + 10 }px`;
popover.style.translate = "0 -100%";
}
}
});

Expand Down Expand Up @@ -130,6 +171,7 @@ export class ShareButton extends HTMLElement {

const wrapper = document.createElement("div");
wrapper.setAttribute("class", "wrapper");
wrapper.setAttribute("part", "wrapper");
wrapper.append(button, dialog);
shadow.append(wrapper);

Expand Down
Loading

0 comments on commit b36f2fc

Please sign in to comment.