The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor widget code a bit
Tim Wilsonae05e3397b18
diff --git a/packages/embeds/core/src/core.ts b/packages/embeds/core/src/core.tsindex f73e431c2f..af1fa2958d 100644--- a/packages/embeds/core/src/core.ts+++ b/packages/embeds/core/src/core.ts@@ -8,7 +8,12 @@ import {} from "./constants";import { EmbedError } from "./error";import { createFloatingButton } from "./floating-button";-import { DubOptions, DubWidgetPlacement, IframeMessage } from "./types";+import {+ DubInitResult,+ DubOptions,+ DubWidgetPlacement,+ IframeMessage,+} from "./types";const CONTAINER_STYLES = ({placement,@@ -90,8 +95,6 @@ const CLOSE_BUTTON_STYLE = {color: "white",};-let isWidgetOpen = false;-const createIframe = (iframeUrl: string,token: string,@@ -109,7 +112,13 @@ const createIframe = (return iframe;};-const renderWidget = (options: DubOptions) => {+/**+ * Generates and renders all of the widget's DOM elements.+ */+const renderWidget = (+ options: DubOptions,+ { closeWidget }: { closeWidget: () => void },+) => {console.debug("[Dub] Rendering widget.", options);const {@@ -208,7 +217,18 @@ const renderWidget = (options: DubOptions) => {return { container, updatePosition };};-export const openWidget = (): void => {+/**+ * Checks if the widget is open.+ */+const isWidgetOpen = (): boolean => {+ const popup = document.getElementById(DUB_POPUP_ID);+ return popup?.style.display === "block";+};++/**+ * Opens the widget.+ */+const openWidget = (): void => {const popup = document.getElementById(DUB_POPUP_ID);if (!popup) {console.error("[Dub] No popup found.");@@ -228,13 +248,12 @@ export const openWidget = (): void => {fill: "forwards",},);-- isWidgetOpen = true;- if (typeof window !== "undefined" && window.Dub)- window.Dub.isWidgetOpen = true;};-export const closeWidget = (): void => {+/**+ * Closes the widget.+ */+const closeWidget = (): void => {const popup = document.getElementById(DUB_POPUP_ID);if (!popup) return;@@ -246,29 +265,29 @@ export const closeWidget = (): void => {});animation.onfinish = () => {- if (popup && !isWidgetOpen) popup.style.display = "none";+ if (popup?.isConnected) popup.style.display = "none";};-- isWidgetOpen = false;- if (typeof window !== "undefined" && window.Dub)- window.Dub.isWidgetOpen = false;};-export const toggleWidget = (): void => {- isWidgetOpen ? closeWidget() : openWidget();+/**+ * Toggles the widget open state.+ */+const toggleWidget = (): void => {+ isWidgetOpen() ? closeWidget() : openWidget();};-export const init = (options: DubOptions) => {+/**+ * Initializes the Dub embed widget.+ */+export const init = (options: DubOptions): DubInitResult => {options.trigger = options.trigger ?? "floating-button";options.placement = options.placement ?? "bottom-right";console.debug("[Dub] Initializing.", options);- const { container, updatePosition } = renderWidget(options);+ const { container, updatePosition } = renderWidget(options, { closeWidget });- if (!container) {- return { destroy: () => {} };- }+ if (!container) return null;if (options.trigger === "floating-button")createFloatingButton({@@ -281,6 +300,10 @@ export const init = (options: DubOptions) => {});return {+ openWidget,+ closeWidget,+ toggleWidget,+ isWidgetOpen,destroy: () => {document.querySelectorAll(`#${DUB_CONTAINER_ID}`)@@ -290,5 +313,3 @@ export const init = (options: DubOptions) => {},};};--export const destroy = (): void => {};diff --git a/packages/embeds/core/src/embed.ts b/packages/embeds/core/src/embed.tsindex 008801b2a9..9f3b32212d 100644--- a/packages/embeds/core/src/embed.ts+++ b/packages/embeds/core/src/embed.ts@@ -1,4 +1,4 @@-import { closeWidget, destroy, init, openWidget, toggleWidget } from "./core";+import { init } from "./core";import { DubEmbed } from "./types";declare global {@@ -10,8 +10,4 @@ declare global {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/core/src/types.ts b/packages/embeds/core/src/types.tsindex 9081ca2013..4f9d471e1f 100644--- a/packages/embeds/core/src/types.ts+++ b/packages/embeds/core/src/types.ts@@ -1,10 +1,13 @@-export interface DubEmbed {- init: (options: DubOptions) => void;- isWidgetOpen: boolean;- toggleWidget: () => void;+export type DubInitResult = {openWidget: () => void;closeWidget: () => void;+ toggleWidget: () => void;+ isWidgetOpen: () => boolean;destroy: () => void;+} | null;++export interface DubEmbed {+ init: (options: DubOptions) => void;}export type DubOptions = {diff --git a/packages/embeds/react/src/widget.tsx b/packages/embeds/react/src/widget.tsxindex fe4d5ae46c..ca2eada915 100644--- a/packages/embeds/react/src/widget.tsx+++ b/packages/embeds/react/src/widget.tsx@@ -1,6 +1,6 @@"use client";-import { DubOptions, init, toggleWidget } from "@dub/embed-core";+import { DubOptions, init } from "@dub/embed-core";import {Children,cloneElement,@@ -10,23 +10,28 @@ import {PropsWithChildren,useEffect,useId,+ useRef,} from "react";export const DubWidget = memo(({ children, ...options }: PropsWithChildren<DubOptions>) => {const id = useId();+ const toggleWidgetRef = useRef<() => void>();useEffect(() => {- const { destroy } = init({- ...options,- anchorId: children && options.placement !== "center" ? id : undefined,- });+ const { destroy, toggleWidget } =+ init({+ ...options,+ anchorId: children && options.placement !== "center" ? id : undefined,+ }) || {};- return () => destroy();+ toggleWidgetRef.current = toggleWidget;++ return () => destroy?.();}, [children, id, options]);return children ? (- <Slot id={id} onClick={() => toggleWidget()}>+ <Slot id={id} onClick={() => toggleWidgetRef.current?.()}>{children}</Slot>) : null;