The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add `themeOptions` to referral embed
Tim Wilson4439400eefe3
diff --git a/apps/web/app/app.dub.co/embed/referrals/loading.tsx b/apps/web/app/app.dub.co/embed/referrals/loading.tsxdeleted file mode 100644index c2c64ca68c..0000000000--- a/apps/web/app/app.dub.co/embed/referrals/loading.tsx+++ /dev/null@@ -1,50 +0,0 @@-import { HeroBackground } from "@/ui/partners/hero-background";-import { Button, Copy, MoneyBill, Wordmark } from "@dub/ui";--export default function EmbedInlineLoading() {- return (- <div className="bg-bg-default flex min-h-screen flex-col">- <div className="p-5">- <div className="border-border-default relative flex flex-col overflow-hidden rounded-lg border p-4 md:p-6">- <HeroBackground color="#737373" />- <span className="text-content-subtle flex items-center gap-2 text-sm">- <MoneyBill className="size-4" />- Refer and earn- </span>- <div className="relative mt-16 flex flex-col gap-2">- <div className="bg-bg-muted h-6 w-60 rounded-md" />- <div className="bg-bg-muted h-6 w-40 rounded-md" />- </div>- <span className="text-content-default mb-1.5 mt-6 block text-sm">- Referral link- </span>- <div className="xs:flex-row relative flex flex-col items-center gap-2">- <div className="xs:w-60 border-border-default bg-bg-muted h-10 w-full rounded-md border" />- <Button- icon={<Copy className="size-4" />}- text="Copy link"- className="xs:w-fit"- disabled- />- </div>- <a- href="https://dub.partners"- target="_blank"- className="hover:text-content-default text-content-subtle mt-4 flex items-center justify-center gap-1.5 transition-colors duration-75 md:absolute md:bottom-3 md:right-3 md:mt-0 md:translate-x-0"- >- <p className="text-xs font-medium">Powered by</p>- <Wordmark className="text-content-emphasis h-3.5" />- </a>- </div>- <div className="mt-4 grid gap-2 sm:h-32 sm:grid-cols-3">- <div className="border-border-subtle bg-bg-muted h-full w-full rounded-lg border sm:col-span-2" />- <div className="border-border-subtle bg-bg-muted h-full w-full rounded-lg border" />- </div>- <div className="mt-4">- <div className="border-border-subtle bg-bg-muted h-10 w-full rounded-lg border" />- <div className="border-border-muted my-4 h-80 w-full rounded-lg border p-2" />- </div>- </div>- </div>- );-}diff --git a/apps/web/app/app.dub.co/embed/referrals/page-client.tsx b/apps/web/app/app.dub.co/embed/referrals/page-client.tsxindex 94b0abd4a4..0bca8dd8d9 100644--- a/apps/web/app/app.dub.co/embed/referrals/page-client.tsx+++ b/apps/web/app/app.dub.co/embed/referrals/page-client.tsx@@ -22,6 +22,7 @@ import { ReferralsEmbedFAQ } from "./faq";import { ReferralsEmbedLeaderboard } from "./leaderboard";import { ReferralsEmbedPayouts } from "./payouts";import { ReferralsEmbedQuickstart } from "./quickstart";+import { ThemeOptions } from "./theme-options";import { ReferralsReferralsEmbedToken } from "./token";const tabs = ["Quickstart", "Earnings", "Leaderboard", "FAQ"];@@ -33,6 +34,7 @@ export function ReferralsEmbedPageClient({discount,payouts,stats,+ themeOptions,}: {program: Program;links: Link[];@@ -47,12 +49,20 @@ export function ReferralsEmbedPageClient({leads: number;sales: number;};+ themeOptions: ThemeOptions;}) {const [copied, copyToClipboard] = useCopyToClipboard();const [selectedTab, setSelectedTab] = useState(tabs[0]);return (- <div className="bg-bg-default flex min-h-screen flex-col">+ <div+ style={+ themeOptions.backgroundColor+ ? { backgroundColor: themeOptions.backgroundColor }+ : undefined+ }+ className="bg-bg-default flex min-h-screen flex-col"+ ><div className="relative z-0 p-5"><div className="border-border-default relative flex flex-col overflow-hidden rounded-lg border p-4 md:p-6"><HeroBackground logo={program.logo} color={program.brandColor} />diff --git a/apps/web/app/app.dub.co/embed/referrals/page.tsx b/apps/web/app/app.dub.co/embed/referrals/page.tsxindex fbb442cc41..a3e772b61b 100644--- a/apps/web/app/app.dub.co/embed/referrals/page.tsx+++ b/apps/web/app/app.dub.co/embed/referrals/page.tsx@@ -1,14 +1,93 @@+import { HeroBackground } from "@/ui/partners/hero-background";+import { Button, Copy, MoneyBill, Wordmark } from "@dub/ui";+import { Suspense } from "react";import { ReferralsEmbedPageClient } from "./page-client";+import { parseThemeOptions, ThemeOptions } from "./theme-options";import { getReferralsEmbedData } from "./utils";-export default async function ReferralsEmbedPage({+export default function ReferralsEmbedPage({searchParams,}: {- searchParams: { token: string };+ searchParams: { token: string; themeOptions?: string };}) {- const { token } = searchParams;+ const { token, themeOptions: themeOptionsRaw } = searchParams;+ const themeOptions = parseThemeOptions(themeOptionsRaw);++ return (+ <Suspense fallback={<EmbedInlineLoading themeOptions={themeOptions} />}>+ <ReferralsEmbedRSC token={token} themeOptions={themeOptions} />+ </Suspense>+ );+}++async function ReferralsEmbedRSC({+ token,+ themeOptions,+}: {+ token: string;+ themeOptions: ThemeOptions;+}) {const embedData = await getReferralsEmbedData(token);- return <ReferralsEmbedPageClient {...embedData} />;+ return (+ <div>+ <ReferralsEmbedPageClient {...embedData} themeOptions={themeOptions} />+ </div>+ );+}++function EmbedInlineLoading({ themeOptions }: { themeOptions: ThemeOptions }) {+ return (+ <div+ style={+ themeOptions.backgroundColor+ ? { backgroundColor: themeOptions.backgroundColor }+ : undefined+ }+ className="bg-bg-default flex min-h-screen flex-col"+ >+ <div className="p-5">+ <div className="border-border-default relative flex flex-col overflow-hidden rounded-lg border p-4 md:p-6">+ <HeroBackground color="#737373" />+ <span className="text-content-subtle flex items-center gap-2 text-sm">+ <MoneyBill className="size-4" />+ Refer and earn+ </span>+ <div className="relative mt-16 flex flex-col gap-2">+ <div className="bg-bg-muted h-6 w-60 rounded-md" />+ <div className="bg-bg-muted h-6 w-40 rounded-md" />+ </div>+ <span className="text-content-default mb-1.5 mt-6 block text-sm">+ Referral link+ </span>+ <div className="xs:flex-row relative flex flex-col items-center gap-2">+ <div className="xs:w-60 border-border-default bg-bg-muted h-10 w-full rounded-md border" />+ <Button+ icon={<Copy className="size-4" />}+ text="Copy link"+ className="xs:w-fit"+ disabled+ />+ </div>+ <a+ href="https://dub.partners"+ target="_blank"+ className="hover:text-content-default text-content-subtle mt-4 flex items-center justify-center gap-1.5 transition-colors duration-75 md:absolute md:bottom-3 md:right-3 md:mt-0 md:translate-x-0"+ >+ <p className="text-xs font-medium">Powered by</p>+ <Wordmark className="text-content-emphasis h-3.5" />+ </a>+ </div>+ <div className="mt-4 grid gap-2 sm:h-32 sm:grid-cols-3">+ <div className="border-border-subtle bg-bg-muted h-full w-full rounded-lg border sm:col-span-2" />+ <div className="border-border-subtle bg-bg-muted h-full w-full rounded-lg border" />+ </div>+ <div className="mt-4">+ <div className="border-border-subtle bg-bg-muted h-10 w-full rounded-lg border" />+ <div className="border-border-muted my-4 h-80 w-full rounded-lg border p-2" />+ </div>+ </div>+ </div>+ );}diff --git a/apps/web/app/app.dub.co/embed/referrals/theme-options.ts b/apps/web/app/app.dub.co/embed/referrals/theme-options.tsnew file mode 100644index 0000000000..a0bac71ee1--- /dev/null+++ b/apps/web/app/app.dub.co/embed/referrals/theme-options.ts@@ -0,0 +1,22 @@+import { z } from "zod";++export type ThemeOptions = {+ backgroundColor?: string;+};++export function parseThemeOptions(themeOptions?: string): ThemeOptions {+ if (!themeOptions) return {};++ try {+ const parsed = JSON.parse(themeOptions);+ return z+ .object({+ backgroundColor: z.string().optional(),+ })+ .parse(parsed);+ } catch (error) {+ console.warn("Error parsing themeOptions", error);+ }++ return {};+}diff --git a/packages/embeds/core/src/core.ts b/packages/embeds/core/src/core.tsindex af8b25ed80..eff4427a3c 100644--- a/packages/embeds/core/src/core.ts+++ b/packages/embeds/core/src/core.ts@@ -87,13 +87,16 @@ class DubEmbed {const createIframe = (iframeUrl: string,token: string,- options: Pick<DubEmbedOptions, "theme">,+ options: Pick<DubEmbedOptions, "theme" | "themeOptions">,): HTMLIFrameElement => {const iframe = document.createElement("iframe");const params = new URLSearchParams({token,...(options.theme ? { theme: options.theme } : {}),+ ...(options.themeOptions+ ? { themeOptions: JSON.stringify(options.themeOptions) }+ : {}),});iframe.src = `${iframeUrl}?${params.toString()}`;diff --git a/packages/embeds/core/src/types.ts b/packages/embeds/core/src/types.tsindex 222524bbf0..dfd84ff0c8 100644--- a/packages/embeds/core/src/types.ts+++ b/packages/embeds/core/src/types.ts@@ -24,6 +24,12 @@ export type DubEmbedOptions = {// The theme for the embed (light by default)theme?: "light" | "dark" | "system";++ // Additional theme options+ themeOptions?: {+ // The background color for the embed+ backgroundColor?: string;+ };};export interface IframeMessage {