Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Make embed work for <script> and React

Tim Wilsond6c851f5517d
diff --git a/packages/embeds/react/src/embed-core.ts b/packages/embeds/react/src/embed-core.ts
new file mode 100644
index 0000000000..3aa15ee038
--- /dev/null
+++ b/packages/embeds/react/src/embed-core.ts
@@ -0,0 +1,188 @@
+import { createFloatingButton } from "./floating-button";
+import { DubEmbed, Options } from "./types";
+
+declare global {
+ interface Window {
+ Dub: DubEmbed;
+ }
+}
+
+const containerStyles: Partial<CSSStyleDeclaration> = {
+ position: "fixed",
+ display: "flex",
+ flexDirection: "column",
+ alignItems: "flex-end",
+ bottom: "0",
+ right: "0",
+ width: "400px",
+ zIndex: "9998",
+ pointerEvents: "none",
+};
+
+const popupStyles: Partial<CSSStyleDeclaration> = {
+ width: "100%",
+ height: "100dvh",
+ maxHeight: "500px",
+ backgroundColor: "white",
+ borderRadius: "12px",
+ border: "1px solid #E5E5E5",
+ margin: "16px",
+ overflow: "hidden",
+ transformOrigin: "bottom right",
+ pointerEvents: "auto",
+};
+
+const WIDGET_URL = "http://localhost:8888/embed/widget";
+
+const createIframe = (iframeUrl: string, token: string): HTMLIFrameElement => {
+ const iframe = document.createElement("iframe");
+
+ iframe.src = `${iframeUrl}?token=${token}`;
+ iframe.style.width = "100%";
+ iframe.style.height = "100%";
+ iframe.style.border = "none";
+ iframe.setAttribute("credentialssupport", "");
+
+ return iframe;
+};
+
+const DUB_CONTAINER_ID = "dub-embed-container";
+const DUB_POPUP_ID = "dub-embed-popup";
+
+const renderWidget = (
+ options: Pick<Options, "token" | "onOpen" | "onClose">,
+): HTMLElement | null => {
+ const { token, onOpen, onClose } = options;
+
+ const existingContainer = document.getElementById(DUB_CONTAINER_ID);
+
+ if (existingContainer) {
+ document.body.removeChild(existingContainer);
+ onClose?.();
+ return existingContainer;
+ }
+
+ if (!token) {
+ console.error("[Dub] A link token is required to embed the widget.");
+ return null;
+ }
+
+ const container = document.createElement("div");
+ container.id = DUB_CONTAINER_ID;
+ Object.assign(container.style, containerStyles);
+
+ const popup: HTMLElement =
+ container.querySelector(`#${DUB_POPUP_ID}`) ??
+ document.createElement("div");
+ popup.id = DUB_POPUP_ID;
+ Object.assign(popup.style, { ...popupStyles, display: "none" });
+
+ const iframe = createIframe(WIDGET_URL, token);
+
+ popup.appendChild(iframe);
+ container.appendChild(popup);
+
+ document.body.appendChild(container);
+
+ onOpen?.();
+
+ return container;
+};
+
+// Initialize when an external button is clicked
+// const addButtonListener = (): void => {
+// const button = document.querySelector(
+// "[data-dub-token]",
+// ) as HTMLElement | null;
+
+// if (!button) {
+// console.error("[Dub] No button found with data-dub-token.");
+// return;
+// }
+
+// button.addEventListener("click", () => {
+// const token = button.getAttribute("data-dub-token");
+
+// if (!token) {
+// console.error("[Dub] A link token is required to embed the widget.");
+// return;
+// }
+
+// renderWidget({ token });
+// });
+// };
+
+let isWidgetOpen = false;
+
+export const openWidget = (): void => {
+ const popup = document.getElementById(DUB_POPUP_ID);
+ if (!popup) {
+ console.error("[Dub] No popup found.");
+ return;
+ }
+
+ popup.getAnimations().forEach((a) => a.cancel());
+ popup.style.display = "block";
+ popup.animate(
+ [
+ { transform: "scale(0.8)", opacity: 0 },
+ { transform: "scale(1)", opacity: 1 },
+ ],
+ {
+ duration: 100,
+ easing: "ease-in-out",
+ fill: "forwards",
+ },
+ );
+
+ isWidgetOpen = true;
+ if (typeof window !== "undefined" && window.Dub)
+ window.Dub.isWidgetOpen = true;
+};
+
+export const closeWidget = (): void => {
+ const popup = document.getElementById(DUB_POPUP_ID);
+ if (!popup) return;
+
+ popup.getAnimations().forEach((a) => a.cancel());
+ const animation = popup.animate([{ opacity: 0, transform: "scale(0.8)" }], {
+ duration: 100,
+ easing: "ease-in-out",
+ fill: "forwards",
+ });
+
+ animation.onfinish = () => {
+ if (popup && !isWidgetOpen) popup.style.display = "none";
+ };
+
+ isWidgetOpen = false;
+ if (typeof window !== "undefined" && window.Dub)
+ window.Dub.isWidgetOpen = false;
+};
+
+// Initialize when DOM is ready
+const init = (options: Options) => {
+ console.debug("[Dub] Initializing");
+
+ const container = renderWidget(options);
+ if (!container) return;
+
+ createFloatingButton({
+ container,
+ onClick: () => {
+ isWidgetOpen ? closeWidget() : openWidget();
+ },
+ });
+};
+
+const destroy = (): void => {
+ document.querySelectorAll(`#${DUB_CONTAINER_ID}`).forEach((c) => c.remove());
+};
+
+// TODO:
+// - Add a loading state
+// - Add a close button
+// - Inline embed (dashboard)
+// - Reuse addEventListener logic
+
+export { destroy, init };
diff --git a/packages/embeds/react/src/embed.ts b/packages/embeds/react/src/embed.ts
index eeb5ac667e..f72fbcbbd4 100644
--- a/packages/embeds/react/src/embed.ts
+++ b/packages/embeds/react/src/embed.ts
@@ -1,5 +1,5 @@
-import { createFloatingButton } from "./floating-button";
-import { DubEmbed, Options } from "./types";
+import { closeWidget, destroy, init, openWidget } from "./embed-core";
+import { DubEmbed } from "./types";
declare global {
interface Window {
@@ -7,185 +7,10 @@ declare global {
}
}
-window.Dub = (window.Dub || {}) as DubEmbed;
-
-const containerStyles: Partial<CSSStyleDeclaration> = {
- position: "fixed",
- display: "flex",
- flexDirection: "column",
- alignItems: "flex-end",
- bottom: "0",
- right: "0",
- width: "400px",
- zIndex: "9998",
- pointerEvents: "none",
-};
-
-const popupStyles: Partial<CSSStyleDeclaration> = {
- width: "100%",
- height: "100dvh",
- maxHeight: "500px",
- backgroundColor: "white",
- borderRadius: "12px",
- border: "1px solid #E5E5E5",
- margin: "16px",
- overflow: "hidden",
- transformOrigin: "bottom right",
- pointerEvents: "auto",
-};
-
-const WIDGET_URL = "http://localhost:8888/embed/widget";
-
-const createIframe = (iframeUrl: string, token: string): HTMLIFrameElement => {
- const iframe = document.createElement("iframe");
-
- iframe.src = `${iframeUrl}?token=${token}`;
- iframe.style.width = "100%";
- iframe.style.height = "100%";
- iframe.style.border = "none";
- iframe.setAttribute("credentialssupport", "");
-
- return iframe;
-};
-
-const DUB_CONTAINER_ID = "dub-embed-container";
-const DUB_POPUP_ID = "dub-embed-popup";
-
-const renderWidget = (
- options: Pick<Options, "token" | "onOpen" | "onClose">,
-): HTMLElement | null => {
- const { token, onOpen, onClose } = options;
-
- const existingContainer = document.getElementById(DUB_CONTAINER_ID);
-
- if (existingContainer) {
- document.body.removeChild(existingContainer);
- onClose?.();
- return existingContainer;
- }
-
- if (!token) {
- console.error("[Dub] A link token is required to embed the widget.");
- return null;
- }
-
- const container = document.createElement("div");
- container.id = DUB_CONTAINER_ID;
- Object.assign(container.style, containerStyles);
-
- const popup: HTMLElement =
- container.querySelector(`#${DUB_POPUP_ID}`) ??
- document.createElement("div");
- popup.id = DUB_POPUP_ID;
- Object.assign(popup.style, { ...popupStyles, display: "none" });
-
- const iframe = createIframe(WIDGET_URL, token);
-
- popup.appendChild(iframe);
- container.appendChild(popup);
-
- document.body.appendChild(container);
-
- // onOpen?.();
-
- return container;
-};
-
-// Initialize when an external button is clicked
-// const addButtonListener = (): void => {
-// const button = document.querySelector(
-// "[data-dub-token]",
-// ) as HTMLElement | null;
-
-// if (!button) {
-// console.error("[Dub] No button found with data-dub-token.");
-// return;
-// }
-
-// button.addEventListener("click", () => {
-// const token = button.getAttribute("data-dub-token");
-
-// if (!token) {
-// console.error("[Dub] A link token is required to embed the widget.");
-// return;
-// }
-
-// renderWidget({ token });
-// });
-// };
-
-let isWidgetOpen = false;
-
-const openWidget = (): void => {
- const popup = document.getElementById(DUB_POPUP_ID);
- if (!popup) {
- console.error("[Dub] No popup found.");
- return;
- }
-
- popup.getAnimations().forEach((a) => a.cancel());
- popup.style.display = "block";
- popup.animate(
- [
- { transform: "scale(0.8)", opacity: 0 },
- { transform: "scale(1)", opacity: 1 },
- ],
- {
- duration: 100,
- easing: "ease-in-out",
- fill: "forwards",
- },
- );
-
- isWidgetOpen = window.Dub.isWidgetOpen = true;
-};
-
-const closeWidget = (): void => {
- const popup = document.getElementById(DUB_POPUP_ID);
- if (!popup) return;
-
- popup.getAnimations().forEach((a) => a.cancel());
- const animation = popup.animate([{ opacity: 0, transform: "scale(0.8)" }], {
- duration: 100,
- easing: "ease-in-out",
- fill: "forwards",
- });
-
- animation.onfinish = () => {
- if (popup && !isWidgetOpen) popup.style.display = "none";
- };
-
- isWidgetOpen = window.Dub.isWidgetOpen = false;
-};
-
-// Initialize when DOM is ready
-const init = (options: Options) => {
- console.debug("[Dub] Initializing");
-
- const container = renderWidget(options);
- if (!container) return;
-
- createFloatingButton({
- container,
- onClick: () => {
- isWidgetOpen ? closeWidget() : openWidget();
- },
- });
-};
-
-const destroy = (): void => {
- document.querySelectorAll(`#${DUB_CONTAINER_ID}`).forEach((c) => c.remove());
-};
-
-window.Dub.init = init;
-window.Dub.destroy = destroy;
-window.Dub.openWidget = openWidget;
-window.Dub.closeWidget = closeWidget;
-
-// TODO:
-// - Add a loading state
-// - Add a close button icon
-// - Inline embed (dashboard)
-// - Reuse addEventListener logic
-
-export { destroy, init };
+if (typeof window !== "undefined") {
+ window.Dub = (window.Dub || {}) as DubEmbed;
+ window.Dub.init = init;
+ window.Dub.destroy = destroy;
+ window.Dub.openWidget = openWidget;
+ window.Dub.closeWidget = closeWidget;
+}
diff --git a/packages/embeds/react/src/widget.tsx b/packages/embeds/react/src/widget.tsx
index f05627f817..4d5f4882a5 100644
--- a/packages/embeds/react/src/widget.tsx
+++ b/packages/embeds/react/src/widget.tsx
@@ -2,7 +2,7 @@
import { APP_DOMAIN } from "@dub/utils";
import { useEffect } from "react";
-import { destroy, init } from "./embed";
+import { destroy, init } from "./embed-core";
import { DubEmbedOptions } from "./types";
export const DubWidget = ({ token, onTokenExpired }: DubEmbedOptions) => {