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 close button

Tim Wilsonc5600d5d60f1
diff --git a/packages/embeds/core/src/constants.ts b/packages/embeds/core/src/constants.ts
index ce2e5d659a..5c5b57a461 100644
--- a/packages/embeds/core/src/constants.ts
+++ b/packages/embeds/core/src/constants.ts
@@ -3,3 +3,25 @@ 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_CLOSE_BUTTON_ID = "dub-embed-close";
+
+export const GIFT_ICON =
+ `<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">` +
+ `<g fill="currentColor">` +
+ `<path d="M3,9.5v4.75c0,1.517,1.233,2.75,2.75,2.75h2.5v-7.5H3Z" />` +
+ `<path d="M9.75,9.5v7.5h2.5c1.517,0,2.75-1.233,2.75-2.75v-4.75h-5.25Z" />` +
+ `<path d="M15.25,4.5h-.462c.135-.307,.212-.644,.212-1,0-1.378-1.121-2.5-2.5-2.5-1.761,` +
+ `0-2.864,1.231-3.5,2.339-.636-1.107-1.739-2.339-3.5-2.339-1.379,0-2.5,1.122-2.5,2.5,0,` +
+ `.356,.077,.693,.212,1h-.462c-.965,0-1.75,.776-1.75,1.75s.785,1.75,1.75,1.75H15.25c.965,0` +
+ `,1.75-.782,1.75-1.75s-.785-1.75-1.75-1.75Zm-2.75-2c.552,0,1,.449,1,1s-.448,1-1,1h-2.419c.405` +
+ `-.86,1.176-2,2.419-2ZM4.5,3.5c0-.551,.448-1,1-1,1.234,0,2.007,1.14,2.415,2h-2.415c-.552,0-1-.449-1-1Z" />` +
+ `</g>` +
+ `</svg>`;
+
+export const CLOSE_ICON =
+ `<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">` +
+ `<g fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" stroke="currentColor">` +
+ `<line x1="14" y1="4" x2="4" y2="14" />` +
+ `<line x1="4" y1="4" x2="14" y2="14" />` +
+ `</g>` +
+ `</svg>`;
diff --git a/packages/embeds/core/src/core.ts b/packages/embeds/core/src/core.ts
index beb8ff8519..d04ca8529d 100644
--- a/packages/embeds/core/src/core.ts
+++ b/packages/embeds/core/src/core.ts
@@ -1,5 +1,7 @@
import {
+ CLOSE_ICON,
DASHBOARD_URL,
+ DUB_CLOSE_BUTTON_ID,
DUB_CONTAINER_ID,
DUB_POPUP_ID,
WIDGET_URL,
@@ -73,6 +75,14 @@ const POPUP_STYLES = (
pointerEvents: "auto",
});
+const CLOSE_BUTTON_STYLE = {
+ position: "absolute",
+ top: "24px",
+ right: "24px",
+ padding: "4px",
+ color: "white",
+};
+
let isWidgetOpen = false;
const createIframe = (iframeUrl: string, token: string): HTMLIFrameElement => {
@@ -191,7 +201,15 @@ const renderWidget = (options: DubOptions): HTMLElement | null => {
const iframe = createIframe(WIDGET_URL, token);
+ // Close button
+ const closeButton = document.createElement("button");
+ closeButton.id = DUB_CLOSE_BUTTON_ID;
+ closeButton.innerHTML = CLOSE_ICON;
+ Object.assign(closeButton.style, CLOSE_BUTTON_STYLE);
+ closeButton.addEventListener("click", () => closeWidget());
+
popup.appendChild(iframe);
+ popup.appendChild(closeButton);
container.appendChild(popup);
// Listen the message from the iframe
diff --git a/packages/embeds/core/src/floating-button.ts b/packages/embeds/core/src/floating-button.ts
index f771c95494..f37405b6b1 100644
--- a/packages/embeds/core/src/floating-button.ts
+++ b/packages/embeds/core/src/floating-button.ts
@@ -1,3 +1,4 @@
+import { GIFT_ICON } from "./constants";
import { DubWidgetPlacement } from "./types";
const DUB_FLOATING_BUTTON_ID = "dub-floating-button";
@@ -40,7 +41,7 @@ export const createFloatingButton = ({
...buttonStyles,
});
- button.innerHTML = giftIcon;
+ button.innerHTML = GIFT_ICON;
button.addEventListener("click", () => onClick());
@@ -65,16 +66,3 @@ export const createFloatingButton = ({
container.appendChild(button);
};
-
-const giftIcon =
- `<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">` +
- `<g fill="currentColor">` +
- `<path d="M3,9.5v4.75c0,1.517,1.233,2.75,2.75,2.75h2.5v-7.5H3Z" />` +
- `<path d="M9.75,9.5v7.5h2.5c1.517,0,2.75-1.233,2.75-2.75v-4.75h-5.25Z" />` +
- `<path d="M15.25,4.5h-.462c.135-.307,.212-.644,.212-1,0-1.378-1.121-2.5-2.5-2.5-1.761,` +
- `0-2.864,1.231-3.5,2.339-.636-1.107-1.739-2.339-3.5-2.339-1.379,0-2.5,1.122-2.5,2.5,0,` +
- `.356,.077,.693,.212,1h-.462c-.965,0-1.75,.776-1.75,1.75s.785,1.75,1.75,1.75H15.25c.965,0` +
- `,1.75-.782,1.75-1.75s-.785-1.75-1.75-1.75Zm-2.75-2c.552,0,1,.449,1,1s-.448,1-1,1h-2.419c.405` +
- `-.86,1.176-2,2.419-2ZM4.5,3.5c0-.551,.448-1,1-1,1.234,0,2.007,1.14,2.415,2h-2.415c-.552,0-1-.449-1-1Z" />` +
- `</g>` +
- `</svg>`;