🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor components to utilize ScrollArea for improved scrolling behavior and layout consistency
Bill Yang4fa41638ec2d
diff --git a/client/src/app/[site]/api-playground/components/EndpointList.tsx b/client/src/app/[site]/api-playground/components/EndpointList.tsxindex eb7cce6d8..ba8167f78 100644--- a/client/src/app/[site]/api-playground/components/EndpointList.tsx+++ b/client/src/app/[site]/api-playground/components/EndpointList.tsx@@ -3,12 +3,13 @@import { cn } from "@/lib/utils";import { endpointCategories, EndpointConfig, methodColors } from "../utils/endpointConfig";import { usePlaygroundStore } from "../hooks/usePlaygroundStore";+import { ScrollArea } from "../../../../components/ui/scroll-area";export function EndpointList() {const { selectedEndpoint, setSelectedEndpoint } = usePlaygroundStore();return (- <div className="h-full overflow-y-auto border-r border-neutral-100 dark:border-neutral-850">+ <ScrollArea className="h-full overflow-y-auto border-r border-neutral-100 dark:border-neutral-850"><div className="p-3 border-b border-neutral-100 dark:border-neutral-850"><h2 className="text-sm font-semibold text-neutral-900 dark:text-neutral-100">Endpoints</h2></div>@@ -31,7 +32,7 @@ export function EndpointList() {</div>))}</div>- </div>+ </ScrollArea>);}diff --git a/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx b/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsxindex c46ad0e5b..19712ca0c 100644--- a/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx+++ b/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx@@ -8,6 +8,7 @@ import { usePaginatedMetric } from "../../../../../api/analytics/hooks/useGetMetimport { MetricResponse } from "../../../../../api/analytics/endpoints";import { ErrorState } from "../../../../../components/ErrorState";import { CardLoader } from "../../../../../components/ui/card";+import { ScrollArea } from "../../../../../components/ui/scroll-area";import { Tooltip, TooltipContent, TooltipTrigger } from "../../../../../components/ui/tooltip";import { IS_CLOUD } from "../../../../../lib/const";import { Row } from "./Row";@@ -86,56 +87,58 @@ export function StandardSection({</div><div>{countLabel || "Sessions"}</div></div>- <div className="flex flex-col gap-2 max-h-[314px] overflow-y-auto overflow-x-hidden">- {isLoading ? (- <StandardSkeleton />- ) : error ? (- <ErrorState title="Failed to load data" message={error.message} refetch={refetch} />- ) : (- <>- {itemsForDisplay?.length ? (- itemsForDisplay- // .slice(0, MAX_ITEMS_TO_DISPLAY)- .map(e => (- <Row- key={getKey(e)}- e={e}- ratio={ratio}- getKey={getKey}- getLabel={getLabel}- getValue={getValue}- getLink={getLink}- filterParameter={filterParameter}- getSubrowLabel={getSubrowLabel}- hasSubrow={hasSubrow}- />- ))- ) : (- <div className="text-neutral-600 dark:text-neutral-300 w-full text-center mt-6 flex flex-row gap-2 items-center justify-center">- <Info className="w-5 h-5" />- No Data- </div>- )}- </>- )}- {!isLoading && !error && itemsForDisplay?.length ? (- <div className="flex flex-row gap-2 justify-between items-center">- <StandardSectionDialog- title={title}- ratio={ratio}- getKey={getKey}- getLabel={getLabel}- getValue={getValue}- getFilterLabel={getFilterLabel}- getLink={getLink}- countLabel={countLabel}- filterParameter={filterParameter}- expanded={expanded}- close={close}- />- </div>- ) : null}- </div>+ <ScrollArea className="h-[314px]">+ <div className="flex flex-col gap-2">+ {isLoading ? (+ <StandardSkeleton />+ ) : error ? (+ <ErrorState title="Failed to load data" message={error.message} refetch={refetch} />+ ) : (+ <>+ {itemsForDisplay?.length ? (+ itemsForDisplay+ // .slice(0, MAX_ITEMS_TO_DISPLAY)+ .map(e => (+ <Row+ key={getKey(e)}+ e={e}+ ratio={ratio}+ getKey={getKey}+ getLabel={getLabel}+ getValue={getValue}+ getLink={getLink}+ filterParameter={filterParameter}+ getSubrowLabel={getSubrowLabel}+ hasSubrow={hasSubrow}+ />+ ))+ ) : (+ <div className="text-neutral-600 dark:text-neutral-300 w-full text-center mt-6 flex flex-row gap-2 items-center justify-center">+ <Info className="w-5 h-5" />+ No Data+ </div>+ )}+ </>+ )}+ {!isLoading && !error && itemsForDisplay?.length ? (+ <div className="flex flex-row gap-2 justify-between items-center">+ <StandardSectionDialog+ title={title}+ ratio={ratio}+ getKey={getKey}+ getLabel={getLabel}+ getValue={getValue}+ getFilterLabel={getFilterLabel}+ getLink={getLink}+ countLabel={countLabel}+ filterParameter={filterParameter}+ expanded={expanded}+ close={close}+ />+ </div>+ ) : null}+ </div>+ </ScrollArea></>);}diff --git a/client/src/app/[site]/events/components/EventLog.tsx b/client/src/app/[site]/events/components/EventLog.tsxindex f5e1a27a1..e1ea4a259 100644--- a/client/src/app/[site]/events/components/EventLog.tsx+++ b/client/src/app/[site]/events/components/EventLog.tsx@@ -6,6 +6,7 @@ import { NothingFound } from "../../../../components/NothingFound";import { formatter } from "../../../../lib/utils";import { EventLogItem, EventLogItemSkeleton } from "./EventLogItem";import { ErrorState } from "../../../../components/ErrorState";+import { ScrollArea } from "../../../../components/ui/scroll-area";export function EventLog() {const containerRef = useRef<HTMLDivElement>(null);@@ -71,9 +72,8 @@ export function EventLog() {}return (- <div className="space-y-1">- {/* Event list */}- <div ref={containerRef} className="max-h-[80vh] overflow-y-auto pr-2">+ <ScrollArea className="h-[80vh]">+ <div ref={containerRef} className="h-full pr-2">{allEvents.map((event, index) => (<EventLogItem key={`${event.timestamp}-${index}`} event={event} />))}@@ -96,6 +96,6 @@ export function EventLog() {Showing {allEvents.length} of {formatter(data.pages[0].pagination.total)} events</div>)}- </div>+ </ScrollArea>);}diff --git a/client/src/app/[site]/events/components/OutboundLinksList.tsx b/client/src/app/[site]/events/components/OutboundLinksList.tsxindex 47101ce38..80e10d5be 100644--- a/client/src/app/[site]/events/components/OutboundLinksList.tsx+++ b/client/src/app/[site]/events/components/OutboundLinksList.tsx@@ -7,6 +7,7 @@ import { memo } from "react";import { OutboundLink } from "../../../../api/analytics/endpoints";import { Favicon } from "../../../../components/Favicon";import { cn } from "../../../../lib/utils";+import { ScrollArea } from "../../../../components/ui/scroll-area";// Skeleton component for OutboundLinksListconst OutboundLinksListSkeleton = memo(({ size = "small" }: { size?: "small" | "large" }) => {@@ -114,54 +115,59 @@ export function OutboundLinksList({ outboundLinks, isLoading, size = "small" }:const maxCount = Math.max(...outboundLinks.map(link => link.count));return (- <div className="flex flex-col gap-2 overflow-y-auto max-h-[60vh] h-auto lg:h-full lg:min-h-0 lg:max-h-full">- {outboundLinks.map((link, index) => {- const percentageOfMax = (link.count / maxCount) * 100;- const percentage = (link.count / totalCount) * 100;- const lastClicked = DateTime.fromSQL(link.lastClicked, {- zone: "utc",- }).toLocal();+ <ScrollArea className="h-[394px]">+ <div className="flex flex-col gap-2 pr-2">+ {outboundLinks.map((link, index) => {+ const percentageOfMax = (link.count / maxCount) * 100;+ const percentage = (link.count / totalCount) * 100;+ const lastClicked = DateTime.fromSQL(link.lastClicked, {+ zone: "utc",+ }).toLocal();- return (- <div- key={`${link.url}-${index}`}- className={cn(- "relative flex items-center hover:bg-neutral-100 dark:hover:bg-neutral-850 group px-2 rounded-md",- size === "small" ? "h-6" : "h-9"- )}- >- <div- className="absolute inset-0 bg-dataviz py-2 opacity-25 rounded-md"- style={{ width: `${percentageOfMax}%` }}- ></div>+ return (<div- className={cn("z-10 flex justify-between items-center w-full", size === "small" ? "text-xs" : "text-sm")}+ key={`${link.url}-${index}`}+ className={cn(+ "relative flex items-center hover:bg-neutral-100 dark:hover:bg-neutral-850 group px-2 rounded-md",+ size === "small" ? "h-6" : "h-9"+ )}>- <div className="font-medium truncate max-w-[70%] flex items-center gap-1">- <Favicon domain={new URL(link.url).hostname} className="w-4 mr-1" />- <a- href={link.url}- target="_blank"- rel="noopener noreferrer"- className="text-neutral-900 dark:text-neutral-100 hover:underline truncate"- title={link.url}- >- {truncateUrl(link.url)}- </a>- </div>- <div className={cn("text-sm flex gap-2 items-center", size === "small" ? "text-xs" : "text-sm")}>- <div className="hidden group-hover:block text-neutral-600 dark:text-neutral-400 text-xs">- {Math.round(percentage * 10) / 10}%+ <div+ className="absolute inset-0 bg-dataviz py-2 opacity-25 rounded-md"+ style={{ width: `${percentageOfMax}%` }}+ ></div>+ <div+ className={cn(+ "z-10 flex justify-between items-center w-full",+ size === "small" ? "text-xs" : "text-sm"+ )}+ >+ <div className="font-medium truncate max-w-[70%] flex items-center gap-1">+ <Favicon domain={new URL(link.url).hostname} className="w-4 mr-1" />+ <a+ href={link.url}+ target="_blank"+ rel="noopener noreferrer"+ className="text-neutral-900 dark:text-neutral-100 hover:underline truncate"+ title={link.url}+ >+ {truncateUrl(link.url)}+ </a></div>- <div className="hidden group-hover:block text-neutral-600 dark:text-neutral-400 text-xs">- {lastClicked.toRelative()}+ <div className={cn("text-sm flex gap-2 items-center", size === "small" ? "text-xs" : "text-sm")}>+ <div className="hidden group-hover:block text-neutral-600 dark:text-neutral-400 text-xs">+ {Math.round(percentage * 10) / 10}%+ </div>+ {/* <div className="hidden group-hover:block text-neutral-600 dark:text-neutral-400 text-xs">+ {lastClicked.toRelative()}+ </div> */}+ <NumberFlow respectMotionPreference={false} value={link.count} format={{ notation: "compact" }} /></div>- <NumberFlow respectMotionPreference={false} value={link.count} format={{ notation: "compact" }} /></div></div>- </div>- );- })}- </div>+ );+ })}+ </div>+ </ScrollArea>);}diff --git a/client/src/app/[site]/main/components/sections/Events.tsx b/client/src/app/[site]/main/components/sections/Events.tsxindex aa404f41c..1ad21da2f 100644--- a/client/src/app/[site]/main/components/sections/Events.tsx+++ b/client/src/app/[site]/main/components/sections/Events.tsx@@ -8,6 +8,7 @@ import { OutboundLinksList } from "../../../events/components/OutboundLinksList"import { OutboundLinksDialog } from "./OutboundLinksDialog";import { useGetOutboundLinks } from "../../../../../api/analytics/hooks/events/useGetOutboundLinks";import { Expand } from "lucide-react";+import { ScrollArea } from "../../../../../components/ui/scroll-area";type Tab = "events" | "outbound";@@ -21,12 +22,14 @@ function Events_() {<CardLoader /></div>)}- <div className="relative max-h-[420px] overflow-y-auto">+ <div className="relative pr-2"><div className="flex flex-row gap-2 justify-between pr-1 text-xs text-neutral-600 dark:text-neutral-400 mb-2"><div>Custom Events</div><div>Count</div></div>- <EventList events={eventNamesData || []} isLoading={isLoadingEventNames} />+ <ScrollArea className="h-[394px]">+ <EventList events={eventNamesData || []} isLoading={isLoadingEventNames} />+ </ScrollArea></div></>);diff --git a/client/src/components/ui/hooks/useTouchPrimary.ts b/client/src/components/ui/hooks/useTouchPrimary.tsnew file mode 100644index 000000000..76fdc5cc1--- /dev/null+++ b/client/src/components/ui/hooks/useTouchPrimary.ts@@ -0,0 +1,28 @@+import { useEffect, useState } from "react";++export function useTouchPrimary() {+ const [isTouchPrimary, setIsTouchPrimary] = useState(false);++ useEffect(() => {+ if (typeof window === "undefined") return;++ const controller = new AbortController();+ const { signal } = controller;++ const handleTouch = () => {+ const hasTouch = "ontouchstart" in window || navigator.maxTouchPoints > 0;+ const prefersTouch = window.matchMedia("(pointer: coarse)").matches;+ setIsTouchPrimary(hasTouch && prefersTouch);+ };++ const mq = window.matchMedia("(pointer: coarse)");+ mq.addEventListener("change", handleTouch, { signal });+ window.addEventListener("pointerdown", handleTouch, { signal });++ handleTouch();++ return () => controller.abort();+ }, []);++ return isTouchPrimary;+}diff --git a/client/src/components/ui/scroll-area.tsx b/client/src/components/ui/scroll-area.tsxindex 667abc69d..0f0ae4229 100644--- a/client/src/components/ui/scroll-area.tsx+++ b/client/src/components/ui/scroll-area.tsx@@ -1,48 +1,219 @@"use client";import * as React from "react";+import * as ScrollAreaPrimitive from "@radix-ui/react-scroll-area";import { cn } from "@/lib/utils";+import { useTouchPrimary } from "./hooks/useTouchPrimary";++const ScrollAreaContext = React.createContext<boolean>(false);+type Mask = {+ top: boolean;+ bottom: boolean;+ left: boolean;+ right: boolean;+};++const ScrollArea = React.forwardRef<+ React.ComponentRef<typeof ScrollAreaPrimitive.Root>,+ React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root> & {+ viewportClassName?: string;+ /**+ * `maskHeight` is the height of the mask in pixels.+ * pass `0` to disable the mask+ * @default 30+ */+ maskHeight?: number;+ maskClassName?: string;+ }+>(({ className, children, scrollHideDelay = 0, viewportClassName, maskClassName, maskHeight = 30, ...props }, ref) => {+ const [showMask, setShowMask] = React.useState<Mask>({+ top: false,+ bottom: false,+ left: false,+ right: false,+ });+ const viewportRef = React.useRef<HTMLDivElement>(null);+ const isTouch = useTouchPrimary();++ const checkScrollability = React.useCallback(() => {+ const element = viewportRef.current;+ if (!element) return;++ const { scrollTop, scrollLeft, scrollWidth, clientWidth, scrollHeight, clientHeight } = element;+ setShowMask(prev => ({+ ...prev,+ top: scrollTop > 0,+ bottom: scrollTop + clientHeight < scrollHeight - 1,+ left: scrollLeft > 0,+ right: scrollLeft + clientWidth < scrollWidth - 1,+ }));+ }, []);++ React.useEffect(() => {+ if (typeof window === "undefined") return;++ const element = viewportRef.current;+ if (!element) return;++ const controller = new AbortController();+ const { signal } = controller;++ const resizeObserver = new ResizeObserver(checkScrollability);+ resizeObserver.observe(element);++ element.addEventListener("scroll", checkScrollability, { signal });+ window.addEventListener("resize", checkScrollability, { signal });++ // Run an initial check whenever dependencies change (including pointer mode)+ checkScrollability();++ return () => {+ controller.abort();+ resizeObserver.disconnect();+ };+ }, [checkScrollability, isTouch]);-function ScrollArea({ className, children, ...props }: React.ComponentProps<typeof ScrollAreaPrimitive.Root>) {return (- <ScrollAreaPrimitive.Root data-slot="scroll-area" className={cn("relative", className)} {...props}>- <ScrollAreaPrimitive.Viewport- data-slot="scroll-area-viewport"- className="focus-visible:ring-ring/50 size-full rounded-[inherit] transition-[color,box-shadow] outline-none focus-visible:ring-[3px] focus-visible:outline-1"- >- {children}- </ScrollAreaPrimitive.Viewport>- <ScrollBar />- <ScrollAreaPrimitive.Corner />- </ScrollAreaPrimitive.Root>+ <ScrollAreaContext.Provider value={isTouch}>+ {isTouch ? (+ <div+ ref={ref}+ role="group"+ data-slot="scroll-area"+ aria-roledescription="scroll area"+ className={cn("relative overflow-hidden", className)}+ {...props}+ >+ <div+ ref={viewportRef}+ data-slot="scroll-area-viewport"+ className={cn("size-full overflow-auto rounded-[inherit]", viewportClassName)}+ tabIndex={0}+ >+ {children}+ </div>++ {maskHeight > 0 && <ScrollMask showMask={showMask} className={maskClassName} maskHeight={maskHeight} />}+ </div>+ ) : (+ <ScrollAreaPrimitive.Root+ ref={ref}+ data-slot="scroll-area"+ scrollHideDelay={scrollHideDelay}+ className={cn("relative overflow-hidden", className)}+ {...props}+ >+ <ScrollAreaPrimitive.Viewport+ ref={viewportRef}+ data-slot="scroll-area-viewport"+ className={cn("size-full rounded-[inherit]", viewportClassName)}+ >+ {children}+ </ScrollAreaPrimitive.Viewport>++ {maskHeight > 0 && <ScrollMask showMask={showMask} className={maskClassName} maskHeight={maskHeight} />}+ <ScrollBar />+ <ScrollAreaPrimitive.Corner />+ </ScrollAreaPrimitive.Root>+ )}+ </ScrollAreaContext.Provider>);-}+});++ScrollArea.displayName = ScrollAreaPrimitive.Root.displayName;++const ScrollBar = React.forwardRef<+ React.ComponentRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>,+ React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>+>(({ className, orientation = "vertical", ...props }, ref) => {+ const isTouch = React.useContext(ScrollAreaContext);++ if (isTouch) return null;-function ScrollBar({- className,- orientation = "vertical",- ...props-}: React.ComponentProps<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>) {return (<ScrollAreaPrimitive.ScrollAreaScrollbar- data-slot="scroll-area-scrollbar"+ ref={ref}orientation={orientation}+ data-slot="scroll-area-scrollbar"className={cn(- "flex touch-none p-px transition-colors select-none",+ "hover:bg-muted dark:hover:bg-muted/50 data-[state=visible]:fade-in-0 data-[state=hidden]:fade-out-0 data-[state=visible]:animate-in data-[state=hidden]:animate-out flex touch-none p-px transition-[colors] duration-150 select-none",orientation === "vertical" && "h-full w-2.5 border-l border-l-transparent",- orientation === "horizontal" && "h-2.5 flex-col border-t border-t-transparent",+ orientation === "horizontal" && "h-2.5 flex-col border-t border-t-transparent px-1 pr-1.25",className)}{...props}><ScrollAreaPrimitive.ScrollAreaThumbdata-slot="scroll-area-thumb"- className="bg-border relative flex-1 rounded-full"+ className={cn(+ "bg-border relative flex-1 origin-center rounded-full transition-[scale]",+ orientation === "vertical" && "my-1 active:scale-y-95",+ orientation === "horizontal" && "active:scale-x-98"+ )}/></ScrollAreaPrimitive.ScrollAreaScrollbar>);-}+});++ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName;++const ScrollMask = ({+ showMask,+ maskHeight,+ className,+ ...props+}: React.ComponentProps<"div"> & {+ showMask: Mask;+ maskHeight: number;+}) => {+ return (+ <>+ <div+ {...props}+ aria-hidden="true"+ style={+ {+ "--top-fade-height": showMask.top ? `${maskHeight}px` : "0px",+ "--bottom-fade-height": showMask.bottom ? `${maskHeight}px` : "0px",+ } as React.CSSProperties+ }+ className={cn(+ "pointer-events-none absolute inset-0 z-10",+ "before:absolute before:inset-x-0 before:top-0 before:transition-[height,opacity] before:duration-300 before:content-['']",+ "after:absolute after:inset-x-0 after:bottom-0 after:transition-[height,opacity] after:duration-300 after:content-['']",+ "before:h-(--top-fade-height) after:h-(--bottom-fade-height)",+ showMask.top ? "before:opacity-100" : "before:opacity-0",+ showMask.bottom ? "after:opacity-100" : "after:opacity-0",+ "before:from-white dark:before:from-neutral-900 before:bg-gradient-to-b before:to-transparent",+ "after:from-white dark:after:from-neutral-900 after:bg-gradient-to-t after:to-transparent",+ className+ )}+ />+ <div+ {...props}+ aria-hidden="true"+ style={+ {+ "--left-fade-width": showMask.left ? `${maskHeight}px` : "0px",+ "--right-fade-width": showMask.right ? `${maskHeight}px` : "0px",+ } as React.CSSProperties+ }+ className={cn(+ "pointer-events-none absolute inset-0 z-10",+ "before:absolute before:inset-y-0 before:left-0 before:transition-[width,opacity] before:duration-300 before:content-['']",+ "after:absolute after:inset-y-0 after:right-0 after:transition-[width,opacity] after:duration-300 after:content-['']",+ "before:w-(--left-fade-width) after:w-(--right-fade-width)",+ showMask.left ? "before:opacity-100" : "before:opacity-0",+ showMask.right ? "after:opacity-100" : "after:opacity-0",+ "before:from-white dark:before:from-neutral-900 before:bg-gradient-to-r before:to-transparent",+ "after:from-white dark:after:from-neutral-900 after:bg-gradient-to-l after:to-transparent",+ className+ )}+ />+ </>+ );+};export { ScrollArea, ScrollBar };