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.

Move the holding period scroll fades into a shared hook and component

Rafidd470c4c3c3f
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/program-payout-holding-periods.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/program-payout-holding-periods.tsx
index 39ab445828..95e7eff78f 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/program-payout-holding-periods.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/payouts/program-payout-holding-periods.tsx
@@ -6,6 +6,7 @@ import { GroupProps } from "@/lib/types";
import { DEFAULT_PARTNER_GROUP } from "@/lib/zod/schemas/groups";
import { GroupColorCircle } from "@/ui/partners/groups/group-color-circle";
import { MarkdownDescription } from "@/ui/shared/markdown-description";
+import { ScrollFades, useScrollFades } from "@/ui/shared/scroll-fades";
import { StatusBadge, Switch } from "@dub/ui";
import { ChevronDown } from "lucide-react";
import Link from "next/link";
@@ -26,7 +27,6 @@ export type HoldingPeriodUpdate = {
type AllGroupsState = { enabled: false } | { enabled: true; days: number };
// Scroll distance (px) over which the top/bottom fades go from hidden to fully visible
-const FADE_SCROLL_DISTANCE = 40;
/**
* Stages holding period edits (per group, or one value for all groups) until
@@ -237,41 +237,13 @@ function HoldingPeriodsTable({
}) {
const { slug: workspaceSlug } = useWorkspace();
- const [fades, setFades] = useState({ top: 0, bottom: 0 });
-
- const updateFades = useCallback((el: HTMLElement) => {
- const maxScrollTop = el.scrollHeight - el.clientHeight;
- const top = Math.min(el.scrollTop / FADE_SCROLL_DISTANCE, 1);
- const bottom =
- maxScrollTop <= 0
- ? 0
- : Math.min((maxScrollTop - el.scrollTop) / FADE_SCROLL_DISTANCE, 1);
-
- setFades((prev) =>
- prev.top === top && prev.bottom === bottom ? prev : { top, bottom },
- );
- }, []);
-
- // Callback ref (rather than an effect): measures once the container mounts and
- // whenever it or its rows resize, e.g. when the skeleton is replaced by groups
- const scrollRef = useCallback(
- (el: HTMLDivElement | null) => {
- if (!el) return;
-
- const observer = new ResizeObserver(() => updateFades(el));
- observer.observe(el);
- if (el.firstElementChild) observer.observe(el.firstElementChild);
-
- return () => observer.disconnect();
- },
- [updateFades],
- );
+ const { fades, scrollRef, onScroll } = useScrollFades();
return (
<div className="relative overflow-hidden rounded-lg border border-neutral-200 bg-white">
<div
ref={scrollRef}
- onScroll={(e) => updateFades(e.currentTarget)}
+ onScroll={onScroll}
className="scrollbar-hide max-h-[268px] overflow-y-auto [clip-path:inset(0)]"
>
<div className="divide-y divide-neutral-200">
@@ -320,19 +292,7 @@ function HoldingPeriodsTable({
</div>
</div>
- {/* Top scroll fade */}
- <div
- aria-hidden
- className="pointer-events-none absolute inset-x-0 top-0 h-8 bg-gradient-to-b from-white to-transparent"
- style={{ opacity: fades.top }}
- />
-
- {/* Bottom scroll fade */}
- <div
- aria-hidden
- className="pointer-events-none absolute inset-x-0 bottom-0 h-20 bg-gradient-to-t from-white to-transparent"
- style={{ opacity: fades.bottom }}
- />
+ <ScrollFades fades={fades} />
</div>
);
}
diff --git a/apps/web/ui/shared/scroll-fades.tsx b/apps/web/ui/shared/scroll-fades.tsx
new file mode 100644
index 0000000000..35f78764bb
--- /dev/null
+++ b/apps/web/ui/shared/scroll-fades.tsx
@@ -0,0 +1,66 @@
+"use client";
+
+import { useCallback, useState } from "react";
+
+const FADE_SCROLL_DISTANCE = 40;
+
+// Fades at the top and bottom of a scroll container that show there is more content
+export function useScrollFades() {
+ const [fades, setFades] = useState({ top: 0, bottom: 0 });
+
+ const updateFades = useCallback((el: HTMLElement) => {
+ const maxScrollTop = el.scrollHeight - el.clientHeight;
+ const top = Math.min(el.scrollTop / FADE_SCROLL_DISTANCE, 1);
+ const bottom =
+ maxScrollTop <= 0
+ ? 0
+ : Math.min((maxScrollTop - el.scrollTop) / FADE_SCROLL_DISTANCE, 1);
+
+ setFades((prev) =>
+ prev.top === top && prev.bottom === bottom ? prev : { top, bottom },
+ );
+ }, []);
+
+ // Callback ref (rather than an effect): measures once the container mounts and
+ // whenever it or its rows resize, e.g. when the skeleton is replaced by rows
+ const scrollRef = useCallback(
+ (el: HTMLDivElement | null) => {
+ if (!el) return;
+
+ const observer = new ResizeObserver(() => updateFades(el));
+ observer.observe(el);
+ if (el.firstElementChild) observer.observe(el.firstElementChild);
+
+ return () => observer.disconnect();
+ },
+ [updateFades],
+ );
+
+ return {
+ fades,
+ scrollRef,
+ onScroll: (e: React.UIEvent<HTMLElement>) => updateFades(e.currentTarget),
+ };
+}
+
+// Place inside a relative parent of the scroll container
+export function ScrollFades({
+ fades,
+}: {
+ fades: ReturnType<typeof useScrollFades>["fades"];
+}) {
+ return (
+ <>
+ <div
+ aria-hidden
+ className="pointer-events-none absolute inset-x-0 top-0 h-8 bg-gradient-to-b from-white to-transparent"
+ style={{ opacity: fades.top }}
+ />
+ <div
+ aria-hidden
+ className="pointer-events-none absolute inset-x-0 bottom-0 h-20 bg-gradient-to-t from-white to-transparent"
+ style={{ opacity: fades.bottom }}
+ />
+ </>
+ );
+}