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 theme option + script

Tim Wilson1ec60eea89f6
diff --git a/apps/web/app/layout.tsx b/apps/web/app/layout.tsx
index 591df371c6..1450a2ff12 100644
--- a/apps/web/app/layout.tsx
+++ b/apps/web/app/layout.tsx
@@ -1,6 +1,7 @@
import { geistMono, inter, satoshi } from "@/styles/fonts";
import "@/styles/globals.css";
import { cn, constructMetadata } from "@dub/utils";
+import Script from "next/script";
import RootProviders from "./providers";
export const metadata = constructMetadata();
@@ -17,6 +18,24 @@ export default function RootLayout({
>
<body>
<RootProviders>{children}</RootProviders>
+
+ <Script id="set-theme" strategy="beforeInteractive">
+ {`
+ (() => {
+ // Only run on referrals embed page for now
+ if (window.location.pathname !== '/embed/referrals') return;
+
+ const urlParams = new URLSearchParams(window.location.search);
+ const theme = urlParams.get('theme');
+
+ if (theme === 'dark' || (theme === 'system' && window.matchMedia("(prefers-color-scheme: dark)").matches)) {
+ document.body.classList.add("dark");
+ } else {
+ document.body.classList.remove("dark");
+ }
+ })();
+ `}
+ </Script>
</body>
</html>
);
diff --git a/packages/embeds/core/src/core.ts b/packages/embeds/core/src/core.ts
index 01f2dd709c..af8b25ed80 100644
--- a/packages/embeds/core/src/core.ts
+++ b/packages/embeds/core/src/core.ts
@@ -52,7 +52,7 @@ class DubEmbed {
return null;
}
- const iframe = createIframe(iframeUrl, token);
+ const iframe = createIframe(iframeUrl, token, this.options);
container.appendChild(iframe);
// Listen the message from the iframe
@@ -84,10 +84,19 @@ class DubEmbed {
}
}
-const createIframe = (iframeUrl: string, token: string): HTMLIFrameElement => {
+const createIframe = (
+ iframeUrl: string,
+ token: string,
+ options: Pick<DubEmbedOptions, "theme">,
+): HTMLIFrameElement => {
const iframe = document.createElement("iframe");
- iframe.src = `${iframeUrl}?token=${token}`;
+ const params = new URLSearchParams({
+ token,
+ ...(options.theme ? { theme: options.theme } : {}),
+ });
+
+ iframe.src = `${iframeUrl}?${params.toString()}`;
iframe.style.width = "100%";
iframe.style.height = "100%";
iframe.style.border = "none";
diff --git a/packages/embeds/core/src/types.ts b/packages/embeds/core/src/types.ts
index 703568db4e..222524bbf0 100644
--- a/packages/embeds/core/src/types.ts
+++ b/packages/embeds/core/src/types.ts
@@ -21,6 +21,9 @@ export type DubEmbedOptions = {
// The data type for the embed
data?: "referrals" | "analytics";
+
+ // The theme for the embed (light by default)
+ theme?: "light" | "dark" | "system";
};
export interface IframeMessage {
diff --git a/packages/embeds/react/src/embed.tsx b/packages/embeds/react/src/embed.tsx
index 24532f8a6b..9c9b464818 100644
--- a/packages/embeds/react/src/embed.tsx
+++ b/packages/embeds/react/src/embed.tsx
@@ -34,7 +34,7 @@ function DubEmbedInner({
}) || {};
return () => destroy?.();
- }, [id, options]);
+ }, [id, JSON.stringify(options)]);
return <div {...rest} ref={rootRef} />;
}