MCPcopy Create free account
hub / github.com/Noumena-Network/code / ScrollBox

Function ScrollBox

src/ink/components/ScrollBox.tsx:82–253  ·  view source on GitHub ↗

* A Box with `overflow: scroll` and an imperative scroll API. * * Children are laid out at their full Yoga-computed height inside a * constrained container. At render time, only children intersecting the * visible window (scrollTop..scrollTop+height) are rendered (viewport * culling). Content i

({
  children,
  ref,
  stickyScroll,
  ...style
}: PropsWithChildren<ScrollBoxProps>)

Source from the content-addressed store, hash-verified

80 * Works best inside a fullscreen (constrained-height root) Ink tree.
81 */
82function ScrollBox({
83 children,
84 ref,
85 stickyScroll,
86 ...style
87}: PropsWithChildren<ScrollBoxProps>): React.ReactNode {
88 const domRef = useRef<DOMElement>(null);
89 // scrollTo/scrollBy bypass React: they mutate scrollTop on the DOM node,
90 // mark it dirty, and call the root's throttled scheduleRender directly.
91 // The Ink renderer reads scrollTop from the node — no React state needed,
92 // no reconciler overhead per wheel event. The microtask defer coalesces
93 // multiple scrollBy calls in one input batch (discreteUpdates) into one
94 // render — otherwise scheduleRender's leading edge fires on the FIRST
95 // event before subsequent events mutate scrollTop. scrollToBottom still
96 // forces a React render: sticky is attribute-observed, no DOM-only path.
97 const [, forceRender] = useState(0);
98 const listenersRef = useRef(new Set<() => void>());
99 const renderQueuedRef = useRef(false);
100 const lastClampBoundsRef = useRef<{
101 min: number | undefined;
102 max: number | undefined;
103 }>({
104 min: undefined,
105 max: undefined
106 });
107 const notify = () => {
108 for (const l of listenersRef.current) l();
109 };
110 function scrollMutated(el: DOMElement): void {
111 // Signal background intervals (IDE poll, LSP poll, GCS fetch, orphan
112 // check) to skip their next tick — they compete for the event loop and
113 // contributed to 1402ms max frame gaps during scroll drain.
114 markScrollActivity();
115 markDirty(el);
116 markCommitStart();
117 notify();
118 if (renderQueuedRef.current) return;
119 renderQueuedRef.current = true;
120 queueMicrotask(() => {
121 renderQueuedRef.current = false;
122 scheduleRenderFrom(el);
123 });
124 }
125 useImperativeHandle(ref, (): ScrollBoxHandle => ({
126 scrollTo(y: number) {
127 const el = domRef.current;
128 if (!el) return;
129 // Explicit false overrides the DOM attribute so manual scroll
130 // breaks stickiness. Render code checks ?? precedence.
131 el.stickyScroll = false;
132 el.pendingScrollDelta = undefined;
133 el.scrollAnchor = undefined;
134 el.scrollTop = Math.max(0, Math.floor(y));
135 scrollMutated(el);
136 },
137 scrollToElement(el: DOMElement, offset = 0) {
138 const box = domRef.current;
139 if (!box) return;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected