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.

Add ID/prefix logic for multiple embeds on the same page

Tim Wilson7ca4f1a878a2
diff --git a/apps/web/ui/layout/page-content/index.tsx b/apps/web/ui/layout/page-content/index.tsx
index 44aeb6216a..425c6b541a 100644
--- a/apps/web/ui/layout/page-content/index.tsx
+++ b/apps/web/ui/layout/page-content/index.tsx
@@ -2,6 +2,7 @@ import { MaxWidthWrapper } from "@dub/ui";
import { cn } from "@dub/utils";
import { PropsWithChildren, ReactNode } from "react";
import { HelpButtonRSC } from "../sidebar/help-button-rsc";
+import { ReferButton } from "../sidebar/refer-button";
import UserDropdown from "../sidebar/user-dropdown";
import { NavButton } from "./nav-button";
@@ -48,7 +49,7 @@ export function PageContent({
<div className="hidden md:block">{titleControls}</div>
)}
<div className="flex items-center gap-4 md:hidden">
- {/* <ReferButton /> */}
+ <ReferButton />
<HelpButtonRSC />
<UserDropdown />
</div>
diff --git a/packages/embeds/core/src/anchor.ts b/packages/embeds/core/src/anchor.ts
index d9e7341f81..76d464819e 100644
--- a/packages/embeds/core/src/anchor.ts
+++ b/packages/embeds/core/src/anchor.ts
@@ -3,9 +3,10 @@ import { DUB_CONTAINER_ID } from "./constants";
import { DubOptions } from "./types";
export function setAnchoredPosition({
+ prefix,
anchorId,
placement,
-}: Pick<DubOptions, "anchorId" | "placement">) {
+}: { prefix?: string } & Pick<DubOptions, "anchorId" | "placement">) {
if (!anchorId) return;
const anchor = document.getElementById(anchorId);
@@ -14,9 +15,11 @@ export function setAnchoredPosition({
return;
}
- const container = document.getElementById(DUB_CONTAINER_ID);
+ const container = document.getElementById(`${prefix}${DUB_CONTAINER_ID}`);
if (!container) {
- console.error(`[Dub] No container found with id: ${DUB_CONTAINER_ID}`);
+ console.error(
+ `[Dub] No container found with id: ${prefix}${DUB_CONTAINER_ID}`,
+ );
return;
}
diff --git a/packages/embeds/core/src/core.ts b/packages/embeds/core/src/core.ts
index b8ef8905c9..2304a373af 100644
--- a/packages/embeds/core/src/core.ts
+++ b/packages/embeds/core/src/core.ts
@@ -121,7 +121,7 @@ const createIframe = (iframeUrl: string, token: string): HTMLIFrameElement => {
*/
const renderWidget = (
options: DubOptions,
- { closeWidget }: { closeWidget: () => void },
+ { prefix, closeWidget }: { prefix?: string; closeWidget: () => void },
) => {
console.debug("[Dub] Rendering widget.", options);
@@ -137,7 +137,9 @@ const renderWidget = (
onTokenExpired,
} = options;
- const existingContainer = document.getElementById(DUB_CONTAINER_ID);
+ const existingContainer = document.getElementById(
+ `${prefix}${DUB_CONTAINER_ID}`,
+ );
if (existingContainer) {
document.body.removeChild(existingContainer);
@@ -151,7 +153,7 @@ const renderWidget = (
}
const container = document.createElement("div");
- container.id = DUB_CONTAINER_ID;
+ container.id = `${prefix}${DUB_CONTAINER_ID}`;
Object.assign(container.style, {
...CONTAINER_STYLES({
placement: placement ?? "bottom-right",
@@ -160,7 +162,7 @@ const renderWidget = (
});
const backdrop = document.createElement("div");
- backdrop.id = DUB_POPUP_BACKDROP_ID;
+ backdrop.id = `${prefix}${DUB_POPUP_BACKDROP_ID}`;
Object.assign(backdrop.style, {
...BACKDROP_STYLES,
display: "none",
@@ -169,9 +171,9 @@ const renderWidget = (
document.body.appendChild(backdrop);
const popup: HTMLElement =
- container.querySelector(`#${DUB_POPUP_ID}`) ??
+ container.querySelector("#" + CSS.escape(`${prefix}${DUB_POPUP_ID}`)) ??
document.createElement("div");
- popup.id = DUB_POPUP_ID;
+ popup.id = `${prefix}${DUB_POPUP_ID}`;
Object.assign(popup.style, {
...POPUP_STYLES({
placement: placement ?? "bottom-right",
@@ -185,7 +187,7 @@ const renderWidget = (
// Close button
const closeButton = document.createElement("button");
- closeButton.id = DUB_CLOSE_BUTTON_ID;
+ closeButton.id = `${prefix}${DUB_CLOSE_BUTTON_ID}`;
closeButton.innerHTML = CLOSE_ICON;
Object.assign(closeButton.style, CLOSE_BUTTON_STYLES);
closeButton.addEventListener("click", () => closeWidget());
@@ -219,7 +221,7 @@ const renderWidget = (
let updatePosition: (() => void) | undefined;
if (anchorId) {
- updatePosition = () => setAnchoredPosition({ anchorId, placement });
+ updatePosition = () => setAnchoredPosition({ prefix, anchorId, placement });
updatePosition();
window.addEventListener("resize", updatePosition);
}
@@ -232,65 +234,70 @@ const renderWidget = (
/**
* Checks if the widget is open.
*/
-const isWidgetOpen = (): boolean => {
- const popup = document.getElementById(DUB_POPUP_ID);
+const isWidgetOpen = (prefix?: string) => (): boolean => {
+ const popup = document.getElementById(`${prefix}${DUB_POPUP_ID}`);
return popup?.style.display === "block";
};
/**
* Opens the widget.
*/
-const openWidget = (): void => {
- const popup = document.getElementById(DUB_POPUP_ID);
- const backdrop = document.getElementById(DUB_POPUP_BACKDROP_ID);
- if (!popup) {
- console.error("[Dub] No popup found.");
- return;
- }
-
- popup.getAnimations().forEach((a) => a.cancel());
- popup.style.display = "block";
+const openWidget =
+ (prefix?: string, updatePosition?: () => void) => (): void => {
+ const popup = document.getElementById(`${prefix}${DUB_POPUP_ID}`);
+ const backdrop = document.getElementById(
+ `${prefix}${DUB_POPUP_BACKDROP_ID}`,
+ );
+ if (!popup) {
+ console.error("[Dub] No popup found.");
+ return;
+ }
- if (backdrop) {
- backdrop.style.display = "block";
- backdrop.animate(
- [
+ popup.getAnimations().forEach((a) => a.cancel());
+ popup.style.display = "block";
+
+ if (backdrop) {
+ backdrop.style.display = "block";
+ backdrop.animate(
+ [
+ {
+ opacity: 0,
+ backgroundColor: "rgba(115, 115, 115, 0)",
+ },
+ {
+ opacity: 1,
+ backgroundColor: "rgba(115, 115, 115, 0.3)",
+ },
+ ],
{
- opacity: 0,
- backgroundColor: "rgba(115, 115, 115, 0)",
- },
- {
- opacity: 1,
- backgroundColor: "rgba(115, 115, 115, 0.3)",
+ duration: 250,
+ easing: "ease-out",
+ fill: "forwards",
},
+ );
+ }
+
+ updatePosition?.();
+
+ popup.animate(
+ [
+ { transform: "translateY(10px)", opacity: 0 },
+ { transform: "translateY(0)", opacity: 1 },
],
{
duration: 250,
- easing: "ease-out",
+ easing: "ease-in-out",
fill: "forwards",
},
);
- }
-
- popup.animate(
- [
- { transform: "translateY(10px)", opacity: 0 },
- { transform: "translateY(0)", opacity: 1 },
- ],
- {
- duration: 250,
- easing: "ease-in-out",
- fill: "forwards",
- },
- );
-};
+ };
/**
* Closes the widget.
*/
-const closeWidget = (): void => {
- const popup = document.getElementById(DUB_POPUP_ID);
- const backdrop = document.getElementById(DUB_POPUP_BACKDROP_ID);
+const closeWidget = (prefix?: string) => (): void => {
+ const popup = document.getElementById(`${prefix}${DUB_POPUP_ID}`);
+ const backdrop = document.getElementById(`${prefix}${DUB_POPUP_BACKDROP_ID}`);
if (!popup) return;
popup.getAnimations().forEach((a) => a.cancel());
@@ -333,9 +340,12 @@ const closeWidget = (): void => {
/**
* Toggles the widget open state.
*/
-const toggleWidget = (): void => {
- isWidgetOpen() ? closeWidget() : openWidget();
-};
+const toggleWidget =
+ (prefix?: string, updatePosition?: () => void) => (): void => {
+ isWidgetOpen(prefix)()
+ ? closeWidget(prefix)()
+ : openWidget(prefix, updatePosition)();
+ };
/**
* Initializes the Dub embed widget.
@@ -346,7 +356,12 @@ export const init = (options: DubOptions): DubInitResult => {
console.debug("[Dub] Initializing.", options);
- const { container, updatePosition } = renderWidget(options, { closeWidget });
+ const prefix = options.id ? `${options.id}-` : "";
+
+ const { container, updatePosition } = renderWidget(options, {
+ prefix,
+ closeWidget: closeWidget(prefix),
+ });
if (!container) {
return null;
@@ -354,23 +369,24 @@ export const init = (options: DubOptions): DubInitResult => {
if (options.trigger === "floating-button") {
createFloatingButton({
+ prefix,
container: container,
buttonStyles: options.buttonStyles,
placement: options.placement,
onClick: () => {
- toggleWidget();
+ toggleWidget(prefix)();
},
});
}
return {
- openWidget,
- closeWidget,
- toggleWidget,
- isWidgetOpen,
+ openWidget: openWidget(prefix, updatePosition),
+ closeWidget: closeWidget(prefix),
+ toggleWidget: toggleWidget(prefix, updatePosition),
+ isWidgetOpen: isWidgetOpen(prefix),
destroy: () => {
document
- .querySelectorAll(`#${DUB_CONTAINER_ID}`)
+ .querySelectorAll("#" + CSS.escape(`${prefix}${DUB_CONTAINER_ID}`))
.forEach((c) => c.remove());
if (updatePosition) {
diff --git a/packages/embeds/core/src/floating-button.ts b/packages/embeds/core/src/floating-button.ts
index f37405b6b1..2a5d69d181 100644
--- a/packages/embeds/core/src/floating-button.ts
+++ b/packages/embeds/core/src/floating-button.ts
@@ -24,18 +24,20 @@ const FLOATING_BUTTON_STYLES = (
});
export const createFloatingButton = ({
+ prefix,
container,
buttonStyles,
placement,
onClick,
}: {
+ prefix?: string;
container: HTMLElement;
buttonStyles?: Record<string, any>;
placement: DubWidgetPlacement;
onClick: () => void;
}): void => {
const button = document.createElement("button");
- button.id = DUB_FLOATING_BUTTON_ID;
+ button.id = `${prefix}${DUB_FLOATING_BUTTON_ID}`;
Object.assign(button.style, {
...FLOATING_BUTTON_STYLES(placement),
...buttonStyles,
diff --git a/packages/embeds/core/src/types.ts b/packages/embeds/core/src/types.ts
index ffe904f3ff..fd8d495101 100644
--- a/packages/embeds/core/src/types.ts
+++ b/packages/embeds/core/src/types.ts
@@ -17,6 +17,9 @@ export type DubOptions = {
// The trigger for the widget
trigger?: DubWidgetTrigger;
+ // The ID of the widget (for multiple widgets on the same page)
+ id?: string;
+
// The widget was opened
onOpen?: () => void;
diff --git a/packages/embeds/react/src/widget.tsx b/packages/embeds/react/src/widget.tsx
index ca2eada915..9703c29903 100644
--- a/packages/embeds/react/src/widget.tsx
+++ b/packages/embeds/react/src/widget.tsx
@@ -21,6 +21,7 @@ export const DubWidget = memo(
useEffect(() => {
const { destroy, toggleWidget } =
init({
+ id,
...options,
anchorId: children && options.placement !== "center" ? id : undefined,
}) || {};