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.

update options

Kiran K595fadfb34ce
diff --git a/apps/web/app/app.dub.co/embed/widget/page-client.tsx b/apps/web/app/app.dub.co/embed/widget/page-client.tsx
index 116fe46301..669c0e67be 100644
--- a/apps/web/app/app.dub.co/embed/widget/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/widget/page-client.tsx
@@ -20,8 +20,8 @@ export function EmbedWidgetPageClient() {
const [copied, copyToClipboard] = useCopyToClipboard();
const [selectedTab, setSelectedTab] = useState<Tab>("invite");
- const { link, isLoading: isLoadingLink } = useReferralLink();
const { program } = useReferralProgram();
+ const { link, isLoading: isLoadingLink } = useReferralLink();
return (
<div>
diff --git a/dub.html b/dub.html
deleted file mode 100644
index fb55b430d0..0000000000
--- a/dub.html
+++ /dev/null
@@ -1,22 +0,0 @@
-<!doctype html>
-<html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Demo Preview</title>
- <script async src="http://localhost:8888/embed.js"></script>
- </head>
- <body>
- <!-- <script type="text/javascript">
- window.addEventListener("load", function () {
- window.Dub.options = {
- linkToken: "dub_embed_cUWcbfn76uAwaHbgoq8yuH6QvOgIaAQb0yoc", // replace with token
- };
- });
- </script> -->
-
- <button data-dub-token="dub_embed_cUWcbfn76uAwaHbgoq8yuH6QvOgIaAQb0yoc">
- Click Me
- </button>
- </body>
-</html>
diff --git a/packages/embeds/react/example/index.html b/packages/embeds/react/example/index.html
index 760e8471d2..c6781149fe 100644
--- a/packages/embeds/react/example/index.html
+++ b/packages/embeds/react/example/index.html
@@ -11,8 +11,26 @@
<script type="module" src="../dist/embed.js"></script>
</head>
<body>
- <button data-dub-token="dub_embed_MrRaGiUA3MkbdjTWJFHMYZrEaBR8KX355R0y">
+ <!-- Via JavaScript API -->
+ <!-- <script>
+ document.addEventListener("DOMContentLoaded", () => {
+ Dub.open({
+ token: "dub_embed_MrRaGiUA3MkbdjTWJFHMYZrEaBR8KX355R0y",
+
+ onOpen: () => {
+ console.log("Widget opened");
+ },
+
+ onClose: () => {
+ console.log("Widget closed");
+ },
+ });
+ });
+ </script> -->
+
+ <!-- Via Button using data attributes -->
+ <!-- <button data-dub-token="dub_embed_MrRaGiUA3MkbdjTWJFHMYZrEaBR8KX355R0y">
Click Me
- </button>
+ </button> -->
</body>
</html>
diff --git a/packages/embeds/react/src/dashboard.tsx b/packages/embeds/react/src/dashboard.tsx
index 71f99927e5..c9a3e67294 100644
--- a/packages/embeds/react/src/dashboard.tsx
+++ b/packages/embeds/react/src/dashboard.tsx
@@ -1,9 +1,9 @@
import { APP_DOMAIN } from "@dub/utils";
import { useEffect } from "react";
-import { DubOptions } from "./types";
+import { DubEmbedOptions } from "./types";
import { iframeStyles } from "./utils";
-export const DubDashboard = ({ token, onTokenExpired }: DubOptions) => {
+export const DubDashboard = ({ token, onTokenExpired }: DubEmbedOptions) => {
// Listen for messages from the iframe
useEffect(() => {
const handleMessage = (event: MessageEvent) => {
diff --git a/packages/embeds/react/src/embed.js b/packages/embeds/react/src/embed.js
deleted file mode 100644
index 31c5c6bc25..0000000000
--- a/packages/embeds/react/src/embed.js
+++ /dev/null
@@ -1 +0,0 @@
-// Move the embed.js to here
\ No newline at end of file
diff --git a/packages/embeds/react/src/embed.ts b/packages/embeds/react/src/embed.ts
index 7d7d0668d7..b166d9f974 100644
--- a/packages/embeds/react/src/embed.ts
+++ b/packages/embeds/react/src/embed.ts
@@ -1,20 +1,12 @@
-// TODO:
-// - Add a loading state
-// - Add a close button
-// - Inline embed (dashboard)
-// - Floating button + widget
-// - Open on a button click
-// - Reuse addEventListener logic
-
-import { DubOptions } from "./types";
+import { DubEmbed, Options } from "./types";
declare global {
interface Window {
- Dub: DubOptions;
+ Dub: DubEmbed;
}
}
-window.Dub = window.Dub || {};
+window.Dub = (window.Dub || {}) as DubEmbed;
const buttonStyles: Partial<CSSStyleDeclaration> = {
position: "fixed",
@@ -35,16 +27,7 @@ const containerStyles: Partial<CSSStyleDeclaration> = {
zIndex: "9998",
};
-const defaultOptions = {
- url: "http://localhost:8888/embed/widget",
-};
-
-const getOptions = (): DubOptions & { url: string } => {
- return {
- ...defaultOptions,
- ...window.Dub,
- };
-};
+const widgetUrl = "http://localhost:8888/embed/widget";
const createIframe = (iframeUrl: string, token: string): HTMLIFrameElement => {
const iframe = document.createElement("iframe");
@@ -58,91 +41,69 @@ const createIframe = (iframeUrl: string, token: string): HTMLIFrameElement => {
return iframe;
};
-// Initialize when an external button is clicked
-const initializeButton = (): void => {
- const button = document.querySelector(
- "[data-dub-token]",
- ) as HTMLElement | null;
+const renderWidget = (
+ options: Pick<Options, "token" | "onOpen" | "onClose">,
+): void => {
+ const containerId = "dub-widget-container";
+ const { token, onOpen, onClose } = options;
- if (!button) {
- console.error("[Dub] No button found with data-dub-token.");
+ const existingContainer = document.getElementById(containerId);
+
+ if (existingContainer) {
+ document.body.removeChild(existingContainer);
+ onClose?.();
return;
}
- button.addEventListener("click", () => {
- const existingContainer = document.getElementById("dub-widget-container");
-
- if (existingContainer) {
- // If the container exists, remove it (close the widget)
- document.body.removeChild(existingContainer);
- return;
- }
-
- const token = button.getAttribute("data-dub-token");
+ if (!token) {
+ console.error("[Dub] A link token is required to embed the widget.");
+ return;
+ }
- if (!token) {
- console.error("[Dub] A link token is required to embed the widget.");
- return;
- }
+ const container = document.createElement("div");
+ container.id = containerId;
+ Object.assign(container.style, containerStyles);
- const { url } = getOptions();
+ const iframe = createIframe(widgetUrl, token);
- const container = document.createElement("div");
- container.id = "dub-widget-container";
- Object.assign(container.style, containerStyles);
+ container.appendChild(iframe);
+ document.body.appendChild(container);
- const iframe = createIframe(url, token);
+ onOpen?.();
+};
- container.appendChild(iframe);
- document.body.appendChild(container);
- });
+// Open the widget via JavaScript API
+window.Dub.open = (options: Options) => {
+ renderWidget(options);
};
-// Initialize when Dub floating button is clicked
-const createFloatingButton = (): void => {
- const button = document.createElement("div");
- Object.assign(button.style, buttonStyles);
+// Initialize when an external button is clicked
+const addButtonListener = (): void => {
+ const button = document.querySelector(
+ "[data-dub-token]",
+ ) as HTMLElement | null;
- button.innerHTML = `
- <button>
- <span>Click Me</span>
- </button>
- `;
+ if (!button) {
+ console.error("[Dub] No button found with data-dub-token.");
+ return;
+ }
button.addEventListener("click", () => {
- const existingContainer = document.getElementById("dub-widget-container");
-
- if (existingContainer) {
- // If the container exists, remove it (close the widget)
- document.body.removeChild(existingContainer);
- return;
- }
-
- const { token, url } = getOptions();
+ const token = button.getAttribute("data-dub-token");
if (!token) {
console.error("[Dub] A link token is required to embed the widget.");
return;
}
- const container = document.createElement("div");
- container.id = "dub-widget-container";
- Object.assign(container.style, containerStyles);
-
- const iframe = createIframe(url, token);
-
- container.appendChild(iframe);
- document.body.appendChild(container);
+ renderWidget({ token });
});
-
- document.body.appendChild(button);
};
// Initialize when DOM is ready
const init = () => {
console.debug("[Dub] Initializing");
- //setTimeout(createFloatingButton, 100);
- setTimeout(initializeButton, 100);
+ setTimeout(addButtonListener, 100);
};
if (document.readyState === "loading") {
@@ -150,3 +111,38 @@ if (document.readyState === "loading") {
} else {
init();
}
+
+// Initialize when Dub floating button is clicked
+// const createFloatingButton = (): void => {
+// const button = document.createElement("div");
+// Object.assign(button.style, buttonStyles);
+
+// button.innerHTML = `
+// <button>
+// <span>Click Me</span>
+// </button>
+// `;
+
+// 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 });
+// });
+
+// document.body.appendChild(button);
+// };
+
+// TODO:
+// - Add a loading state
+// - Add a close button
+// - Inline embed (dashboard)
+// - Floating button + widget
+// - Open on a button click
+// - Reuse addEventListener logic
+
+// export {};
diff --git a/packages/embeds/react/src/types.ts b/packages/embeds/react/src/types.ts
index 87a28556eb..027dcd7e1b 100644
--- a/packages/embeds/react/src/types.ts
+++ b/packages/embeds/react/src/types.ts
@@ -1,4 +1,20 @@
-export interface DubOptions {
+export interface DubEmbedOptions {
token: string;
onTokenExpired?: () => void;
}
+
+export interface DubEmbed {
+ // Open the widget
+ open: (options: Options) => void;
+}
+
+export interface Options {
+ // The link token
+ token: string;
+
+ // The widget was opened
+ onOpen?: () => void;
+
+ // The widget was closed
+ onClose?: () => void;
+}
diff --git a/packages/embeds/react/src/utils.ts b/packages/embeds/react/src/utils.ts
index a7acee1476..e33eb6c2c8 100644
--- a/packages/embeds/react/src/utils.ts
+++ b/packages/embeds/react/src/utils.ts
@@ -2,7 +2,4 @@ export const iframeStyles = {
width: "100%",
height: "100%",
border: "none",
- credentialssupport: "",
- allow: "same-origin",
- crossOrigin: "use-credentials",
} as const;
diff --git a/packages/embeds/react/src/widget.tsx b/packages/embeds/react/src/widget.tsx
index ae9105f09f..38fd8779a4 100644
--- a/packages/embeds/react/src/widget.tsx
+++ b/packages/embeds/react/src/widget.tsx
@@ -1,9 +1,9 @@
import { APP_DOMAIN } from "@dub/utils";
import { useEffect } from "react";
-import { DubOptions } from "./types";
+import { DubEmbedOptions } from "./types";
import { iframeStyles } from "./utils";
-export const DubWidget = ({ token, onTokenExpired }: DubOptions) => {
+export const DubWidget = ({ token, onTokenExpired }: DubEmbedOptions) => {
// Listen for messages from the iframe
useEffect(() => {
const handleMessage = (event: MessageEvent) => {