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.

useIframeVisibility

Steven Tey75f38dc56818
diff --git a/apps/web/app/app.dub.co/embed/inline/page-client.tsx b/apps/web/app/app.dub.co/embed/inline/page-client.tsx
index b133532312..8af147a79f 100644
--- a/apps/web/app/app.dub.co/embed/inline/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/inline/page-client.tsx
@@ -10,16 +10,16 @@ import {
Copy,
MoneyBill2,
useCopyToClipboard,
- useInViewport,
Wordmark,
} from "@dub/ui";
import { cn, fetcher, getPrettyUrl } from "@dub/utils";
import { Link, Program } from "@prisma/client";
-import { CSSProperties, useRef } from "react";
+import { CSSProperties } from "react";
import useSWR from "swr";
import { Activity } from "../activity";
import { SalesList } from "../sales-list";
import { LinkToken } from "../token";
+import { useIframeVisibility } from "../use-iframe-visibility";
export function EmbedInlinePageClient({
program,
@@ -34,17 +34,15 @@ export function EmbedInlinePageClient({
}) {
const [copied, copyToClipboard] = useCopyToClipboard();
- const divRef = useRef<HTMLDivElement>(null);
- const isVisible = useInViewport(divRef);
+ const isIframeVisible = useIframeVisibility();
const { data: sales, isLoading } = useSWR<PartnerSaleResponse[]>(
- isVisible && "/api/embed/sales",
+ isIframeVisible && "/api/embed/sales",
fetcher,
);
return (
<div
- ref={divRef}
className="flex min-h-screen flex-col"
style={
{ "--accent-color": program.brandColor || "#171717" } as CSSProperties
@@ -147,7 +145,7 @@ export function EmbedInlinePageClient({
))}
</div>
</>
- {isVisible && <LinkToken />}
+ {isIframeVisible && <LinkToken />}
</div>
</div>
<div className="flex grow flex-col justify-end">
diff --git a/apps/web/app/app.dub.co/embed/use-iframe-visibility.ts b/apps/web/app/app.dub.co/embed/use-iframe-visibility.ts
new file mode 100644
index 0000000000..3a81171104
--- /dev/null
+++ b/apps/web/app/app.dub.co/embed/use-iframe-visibility.ts
@@ -0,0 +1,66 @@
+import { useEffect, useState } from "react";
+
+export function useIframeVisibility() {
+ const [isIframeVisible, setIsIframeVisible] = useState(false);
+
+ useEffect(() => {
+ const isInIframe = window !== window.parent;
+
+ if (isInIframe) {
+ const isInitiallyVisible =
+ document.visibilityState === "visible" && !document.hidden;
+ setIsIframeVisible(false);
+
+ const handleVisibilityChange = () => {
+ const isVisible =
+ document.visibilityState === "visible" && !document.hidden;
+ setIsIframeVisible(isVisible);
+ };
+
+ const handleFocus = () => {
+ if (document.visibilityState === "visible" && !document.hidden) {
+ setIsIframeVisible(true);
+ }
+ };
+
+ const handleBlur = () => {
+ setIsIframeVisible(false);
+ };
+
+ document.addEventListener("visibilitychange", handleVisibilityChange);
+ window.addEventListener("focus", handleFocus);
+ window.addEventListener("blur", handleBlur);
+ document.addEventListener("focus", handleFocus);
+ document.addEventListener("blur", handleBlur);
+
+ const observer = new IntersectionObserver(
+ ([entry]) => {
+ if (!entry.isIntersecting) {
+ setIsIframeVisible(false);
+ }
+ },
+ { threshold: [0] },
+ );
+
+ if (window.frameElement) {
+ observer.observe(window.frameElement);
+ }
+
+ return () => {
+ document.removeEventListener(
+ "visibilitychange",
+ handleVisibilityChange,
+ );
+ window.removeEventListener("focus", handleFocus);
+ window.removeEventListener("blur", handleBlur);
+ document.removeEventListener("focus", handleFocus);
+ document.removeEventListener("blur", handleBlur);
+ observer.disconnect();
+ };
+ } else {
+ setIsIframeVisible(true);
+ }
+ }, []);
+
+ return isIframeVisible;
+}
diff --git a/apps/web/app/app.dub.co/embed/widget/page-client.tsx b/apps/web/app/app.dub.co/embed/widget/page-client.tsx
index 2e19815965..15dad6fd0e 100644
--- a/apps/web/app/app.dub.co/embed/widget/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/widget/page-client.tsx
@@ -7,7 +7,6 @@ import {
buttonVariants,
ToggleGroup,
useCopyToClipboard,
- useInViewport,
Wordmark,
} from "@dub/ui";
import {
@@ -22,11 +21,12 @@ import {
import { cn, fetcher, getPrettyUrl } from "@dub/utils";
import { Link, Program } from "@prisma/client";
import { motion } from "framer-motion";
-import { CSSProperties, useRef, useState } from "react";
+import { CSSProperties, useState } from "react";
import useSWR from "swr";
import { Activity } from "../activity";
import { SalesList } from "../sales-list";
import { LinkToken } from "../token";
+import { useIframeVisibility } from "../use-iframe-visibility";
type Tab = "invite" | "rewards";
@@ -46,17 +46,15 @@ export function EmbedWidgetPageClient({
const [copied, copyToClipboard] = useCopyToClipboard();
const [selectedTab, setSelectedTab] = useState<Tab>("invite");
- const divRef = useRef<HTMLDivElement>(null);
- const isVisible = useInViewport(divRef);
+ const isIframeVisible = useIframeVisibility();
const { data: sales, isLoading } = useSWR<PartnerSaleResponse[]>(
- isVisible && "/api/embed/sales",
+ isIframeVisible && "/api/embed/sales",
fetcher,
);
return (
<div
- ref={divRef}
className="flex min-h-screen flex-col"
style={
{ "--accent-color": program.brandColor || "#171717" } as CSSProperties
@@ -261,7 +259,7 @@ export function EmbedWidgetPageClient({
</motion.div>
</>
)}
- {isVisible && <LinkToken />}
+ {isIframeVisible && <LinkToken />}
</div>
</div>
<div className="flex grow flex-col justify-end">