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 inline embed variant

Tim Wilsonf5a08136f042
diff --git a/apps/web/app/app.dub.co/embed/activity.tsx b/apps/web/app/app.dub.co/embed/activity.tsx
new file mode 100644
index 0000000000..2e848c1336
--- /dev/null
+++ b/apps/web/app/app.dub.co/embed/activity.tsx
@@ -0,0 +1,33 @@
+import { currencyFormatter, nFormatter } from "@dub/utils";
+
+export function Activity({
+ clicks,
+ leads,
+ earnings,
+}: {
+ clicks: number;
+ leads: number;
+ earnings: number;
+}) {
+ return (
+ <div className="mt-3 grid grid-cols-3 gap-2">
+ {[
+ { label: "Clicks", value: clicks },
+ { label: "Signups", value: leads },
+ { label: "Total earned", value: earnings },
+ ].map(({ label, value }) => (
+ <div className="flex flex-col gap-1.5 rounded-lg bg-neutral-100 p-2">
+ <span className="text-xs text-neutral-500">{label}</span>
+ <span className="text-sm font-semibold text-neutral-600">
+ {label === "Total earned"
+ ? currencyFormatter(value / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ })
+ : nFormatter(value)}
+ </span>
+ </div>
+ ))}
+ </div>
+ );
+}
diff --git a/apps/web/app/app.dub.co/embed/inline/page-client.tsx b/apps/web/app/app.dub.co/embed/inline/page-client.tsx
new file mode 100644
index 0000000000..b0f4d6c745
--- /dev/null
+++ b/apps/web/app/app.dub.co/embed/inline/page-client.tsx
@@ -0,0 +1,166 @@
+"use client";
+
+import { PartnerSaleResponse } from "@/lib/types";
+import { HeroBackground } from "@/ui/partners/hero-background";
+import { ProgramCommissionDescription } from "@/ui/partners/program-commission-description";
+import {
+ Button,
+ buttonVariants,
+ Check,
+ Copy,
+ MoneyBill2,
+ useCopyToClipboard,
+ Wordmark,
+} from "@dub/ui";
+import { cn, fetcher, getPrettyUrl } from "@dub/utils";
+import { Link, Program } from "@prisma/client";
+import { CSSProperties } from "react";
+import useSWR from "swr";
+import { Activity } from "../activity";
+import { SalesList } from "../sales-list";
+import { LinkToken } from "../token";
+
+type Tab = "invite" | "rewards";
+
+const heroAnimationDuration = 0.2;
+
+export function EmbedInlinePageClient({
+ program,
+ link,
+ earnings,
+ hasPartnerProfile,
+}: {
+ program: Program;
+ link: Link;
+ earnings: number;
+ hasPartnerProfile: boolean;
+}) {
+ const [copied, copyToClipboard] = useCopyToClipboard();
+
+ const { data: sales, isLoading } = useSWR<PartnerSaleResponse[]>(
+ "/api/embed/sales",
+ fetcher,
+ );
+
+ return (
+ <div
+ className="flex min-h-screen flex-col"
+ style={
+ { "--accent-color": program.brandColor || "#171717" } as CSSProperties
+ }
+ >
+ <div className="p-5">
+ <div className="relative flex flex-col rounded-lg border border-neutral-300 bg-gradient-to-r from-neutral-50 p-4 md:p-6">
+ {program && (
+ <HeroBackground
+ logo={program?.logo}
+ color={program?.brandColor || "#647481"}
+ />
+ )}
+ <span className="flex items-center gap-2 text-sm text-neutral-500">
+ <MoneyBill2 className="size-4" />
+ Refer and earn
+ </span>
+ <div className="relative mt-24 text-lg text-neutral-900 sm:max-w-[50%]">
+ <ProgramCommissionDescription program={program} />
+ </div>
+ <span className="mb-1.5 mt-6 block text-sm text-neutral-800">
+ Referral link
+ </span>
+ <div className="xs:flex-row relative flex flex-col items-center gap-2">
+ <input
+ type="text"
+ readOnly
+ value={getPrettyUrl(link.shortLink)}
+ className="xs:w-auto h-10 w-full rounded-md border border-neutral-300 px-3 text-sm focus:border-gray-500 focus:outline-none focus:ring-gray-500 lg:min-w-64 xl:min-w-72"
+ />
+ <Button
+ icon={
+ <div className="relative size-4">
+ <div
+ className={cn(
+ "absolute inset-0 transition-[transform,opacity]",
+ copied && "translate-y-1 opacity-0",
+ )}
+ >
+ <Copy className="size-4" />
+ </div>
+ <div
+ className={cn(
+ "absolute inset-0 transition-[transform,opacity]",
+ !copied && "translate-y-1 opacity-0",
+ )}
+ >
+ <Check className="size-4" />
+ </div>
+ </div>
+ }
+ text={copied ? "Copied link" : "Copy link"}
+ className="xs:w-fit"
+ onClick={() => copyToClipboard(link.shortLink)}
+ />
+ </div>
+ </div>
+ <div className="mt-5">
+ <>
+ <h2 className="text-sm font-semibold text-neutral-900">Activity</h2>
+ <Activity
+ clicks={link.clicks}
+ leads={link.leads}
+ earnings={earnings}
+ />
+ <div className="mt-4">
+ <h2 className="text-sm font-semibold text-neutral-900">
+ Recent sales
+ </h2>
+ <SalesList
+ sales={sales}
+ isLoading={isLoading}
+ hasPartnerProfile={hasPartnerProfile}
+ />
+ {!isLoading &&
+ sales &&
+ sales.length > 0 &&
+ (hasPartnerProfile ? (
+ <a
+ href="https://partners.dub.co/settings/payouts"
+ target="_blank"
+ className={cn(
+ buttonVariants({ variant: "primary" }),
+ "mt-3 flex h-10 items-center justify-center whitespace-nowrap rounded-lg border px-4 text-sm",
+ )}
+ >
+ Withdraw earnings
+ </a>
+ ) : (
+ <a
+ href="https://partners.dub.co/register"
+ target="_blank"
+ className={cn(
+ buttonVariants({ variant: "primary" }),
+ "mt-3 flex h-10 items-center justify-center whitespace-nowrap rounded-lg border px-4 text-sm",
+ )}
+ >
+ Create partner account
+ </a>
+ ))}
+ </div>
+ </>
+ <LinkToken />
+ </div>
+ </div>
+ <div className="flex grow flex-col justify-end">
+ <div className="flex items-center justify-center">
+ <a
+ href="https://d.to/conversions"
+ target="_blank"
+ className="flex items-center justify-center gap-1 rounded-lg bg-white p-2 pb-2.5 transition-colors"
+ >
+ <p className="text-sm text-neutral-500">Powered by</p>
+ <Wordmark className="h-4" />
+ </a>
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/apps/web/app/app.dub.co/embed/inline/page.tsx b/apps/web/app/app.dub.co/embed/inline/page.tsx
new file mode 100644
index 0000000000..4f12f6876d
--- /dev/null
+++ b/apps/web/app/app.dub.co/embed/inline/page.tsx
@@ -0,0 +1,22 @@
+import { getEmbedData } from "../utils";
+import { EmbedInlinePageClient } from "./page-client";
+
+export default async function EmbedInlinePage({
+ searchParams,
+}: {
+ searchParams: { token: string };
+}) {
+ const { token } = searchParams;
+
+ const { link, program, hasPartnerProfile, earnings } =
+ await getEmbedData(token);
+
+ return (
+ <EmbedInlinePageClient
+ program={program}
+ link={link}
+ earnings={earnings}
+ hasPartnerProfile={hasPartnerProfile}
+ />
+ );
+}
diff --git a/apps/web/app/app.dub.co/embed/sales-list.tsx b/apps/web/app/app.dub.co/embed/sales-list.tsx
new file mode 100644
index 0000000000..182fc129f6
--- /dev/null
+++ b/apps/web/app/app.dub.co/embed/sales-list.tsx
@@ -0,0 +1,77 @@
+import { PartnerSaleResponse } from "@/lib/types";
+import { Gift, LoadingSpinner } from "@dub/ui";
+import { cn, currencyFormatter } from "@dub/utils";
+
+export function SalesList({
+ sales,
+ isLoading,
+ hasPartnerProfile,
+}: {
+ sales: PartnerSaleResponse[] | undefined;
+ isLoading: boolean;
+ hasPartnerProfile: boolean;
+}) {
+ return sales ? (
+ sales.length ? (
+ <div className="mt-2.5 rounded-md border border-neutral-200">
+ <div
+ className={cn(
+ "grid grid-cols-1",
+ !hasPartnerProfile &&
+ "[mask-image:linear-gradient(black,transparent)]",
+ )}
+ >
+ {sales.slice(0, hasPartnerProfile ? 3 : 1).map((sale, idx) => (
+ <div
+ key={sale.id}
+ className={cn(
+ "flex items-center justify-between gap-4 border-neutral-200 px-3 py-2.5",
+ idx > 0 && "border-t",
+ )}
+ >
+ <div className="flex min-w-0 flex-col">
+ <span className="truncate text-sm font-medium text-neutral-600">
+ {sale.customer.email}
+ </span>
+ </div>
+ <div className="flex flex-col">
+ <span className="text-sm font-medium text-neutral-600">
+ {currencyFormatter(sale.earnings / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ })}
+ </span>
+ </div>
+ </div>
+ ))}
+ </div>
+ {!hasPartnerProfile && (
+ <p className="px-8 py-4 text-center text-xs text-neutral-500">
+ To withdraw your earnings or view all of your sales, create a free
+ Dub partner account below.
+ </p>
+ )}
+ </div>
+ ) : (
+ <EmptyState />
+ )
+ ) : isLoading ? (
+ <div className="mt-6 flex items-center justify-center">
+ <LoadingSpinner className="size-4" />
+ </div>
+ ) : (
+ <EmptyState />
+ );
+}
+
+const EmptyState = () => {
+ return (
+ <div className="mt-2.5 flex h-40 w-full flex-col items-center justify-center gap-2 rounded-lg border border-neutral-200 bg-neutral-50">
+ <Gift className="size-6 text-neutral-400" />
+ <p className="max-w-60 text-pretty text-center text-xs text-neutral-400">
+ No sales yet. When you refer a friend and they make a purchase, they'll
+ show up here.
+ </p>
+ </div>
+ );
+};
diff --git a/apps/web/app/app.dub.co/embed/widget/loading.tsx b/apps/web/app/app.dub.co/embed/widget/loading.tsx
deleted file mode 100644
index 4390743fb3..0000000000
--- a/apps/web/app/app.dub.co/embed/widget/loading.tsx
+++ /dev/null
@@ -1,9 +0,0 @@
-import { LoadingSpinner } from "@dub/ui/src/icons";
-
-export default function EmbedWidgetLoading() {
- return (
- <div className="flex h-full min-h-screen w-full items-center justify-center">
- <LoadingSpinner />
- </div>
- );
-}
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 449f5624aa..a8d2ade260 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
@@ -5,7 +5,6 @@ import {
AnimatedSizeContainer,
Button,
buttonVariants,
- LoadingSpinner,
ToggleGroup,
useCopyToClipboard,
Wordmark,
@@ -14,23 +13,18 @@ import {
Check2,
Copy,
EnvelopeArrowRight,
- Gift,
GiftFill,
LinkedIn,
QRCode,
Twitter,
} from "@dub/ui/src/icons";
-import {
- cn,
- currencyFormatter,
- fetcher,
- getPrettyUrl,
- nFormatter,
-} from "@dub/utils";
+import { cn, fetcher, getPrettyUrl } from "@dub/utils";
import { Link, Program } from "@prisma/client";
import { motion } from "framer-motion";
import { CSSProperties, useState } from "react";
import useSWR from "swr";
+import { Activity } from "../activity";
+import { SalesList } from "../sales-list";
import { LinkToken } from "../token";
type Tab = "invite" | "rewards";
@@ -218,82 +212,20 @@ export function EmbedWidgetPageClient({
}}
className="overflow-clip"
>
- <div className="mt-3 grid grid-cols-3 gap-2">
- {[
- { label: "Clicks", value: link.clicks },
- { label: "Signups", value: link.leads },
- { label: "Total earned", value: earnings },
- ].map(({ label, value }) => (
- <div className="flex flex-col gap-1.5 rounded-lg bg-neutral-100 p-2">
- <span className="text-xs text-neutral-500">{label}</span>
- <span className="text-sm font-semibold text-neutral-600">
- {label === "Total earned"
- ? currencyFormatter(value / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })
- : nFormatter(value)}
- </span>
- </div>
- ))}
- </div>
+ <Activity
+ clicks={link.clicks}
+ leads={link.leads}
+ earnings={earnings}
+ />
<div className="mt-4">
<h2 className="text-sm font-semibold text-neutral-900">
Recent sales
</h2>
- {sales ? (
- sales.length ? (
- <div className="mt-2.5 rounded-md border border-neutral-200">
- <div
- className={cn(
- "grid grid-cols-1",
- !hasPartnerProfile &&
- "[mask-image:linear-gradient(black,transparent)]",
- )}
- >
- {sales
- .slice(0, hasPartnerProfile ? 3 : 1)
- .map((sale, idx) => (
- <div
- key={sale.id}
- className={cn(
- "flex items-center justify-between gap-4 border-neutral-200 px-3 py-2.5",
- idx > 0 && "border-t",
- )}
- >
- <div className="flex min-w-0 flex-col">
- <span className="truncate text-sm font-medium text-neutral-600">
- {sale.customer.email}
- </span>
- </div>
- <div className="flex flex-col">
- <span className="text-sm font-medium text-neutral-600">
- {currencyFormatter(sale.earnings / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })}
- </span>
- </div>
- </div>
- ))}
- </div>
- {!hasPartnerProfile && (
- <p className="px-8 py-4 text-center text-xs text-neutral-500">
- To withdraw your earnings or view all of your sales,
- create a free Dub partner account below.
- </p>
- )}
- </div>
- ) : (
- <EmptyState />
- )
- ) : isLoading ? (
- <div className="mt-6 flex items-center justify-center">
- <LoadingSpinner className="size-4" />
- </div>
- ) : (
- <EmptyState />
- )}
+ <SalesList
+ sales={sales}
+ isLoading={isLoading}
+ hasPartnerProfile={hasPartnerProfile}
+ />
{!isLoading &&
sales &&
sales.length > 0 &&
@@ -342,15 +274,3 @@ export function EmbedWidgetPageClient({
</div>
);
}
-
-const EmptyState = () => {
- return (
- <div className="mt-2.5 flex h-40 w-full flex-col items-center justify-center gap-2 rounded-lg border border-neutral-200 bg-neutral-50">
- <Gift className="size-6 text-neutral-400" />
- <p className="max-w-60 text-pretty text-center text-xs text-neutral-400">
- No sales yet. When you refer a friend and they make a purchase, they'll
- show up here.
- </p>
- </div>
- );
-};
diff --git a/packages/embeds/core/src/constants.ts b/packages/embeds/core/src/constants.ts
index e0f9b0b69e..211b8b755b 100644
--- a/packages/embeds/core/src/constants.ts
+++ b/packages/embeds/core/src/constants.ts
@@ -1,4 +1,5 @@
export const WIDGET_URL = "https://app.dub.co/embed/widget";
+export const INLINE_URL = "https://app.dub.co/embed/inline";
export const DUB_CONTAINER_ID = "dub-embed-container";
export const DUB_POPUP_ID = "dub-embed-popup";
diff --git a/packages/embeds/core/src/core.ts b/packages/embeds/core/src/core.ts
index 63128841f3..0c82cd3fcf 100644
--- a/packages/embeds/core/src/core.ts
+++ b/packages/embeds/core/src/core.ts
@@ -4,12 +4,20 @@ import {
DUB_CONTAINER_ID,
DUB_FLOATING_BUTTON_ID,
DUB_POPUP_ID,
+ INLINE_URL,
WIDGET_URL,
} from "./constants";
import { EmbedError } from "./error";
import { createFloatingButton } from "./floating-button";
import { DubInitResult, DubOptions, IframeMessage } from "./types";
+const INLINE_CONTAINER_STYLES = {
+ position: "relative",
+ maxWidth: "1024px",
+ height: "730px",
+ margin: "0 auto",
+};
+
const CONTAINER_STYLES: Partial<CSSStyleDeclaration> = {
position: "fixed",
display: "flex",
@@ -49,6 +57,7 @@ class DubWidget {
container: HTMLElement | null;
constructor(options: DubOptions) {
+ options.variant = options.variant ?? "popup";
options.trigger = options.trigger ?? "floating-button";
options.buttonPlacement = options.buttonPlacement ?? "bottom-right";
@@ -61,7 +70,7 @@ class DubWidget {
this.container = this.renderWidget();
- if (options.trigger === "floating-button") {
+ if (options.variant === "popup" && options.trigger === "floating-button") {
createFloatingButton({
prefix,
buttonStyles: options.buttonStyles,
@@ -79,9 +88,10 @@ class DubWidget {
const {
token,
+ variant,
+ root,
containerStyles,
popupStyles,
- onClose,
onError,
onTokenExpired,
} = this.options;
@@ -90,11 +100,7 @@ class DubWidget {
`${this.prefix}${DUB_CONTAINER_ID}`,
);
- if (existingContainer) {
- document.body.removeChild(existingContainer);
- onClose?.();
- return existingContainer;
- }
+ if (existingContainer) return existingContainer;
if (!token) {
console.error("[Dub] A link token is required to embed the widget.");
@@ -104,37 +110,43 @@ class DubWidget {
const container = document.createElement("div");
container.id = `${this.prefix}${DUB_CONTAINER_ID}`;
Object.assign(container.style, {
- ...CONTAINER_STYLES,
+ ...(variant === "inline"
+ ? INLINE_CONTAINER_STYLES
+ : { ...CONTAINER_STYLES, visibility: "hidden" }),
...containerStyles,
- visibility: "hidden",
});
- container.addEventListener("click", (e) => {
- if (e.target === container) this.closeWidget();
- });
+ if (variant === "inline") {
+ const iframe = createIframe(INLINE_URL, token);
+ container.appendChild(iframe);
+ } else {
+ container.addEventListener("click", (e) => {
+ if (e.target === container) this.closeWidget();
+ });
- const popup: HTMLElement =
- container.querySelector(
- "#" + CSS.escape(`${this.prefix}${DUB_POPUP_ID}`),
- ) ?? document.createElement("div");
- popup.id = `${this.prefix}${DUB_POPUP_ID}`;
- Object.assign(popup.style, {
- ...POPUP_STYLES,
- ...popupStyles,
- });
+ const popup: HTMLElement =
+ container.querySelector(
+ "#" + CSS.escape(`${this.prefix}${DUB_POPUP_ID}`),
+ ) ?? document.createElement("div");
+ popup.id = `${this.prefix}${DUB_POPUP_ID}`;
+ Object.assign(popup.style, {
+ ...POPUP_STYLES,
+ ...popupStyles,
+ });
- const iframe = createIframe(WIDGET_URL, token);
+ const iframe = createIframe(WIDGET_URL, token);
- // Close button
- const closeButton = document.createElement("button");
- closeButton.id = `${this.prefix}${DUB_CLOSE_BUTTON_ID}`;
- closeButton.innerHTML = CLOSE_ICON;
- Object.assign(closeButton.style, CLOSE_BUTTON_STYLES);
- closeButton.addEventListener("click", () => this.closeWidget());
+ // Close button
+ const closeButton = document.createElement("button");
+ closeButton.id = `${this.prefix}${DUB_CLOSE_BUTTON_ID}`;
+ closeButton.innerHTML = CLOSE_ICON;
+ Object.assign(closeButton.style, CLOSE_BUTTON_STYLES);
+ closeButton.addEventListener("click", () => this.closeWidget());
- popup.appendChild(iframe);
- popup.appendChild(closeButton);
- container.appendChild(popup);
+ popup.appendChild(iframe);
+ popup.appendChild(closeButton);
+ container.appendChild(popup);
+ }
// Listen the message from the iframe
window.addEventListener("message", (e) => {
@@ -156,7 +168,7 @@ class DubWidget {
}
});
- document.body.appendChild(container);
+ (root ?? document.body).appendChild(container);
return container;
}
diff --git a/packages/embeds/core/src/example/inline.html b/packages/embeds/core/src/example/inline.html
new file mode 100644
index 0000000000..3a9269a7b2
--- /dev/null
+++ b/packages/embeds/core/src/example/inline.html
@@ -0,0 +1,19 @@
+<html></html>
+ <head>
+ <script type="module" src="../../dist/embed/script.js"></script>
+ </head>
+ <body>
+ <h1>Dub Referral Widget</h1>
+ <script>
+ document.addEventListener("DOMContentLoaded", () => {
+ const widget = Dub.init({
+ variant: "inline",
+ root: document.getElementById("root"),
+ token: "dub_embed_", // Add your token here
+ });
+ });
+ </script>
+
+ <div id="root" style="border: 1px solid #aaa;"></div>
+ </body>
+</html>
\ No newline at end of file
diff --git a/packages/embeds/core/src/example/widget.html b/packages/embeds/core/src/example/popup.html
similarity index 100%
rename from packages/embeds/core/src/example/widget.html
rename to packages/embeds/core/src/example/popup.html
diff --git a/packages/embeds/core/src/types.ts b/packages/embeds/core/src/types.ts
index a47f607b54..a98c32aa47 100644
--- a/packages/embeds/core/src/types.ts
+++ b/packages/embeds/core/src/types.ts
@@ -14,7 +14,13 @@ export type DubOptions = {
// The link token
token: string;
- // The trigger for the widget
+ // The variant of the widget
+ variant?: "inline" | "popup";
+
+ // The root element for the widget
+ root?: HTMLElement;
+
+ // The trigger for the popup widget
trigger?: DubWidgetTrigger;
// The ID of the widget (for multiple widgets on the same page)