The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add toggleWidget
Tim Wilsonaaf52aa6d8a2
diff --git a/packages/embeds/react/example/index.html b/packages/embeds/react/example/index.htmlindex b27b1c3cef..fd40298df6 100644--- a/packages/embeds/react/example/index.html+++ b/packages/embeds/react/example/index.html@@ -20,9 +20,7 @@});</script>- <!-- Via Button using data attributes -->- <!-- <button data-dub-token="dub_embed_MrRaGiUA3MkbdjTWJFHMYZrEaBR8KX355R0y">- Click Me- </button> -->+ <!-- Via custom button -->+ <button onclick="window.Dub.toggleWidget()">Click Me</button></body></html>diff --git a/packages/embeds/react/src/embed-core.ts b/packages/embeds/react/src/embed-core.tsindex 7f2fb08c32..000753844c 100644--- a/packages/embeds/react/src/embed-core.ts+++ b/packages/embeds/react/src/embed-core.ts@@ -26,6 +26,7 @@ const POPUP_STYLES: Partial<CSSStyleDeclaration> = {backgroundColor: "white",borderRadius: "12px",border: "1px solid #E5E5E5",+ boxShadow: "0px 4px 20px 0px #0000000D",margin: "16px",overflow: "hidden",transformOrigin: "bottom right",@@ -92,29 +93,6 @@ const renderWidget = (options: Options): HTMLElement | null => {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 => {@@ -163,8 +141,11 @@ export const closeWidget = (): void => {window.Dub.isWidgetOpen = false;};-// Initialize when DOM is ready-const init = (options: Options) => {+export const toggleWidget = (): void => {+ isWidgetOpen ? closeWidget() : openWidget();+};++export const init = (options: Options) => {console.debug("[Dub] Initializing");const container = renderWidget(options);@@ -173,12 +154,12 @@ const init = (options: Options) => {createFloatingButton({container,onClick: () => {- isWidgetOpen ? closeWidget() : openWidget();+ toggleWidget();},});};-const destroy = (): void => {+export const destroy = (): void => {document.querySelectorAll(`#${DUB_CONTAINER_ID}`).forEach((c) => c.remove());};@@ -187,5 +168,3 @@ const destroy = (): void => {// - 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.tsindex f72fbcbbd4..6a3f076b1b 100644--- a/packages/embeds/react/src/embed.ts+++ b/packages/embeds/react/src/embed.ts@@ -1,4 +1,10 @@-import { closeWidget, destroy, init, openWidget } from "./embed-core";+import {+ closeWidget,+ destroy,+ init,+ openWidget,+ toggleWidget,+} from "./embed-core";import { DubEmbed } from "./types";declare global {@@ -11,6 +17,7 @@ if (typeof window !== "undefined") {window.Dub = (window.Dub || {}) as DubEmbed;window.Dub.init = init;window.Dub.destroy = destroy;+ window.Dub.toggleWidget = toggleWidget;window.Dub.openWidget = openWidget;window.Dub.closeWidget = closeWidget;}diff --git a/packages/embeds/react/src/types.ts b/packages/embeds/react/src/types.tsindex 9bba26d921..57a26e9378 100644--- a/packages/embeds/react/src/types.ts+++ b/packages/embeds/react/src/types.ts@@ -7,6 +7,7 @@ export interface DubEmbed {init: (options: Options) => void;isWidgetOpen: boolean;+ toggleWidget: () => void;openWidget: () => void;closeWidget: () => void;