Both CodeView and DiffView support a fixed-height viewport with programmatic scrolling. To enable scroll mode, you must provide a height prop (representing the number of visual rows after line wrapping).
Behavior
- Without
height: The full content is rendered (backward compatible). - With
height: The viewport is constrained. If height is larger than the content, the viewport shrinks to the content height. - Width changes: When
width changes, the scroll offset resets to 0 because line wrapping counts change.
ScrollState Object
onScrollChange provides a ScrollState object containing:
totalLines: logical line counttotalRows: visual row count after wrapviewportHeight: effective height (min of height and totalRows)scrollOffset: top visual row (0-based)startLine: first visible logical lineendLine: last visible logical linecanScrollUp: booleancanScrollDown: boolean
ScrollViewRef Methods
Use a ref to control the view programmatically:
getScrollState(): Get current snapshot.scrollToTop(line): Align target logical line to viewport top.scrollToBottom(line): Align target logical line to viewport bottom.scrollUp({ unit?, step? }): Scroll up (unit is "visual" or "logical").scrollDown({ unit?, step? }): Scroll down.
import { useRef } from "react";
import { CodeView, DiffView, type CodeViewRef } from "@git-diff-view/cli";
const ref = useRef<CodeViewRef>(null);
<CodeView
ref={ref}
file={file}
height={20}
width={80}
onScrollChange={(state) => console.log(state.startLine, state.endLine)}
/>;
ref.current?.scrollToTop(100);
ref.current?.scrollDown({ unit: "logical" });