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 placement options

Tim Wilson53020b8c5545
diff --git a/packages/embeds/react/example/index.html b/packages/embeds/react/example/index.html
index fd40298df6..90935cefb3 100644
--- a/packages/embeds/react/example/index.html
+++ b/packages/embeds/react/example/index.html
@@ -11,16 +11,16 @@
<script type="module" src="../dist/embed.js"></script>
</head>
<body>
- <!-- Via JavaScript API -->
<script>
document.addEventListener("DOMContentLoaded", () => {
Dub.init({
token: "dub_embed_MrRaGiUA3MkbdjTWJFHMYZrEaBR8KX355R0y",
+ trigger: "manual",
});
});
</script>
- <!-- Via custom button -->
+ <!-- Custom toggle button -->
<button onclick="window.Dub.toggleWidget()">Click Me</button>
</body>
</html>
diff --git a/packages/embeds/react/src/embed-core.ts b/packages/embeds/react/src/embed-core.ts
index 000753844c..9a6da6b244 100644
--- a/packages/embeds/react/src/embed-core.ts
+++ b/packages/embeds/react/src/embed-core.ts
@@ -1,5 +1,5 @@
import { createFloatingButton } from "./floating-button";
-import { DubEmbed, Options } from "./types";
+import { DubEmbed, DubWidgetPlacement, Options } from "./types";
declare global {
interface Window {
@@ -7,19 +7,52 @@ declare global {
}
}
-const CONTAINER_STYLES: Partial<CSSStyleDeclaration> = {
+const CONTAINER_STYLES = (
+ placement: DubWidgetPlacement,
+): Partial<CSSStyleDeclaration> => ({
position: "fixed",
display: "flex",
- flexDirection: "column",
- alignItems: "flex-end",
- bottom: "0",
- right: "0",
+ ...{
+ "bottom-right": {
+ bottom: "0",
+ right: "0",
+ alignItems: "flex-end",
+ flexDirection: "column",
+ },
+ "bottom-left": {
+ bottom: "0",
+ left: "0",
+ alignItems: "flex-start",
+ flexDirection: "column",
+ },
+ "top-right": {
+ top: "0",
+ right: "0",
+ alignItems: "flex-end",
+ flexDirection: "column-reverse",
+ },
+ "top-left": {
+ top: "0",
+ left: "0",
+ alignItems: "flex-start",
+ flexDirection: "column-reverse",
+ },
+ center: {
+ top: "50%",
+ left: "50%",
+ alignItems: "flex-start",
+ flexDirection: "column",
+ transform: "translate(-50%, -50%)",
+ },
+ }[placement],
width: "400px",
zIndex: "9998",
pointerEvents: "none",
-};
+});
-const POPUP_STYLES: Partial<CSSStyleDeclaration> = {
+const POPUP_STYLES = (
+ placement: DubWidgetPlacement,
+): Partial<CSSStyleDeclaration> => ({
width: "100%",
height: "100dvh",
maxHeight: "500px",
@@ -29,9 +62,15 @@ const POPUP_STYLES: Partial<CSSStyleDeclaration> = {
boxShadow: "0px 4px 20px 0px #0000000D",
margin: "16px",
overflow: "hidden",
- transformOrigin: "bottom right",
+ ...{
+ "bottom-right": { transformOrigin: "bottom right" },
+ "bottom-left": { transformOrigin: "bottom left" },
+ "top-right": { transformOrigin: "top right" },
+ "top-left": { transformOrigin: "top left" },
+ center: { transformOrigin: "center" },
+ }[placement],
pointerEvents: "auto",
-};
+});
const WIDGET_URL = "http://localhost:8888/embed/widget";
@@ -51,7 +90,7 @@ const DUB_CONTAINER_ID = "dub-embed-container";
const DUB_POPUP_ID = "dub-embed-popup";
const renderWidget = (options: Options): HTMLElement | null => {
- const { token, onOpen, onClose, containerStyles, popupStyles, buttonStyles } =
+ const { token, placement, onOpen, onClose, containerStyles, popupStyles } =
options;
const existingContainer = document.getElementById(DUB_CONTAINER_ID);
@@ -69,14 +108,17 @@ const renderWidget = (options: Options): HTMLElement | null => {
const container = document.createElement("div");
container.id = DUB_CONTAINER_ID;
- Object.assign(container.style, { ...CONTAINER_STYLES, containerStyles });
+ Object.assign(container.style, {
+ ...CONTAINER_STYLES(placement ?? "bottom-right"),
+ containerStyles,
+ });
const popup: HTMLElement =
container.querySelector(`#${DUB_POPUP_ID}`) ??
document.createElement("div");
popup.id = DUB_POPUP_ID;
Object.assign(popup.style, {
- ...POPUP_STYLES,
+ ...POPUP_STYLES(placement ?? "bottom-right"),
...popupStyles,
display: "none",
});
@@ -146,17 +188,23 @@ export const toggleWidget = (): void => {
};
export const init = (options: Options) => {
+ options.trigger = options.trigger ?? "floating-button";
+ options.placement = options.placement ?? "bottom-right";
+
console.debug("[Dub] Initializing");
const container = renderWidget(options);
if (!container) return;
- createFloatingButton({
- container,
- onClick: () => {
- toggleWidget();
- },
- });
+ if (options.trigger === "floating-button")
+ createFloatingButton({
+ container,
+ buttonStyles: options.buttonStyles,
+ placement: options.placement,
+ onClick: () => {
+ toggleWidget();
+ },
+ });
};
export const destroy = (): void => {
diff --git a/packages/embeds/react/src/floating-button.ts b/packages/embeds/react/src/floating-button.ts
index c799635a8b..f771c95494 100644
--- a/packages/embeds/react/src/floating-button.ts
+++ b/packages/embeds/react/src/floating-button.ts
@@ -1,7 +1,18 @@
+import { DubWidgetPlacement } from "./types";
+
const DUB_FLOATING_BUTTON_ID = "dub-floating-button";
-const FLOATING_BUTTON_STYLES: Partial<CSSStyleDeclaration> = {
- margin: "0 16px 16px 0",
+const FLOATING_BUTTON_STYLES = (
+ placement: DubWidgetPlacement,
+): Partial<CSSStyleDeclaration> => ({
+ margin: "16px",
+ ...{
+ "bottom-right": { marginTop: "0" },
+ "bottom-left": { marginTop: "0" },
+ "top-right": { marginBottom: "0" },
+ "top-left": { marginBottom: "0" },
+ center: {},
+ }[placement],
backgroundColor: "#171717",
color: "#fafafa",
padding: "12px",
@@ -9,21 +20,23 @@ const FLOATING_BUTTON_STYLES: Partial<CSSStyleDeclaration> = {
border: "none",
transition: "transform 0.1s ease-in-out",
pointerEvents: "auto",
-};
+});
export const createFloatingButton = ({
container,
buttonStyles,
+ placement,
onClick,
}: {
container: HTMLElement;
buttonStyles?: Record<string, any>;
+ placement: DubWidgetPlacement;
onClick: () => void;
}): void => {
const button = document.createElement("button");
button.id = DUB_FLOATING_BUTTON_ID;
Object.assign(button.style, {
- ...FLOATING_BUTTON_STYLES,
+ ...FLOATING_BUTTON_STYLES(placement),
...buttonStyles,
});
diff --git a/packages/embeds/react/src/types.ts b/packages/embeds/react/src/types.ts
index 57a26e9378..67550b7e16 100644
--- a/packages/embeds/react/src/types.ts
+++ b/packages/embeds/react/src/types.ts
@@ -18,13 +18,28 @@ export interface Options {
// The link token
token: string;
+ // The trigger for the widget
+ trigger?: DubWidgetTrigger;
+
// The widget was opened
onOpen?: () => void;
// The widget was closed
onClose?: () => void;
+ // The placement of the widget
+ placement?: DubWidgetPlacement;
+
containerStyles?: Partial<CSSStyleDeclaration>;
popupStyles?: Partial<CSSStyleDeclaration>;
buttonStyles?: Partial<CSSStyleDeclaration>;
}
+
+export type DubWidgetTrigger = "floating-button" | "manual";
+
+export type DubWidgetPlacement =
+ | "bottom-right"
+ | "bottom-left"
+ | "top-right"
+ | "top-left"
+ | "center";
diff --git a/packages/embeds/react/src/widget.tsx b/packages/embeds/react/src/widget.tsx
index 4d5f4882a5..b8852b06bd 100644
--- a/packages/embeds/react/src/widget.tsx
+++ b/packages/embeds/react/src/widget.tsx
@@ -3,9 +3,13 @@
import { APP_DOMAIN } from "@dub/utils";
import { useEffect } from "react";
import { destroy, init } from "./embed-core";
-import { DubEmbedOptions } from "./types";
+import { DubEmbedOptions, Options } from "./types";
-export const DubWidget = ({ token, onTokenExpired }: DubEmbedOptions) => {
+export const DubWidget = ({
+ token,
+ onTokenExpired,
+ ...options
+}: DubEmbedOptions & Partial<Options>) => {
// Listen for messages from the iframe
useEffect(() => {
const handleMessage = (event: MessageEvent) => {
@@ -25,7 +29,7 @@ export const DubWidget = ({ token, onTokenExpired }: DubEmbedOptions) => {
}, [onTokenExpired]);
useEffect(() => {
- init({ token });
+ init({ token, ...options });
return () => destroy();
}, []);