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.

Replace floating popup w/ modal

Tim Wilsonfd5a48c0702d
diff --git a/apps/web/ui/layout/sidebar/refer-button.tsx b/apps/web/ui/layout/sidebar/refer-button.tsx
index b4fe5bd8df..57a8f34a8d 100644
--- a/apps/web/ui/layout/sidebar/refer-button.tsx
+++ b/apps/web/ui/layout/sidebar/refer-button.tsx
@@ -41,11 +41,7 @@ export function ReferButton() {
if (!flags?.referrals || !referralLinkId || !publicToken) return null;
return (
- <DubWidget
- token={publicToken}
- trigger="manual"
- placement={isMobile ? "center" : "top-left"}
- >
+ <DubWidget token={publicToken} trigger="manual">
<button
type="button"
className={cn(
diff --git a/packages/embeds/core/src/constants.ts b/packages/embeds/core/src/constants.ts
index befbc6607b..49201445aa 100644
--- a/packages/embeds/core/src/constants.ts
+++ b/packages/embeds/core/src/constants.ts
@@ -3,8 +3,8 @@ export const WIDGET_URL = `http://localhost:8888/embed/widget`;
export const DUB_CONTAINER_ID = "dub-embed-container";
export const DUB_POPUP_ID = "dub-embed-popup";
-export const DUB_POPUP_BACKDROP_ID = "dub-embed-backdrop";
export const DUB_CLOSE_BUTTON_ID = "dub-embed-close";
+export const DUB_FLOATING_BUTTON_ID = "dub-floating-button";
export const GIFT_ICON =
`<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">` +
diff --git a/packages/embeds/core/src/core.ts b/packages/embeds/core/src/core.ts
index 8f7e33748b..b402ebd661 100644
--- a/packages/embeds/core/src/core.ts
+++ b/packages/embeds/core/src/core.ts
@@ -2,87 +2,35 @@ import {
CLOSE_ICON,
DUB_CLOSE_BUTTON_ID,
DUB_CONTAINER_ID,
- DUB_POPUP_BACKDROP_ID,
+ DUB_FLOATING_BUTTON_ID,
DUB_POPUP_ID,
WIDGET_URL,
} from "./constants";
import { EmbedError } from "./error";
import { createFloatingButton } from "./floating-button";
-import {
- DubInitResult,
- DubOptions,
- DubWidgetPlacement,
- IframeMessage,
-} from "./types";
-
-const CONTAINER_STYLES = ({
- placement,
-}: {
- placement: DubWidgetPlacement;
-}): Partial<CSSStyleDeclaration> => ({
+import { DubInitResult, DubOptions, IframeMessage } from "./types";
+
+const CONTAINER_STYLES: Partial<CSSStyleDeclaration> = {
position: "fixed",
display: "flex",
- ...{
- "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],
+ alignItems: "center",
+ justifyContent: "center",
+ inset: "0",
+ padding: "16px",
+ zIndex: "9998",
+ backgroundColor: "rgba(115, 115, 115, 0.3)",
+};
+
+const POPUP_STYLES: Partial<CSSStyleDeclaration> = {
width: "100%",
+ height: "100%",
maxWidth: "400px",
- zIndex: "9998",
- pointerEvents: "none",
-});
-
-const POPUP_STYLES = ({
- placement,
-}: {
- placement: DubWidgetPlacement;
-}): Partial<CSSStyleDeclaration> => ({
- width: "calc(100% - 32px)",
- height: "100dvh",
- maxHeight: "532px",
+ maxHeight: "500px",
backgroundColor: "white",
borderRadius: "12px",
boxShadow: "0px 8px 30px rgba(0, 0, 0, 0.12), 0px 2px 4px rgba(0, 0, 0, 0.1)",
- margin: "16px",
overflow: "hidden",
- ...{
- "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 CLOSE_BUTTON_STYLES = {
position: "absolute",
@@ -92,14 +40,6 @@ const CLOSE_BUTTON_STYLES = {
color: "white",
};
-const BACKDROP_STYLES: Partial<CSSStyleDeclaration> = {
- position: "fixed",
- inset: "0",
- zIndex: "9997",
- backgroundColor: "rgba(115, 115, 115, 0.3)",
- pointerEvents: "none",
-};
-
const createIframe = (iframeUrl: string, token: string): HTMLIFrameElement => {
const iframe = document.createElement("iframe");
@@ -124,7 +64,6 @@ const renderWidget = (
const {
token,
- placement,
containerStyles,
popupStyles,
onClose,
@@ -150,31 +89,22 @@ const renderWidget = (
const container = document.createElement("div");
container.id = `${prefix}${DUB_CONTAINER_ID}`;
Object.assign(container.style, {
- ...CONTAINER_STYLES({
- placement: placement ?? "bottom-right",
- }),
+ ...CONTAINER_STYLES,
...containerStyles,
- });
-
- const backdrop = document.createElement("div");
- backdrop.id = `${prefix}${DUB_POPUP_BACKDROP_ID}`;
- Object.assign(backdrop.style, {
- ...BACKDROP_STYLES,
display: "none",
});
- document.body.appendChild(backdrop);
+ container.addEventListener("click", (e) => {
+ if (e.target === container) closeWidget();
+ });
const popup: HTMLElement =
container.querySelector("#" + CSS.escape(`${prefix}${DUB_POPUP_ID}`)) ??
document.createElement("div");
popup.id = `${prefix}${DUB_POPUP_ID}`;
Object.assign(popup.style, {
- ...POPUP_STYLES({
- placement: placement ?? "bottom-right",
- }),
+ ...POPUP_STYLES,
...popupStyles,
- display: "none",
});
const iframe = createIframe(WIDGET_URL, token);
@@ -219,90 +149,76 @@ const renderWidget = (
* Checks if the widget is open.
*/
const isWidgetOpen = (prefix?: string) => (): boolean => {
- const popup = document.getElementById(`${prefix}${DUB_POPUP_ID}`);
- return popup?.style.display === "block";
+ const container = document.getElementById(`${prefix}${DUB_CONTAINER_ID}`);
+ return container?.style.display !== "none";
};
/**
* Opens the widget.
*/
-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;
- }
+const openWidget = (prefix?: string) => (): void => {
+ const popup = document.getElementById(`${prefix}${DUB_POPUP_ID}`);
+ const container = document.getElementById(`${prefix}${DUB_CONTAINER_ID}`);
+ if (!popup) {
+ console.error("[Dub] No popup found.");
+ return;
+ }
- 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)",
- },
- ],
+ if (container) {
+ container.style.display = "flex";
+ container.animate(
+ [
{
- duration: 250,
- easing: "ease-out",
- fill: "forwards",
+ backgroundColor: "rgba(115, 115, 115, 0)",
+ },
+ {
+ backgroundColor: "rgba(115, 115, 115, 0.3)",
},
- );
- }
-
- updatePosition?.();
-
- popup.animate(
- [
- { transform: "translateY(10px)", opacity: 0 },
- { transform: "translateY(0)", opacity: 1 },
],
{
duration: 250,
- easing: "ease-in-out",
+ easing: "ease-out",
fill: "forwards",
},
);
- };
+ }
+
+ popup.getAnimations().forEach((a) => a.cancel());
+ 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 = (prefix?: string) => (): void => {
const popup = document.getElementById(`${prefix}${DUB_POPUP_ID}`);
- const backdrop = document.getElementById(`${prefix}${DUB_POPUP_BACKDROP_ID}`);
+ const container = document.getElementById(`${prefix}${DUB_CONTAINER_ID}`);
if (!popup) return;
popup.getAnimations().forEach((a) => a.cancel());
- const animation = popup.animate(
- [{ transform: "translateY(10px)", opacity: 0 }],
- {
- duration: 100,
- easing: "ease-in-out",
- fill: "forwards",
- },
- );
+ popup.animate([{ transform: "translateY(10px)", opacity: 0 }], {
+ duration: 100,
+ easing: "ease-in-out",
+ fill: "forwards",
+ });
- if (backdrop) {
- backdrop.animate(
+ if (container) {
+ container.animate(
[
{
- opacity: 1,
backgroundColor: "rgba(115, 115, 115, 0.3)",
},
{
- opacity: 0,
backgroundColor: "rgba(115, 115, 115, 0)",
},
],
@@ -312,31 +228,24 @@ const closeWidget = (prefix?: string) => (): void => {
fill: "forwards",
},
).onfinish = () => {
- backdrop.style.display = "none";
+ container.style.display = "none";
};
}
-
- animation.onfinish = () => {
- if (popup?.isConnected) popup.style.display = "none";
- };
};
/**
* Toggles the widget open state.
*/
-const toggleWidget =
- (prefix?: string, updatePosition?: () => void) => (): void => {
- isWidgetOpen(prefix)()
- ? closeWidget(prefix)()
- : openWidget(prefix, updatePosition)();
- };
+const toggleWidget = (prefix?: string) => (): void => {
+ isWidgetOpen(prefix)() ? closeWidget(prefix)() : openWidget(prefix)();
+};
/**
* Initializes the Dub embed widget.
*/
export const init = (options: DubOptions): DubInitResult => {
options.trigger = options.trigger ?? "floating-button";
- options.placement = options.placement ?? "bottom-right";
+ options.buttonPlacement = options.buttonPlacement ?? "bottom-right";
console.debug("[Dub] Initializing.", options);
@@ -354,9 +263,8 @@ export const init = (options: DubOptions): DubInitResult => {
if (options.trigger === "floating-button") {
createFloatingButton({
prefix,
- container: container,
buttonStyles: options.buttonStyles,
- placement: options.placement,
+ buttonPlacement: options.buttonPlacement,
onClick: () => {
toggleWidget(prefix)();
},
@@ -369,9 +277,8 @@ export const init = (options: DubOptions): DubInitResult => {
toggleWidget: toggleWidget(prefix),
isWidgetOpen: isWidgetOpen(prefix),
destroy: () => {
- document
- .querySelectorAll("#" + CSS.escape(`${prefix}${DUB_CONTAINER_ID}`))
- .forEach((c) => c.remove());
+ document.getElementById(`${prefix}${DUB_CONTAINER_ID}`)?.remove();
+ document.getElementById(`${prefix}${DUB_FLOATING_BUTTON_ID}`)?.remove();
},
};
};
diff --git a/packages/embeds/core/src/floating-button.ts b/packages/embeds/core/src/floating-button.ts
index 2a5d69d181..a0378d5934 100644
--- a/packages/embeds/core/src/floating-button.ts
+++ b/packages/embeds/core/src/floating-button.ts
@@ -1,45 +1,53 @@
-import { GIFT_ICON } from "./constants";
-import { DubWidgetPlacement } from "./types";
-
-const DUB_FLOATING_BUTTON_ID = "dub-floating-button";
+import { DUB_FLOATING_BUTTON_ID, GIFT_ICON } from "./constants";
+import { DubFloatingButtonPlacement } from "./types";
const FLOATING_BUTTON_STYLES = (
- placement: DubWidgetPlacement,
+ buttonPlacement: DubFloatingButtonPlacement,
): 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",
borderRadius: "9999px",
border: "none",
transition: "transform 0.1s ease-in-out",
- pointerEvents: "auto",
+ position: "fixed",
+ ...{
+ "top-left": {
+ top: "0",
+ left: "0",
+ },
+ "top-right": {
+ top: "0",
+ right: "0",
+ },
+ "bottom-left": {
+ bottom: "0",
+ left: "0",
+ },
+ "bottom-right": {
+ bottom: "0",
+ right: "0",
+ },
+ }[buttonPlacement],
+ zIndex: "9997",
});
export const createFloatingButton = ({
prefix,
- container,
buttonStyles,
- placement,
+ buttonPlacement,
onClick,
}: {
prefix?: string;
- container: HTMLElement;
buttonStyles?: Record<string, any>;
- placement: DubWidgetPlacement;
+ buttonPlacement: DubFloatingButtonPlacement;
onClick: () => void;
}): void => {
const button = document.createElement("button");
button.id = `${prefix}${DUB_FLOATING_BUTTON_ID}`;
Object.assign(button.style, {
- ...FLOATING_BUTTON_STYLES(placement),
+ ...FLOATING_BUTTON_STYLES(buttonPlacement),
...buttonStyles,
});
@@ -66,5 +74,5 @@ export const createFloatingButton = ({
() => (button.style.transform = "scale(1.1)"),
);
- container.appendChild(button);
+ document.body.appendChild(button);
};
diff --git a/packages/embeds/core/src/types.ts b/packages/embeds/core/src/types.ts
index c65eb1ad18..a47f607b54 100644
--- a/packages/embeds/core/src/types.ts
+++ b/packages/embeds/core/src/types.ts
@@ -32,8 +32,8 @@ export type DubOptions = {
// The token expired
onTokenExpired?: () => void;
- // The placement of the widget
- placement?: DubWidgetPlacement;
+ // The placement of the floating button
+ buttonPlacement?: DubFloatingButtonPlacement;
// The styles for the widget container
containerStyles?: Partial<CSSStyleDeclaration>;
@@ -43,12 +43,11 @@ export type DubOptions = {
export type DubWidgetTrigger = "floating-button" | "manual";
-export type DubWidgetPlacement =
+export type DubFloatingButtonPlacement =
| "bottom-right"
| "bottom-left"
| "top-right"
- | "top-left"
- | "center";
+ | "top-left";
export interface IframeMessage {
originator?: "Dub";
diff --git a/packages/embeds/react/src/widget.tsx b/packages/embeds/react/src/widget.tsx
index 9703c29903..5c3751093b 100644
--- a/packages/embeds/react/src/widget.tsx
+++ b/packages/embeds/react/src/widget.tsx
@@ -23,7 +23,6 @@ export const DubWidget = memo(
init({
id,
...options,
- anchorId: children && options.placement !== "center" ? id : undefined,
}) || {};
toggleWidgetRef.current = toggleWidget;