Open-source, local-first AI agent for real work.
TypeScript agent-toolsai-agentbyokcoding-agentcross-platformdesktop-appdeveloper-toolselectronharnessknowledge-basellmwikilocal-firstmcpplugin-systemprivacy-firstskillstypescriptvettaworkflow-automation
import {
type CSSProperties,
type HTMLAttributes,
type JSX,
type ReactNode,
useCallback,
useEffect,
useState,
} from "react";
/** Default fade height (px) at the bottom edge when more content is below. */
const DEFAULT_FADE_SIZE = 28;
export interface ScrollFadeProps extends Omit<HTMLAttributes<HTMLDivElement>, "children"> {
children: ReactNode;
/** Callback ref for the scroll element (e.g. Virtuoso customScrollParent). */
onScrollRef?: (el: HTMLDivElement | null) => void;
/** Fade height in px. Default 28. */
fadeSize?: number;
/** Which edges fade while there is more content beyond them. Default `"bottom"`. */
edges?: "bottom" | "both";
}
/**
* Scroll container that fades an edge (via mask-image) while more content lies beyond it —
* a scroll affordance without showing a scrollbar. The top edge only fades with `edges="both"`.
*/
export function ScrollFade({
children,
className,
fadeSize = DEFAULT_FADE_SIZE,
edges = "bottom",
onScrollRef,
style,
...rest
}: ScrollFadeProps): JSX.Element {
const [el, setEl] = useState<HTMLDivElement | null>(null);
const [canScrollMore, setCanScrollMore] = useState(false);
const [canScrollBack, setCanScrollBack] = useState(false);
const setRef = useCallback(
(node: HTMLDivElement | null) => {
setEl(node);
onScrollRef?.(node);
},
[onScrollRef],
);
useEffect(() => {
if (!el) {
setCanScrollMore(false);
setCanScrollBack(false);
return;
}
const sync = () => {
const more = el.scrollHeight - el.scrollTop - el.clientHeight > 1;
setCanScrollMore((prev) => (prev === more ? prev : more));
const back = el.scrollTop > 1;
setCanScrollBack((prev) => (prev === back ? prev : back));
};
// 与 QuickScrollOverlay 同款合帧:高频 DOM 变动下每帧至多一次布局读取。
let frame: number | null = null;
const scheduleSync = () => {
if (frame !== null) return;
frame = requestAnimationFrame(() => {
frame = null;
sync();
});
};
sync();
el.addEventListener("scroll", scheduleSync, { passive: true });
const ro = new ResizeObserver(scheduleSync);
const observeChildren = () => {
ro.disconnect();
ro.observe(el);
for (const child of el.children) {
ro.observe(child);
}
};
observeChildren();
// 深层变动由子节点 ResizeObserver 覆盖,childList 只为重建子节点观察列表。
const mo = new MutationObserver(() => {
observeChildren();
scheduleSync();
});
mo.observe(el, { childList: true });
return () => {
if (frame !== null) cancelAnimationFrame(frame);
el.removeEventListener("scroll", scheduleSync);
ro.disconnect();
mo.disconnect();
};
}, [el]);
const fadeTop = edges === "both" && canScrollBack;
const maskImage =
canScrollMore || fadeTop
? `linear-gradient(to bottom, ${fadeTop ? `transparent 0%, black ${fadeSize}px` : "black 0%"}, ${
canScrollMore ? `black calc(100% - ${fadeSize}px), transparent 100%` : "black 100%"
})`
: undefined;
const mergedStyle: CSSProperties = {
...style,
...(maskImage
? {
WebkitMaskImage: maskImage,
maskImage,
}
: undefined),
};
return (
<div ref={setRef} className={className} style={mergedStyle} {...rest}>
{children}
</div>
);
}