@@ -12,6 +12,8 @@ import { DiffViewContext } from "./DiffViewContext";
1212import { createDiffConfigStore } from "./tools" ;
1313
1414import type { DiffHighlighter , DiffHighlighterLang } from "@git-diff-view/core" ;
15+ import type { DOMElement } from "ink" ;
16+ import type { RefObject } from "react" ;
1517
1618_cacheMap . name = "@git-diff-view/cli" ;
1719
@@ -23,6 +25,7 @@ export type DiffViewProps = {
2325 newFile ?: { fileName ?: string | null ; fileLang ?: DiffHighlighterLang | string | null ; content ?: string | null } ;
2426 hunks : string [ ] ;
2527 } ;
28+ width ?: number ;
2629 diffFile ?: DiffFile ;
2730 diffViewMode ?: DiffModeEnum ;
2831 diffViewTheme ?: "light" | "dark" ;
@@ -49,9 +52,10 @@ type DiffViewProps_2 = Omit<DiffViewProps, "data"> & {
4952const InternalDiffView = (
5053 props : Omit < DiffViewProps , "data" > & {
5154 isMounted : boolean ;
55+ wrapperRef ?: RefObject < DOMElement > ;
5256 }
5357) => {
54- const { diffFile, diffViewMode, diffViewHighlight, isMounted } = props ;
58+ const { diffFile, diffViewMode, diffViewHighlight, isMounted, wrapperRef } = props ;
5559
5660 const diffFileId = useMemo ( ( ) => diffFile . getId ( ) , [ diffFile ] ) ;
5761
@@ -79,6 +83,13 @@ const InternalDiffView = (
7983 }
8084 } , [ useDiffContext , diffViewHighlight , diffViewMode , diffFileId , isMounted ] ) ;
8185
86+ useEffect ( ( ) => {
87+ const { wrapper, setWrapper } = useDiffContext . getReadonlyState ( ) ;
88+ if ( wrapperRef . current !== wrapper . current ) {
89+ setWrapper ( wrapperRef . current ) ;
90+ }
91+ } ) ;
92+
8293 const value = useMemo ( ( ) => ( { useDiffContext } ) , [ useDiffContext ] ) ;
8394
8495 return (
@@ -102,8 +113,10 @@ const InternalDiffView = (
102113
103114const MemoedInternalDiffView = memo ( InternalDiffView ) ;
104115
105- const DiffViewWithRef = ( props : DiffViewProps ) => {
106- const { registerHighlighter, data, diffViewTheme, diffFile : _diffFile , ...restProps } = props ;
116+ const DiffViewContainer = ( props : DiffViewProps ) => {
117+ const { registerHighlighter, data, diffViewTheme, diffFile : _diffFile , width, ...restProps } = props ;
118+
119+ const ref = useRef < DOMElement > ( null ) ;
107120
108121 const diffFile = useMemo ( ( ) => {
109122 if ( _diffFile ) {
@@ -167,22 +180,30 @@ const DiffViewWithRef = (props: DiffViewProps) => {
167180 if ( ! diffFile ) return null ;
168181
169182 return (
170- < MemoedInternalDiffView
171- key = { diffFile . getId ( ) }
172- { ...restProps }
173- diffFile = { diffFile }
174- isMounted = { isMounted }
175- diffViewTheme = { diffViewTheme }
176- diffViewMode = { restProps . diffViewMode || DiffModeEnum . SplitGitHub }
177- />
183+ < Box
184+ ref = { ref }
185+ width = { width }
186+ flexGrow = { typeof width === "number" ? undefined : 1 }
187+ flexShrink = { typeof width === "number" ? 0 : undefined }
188+ >
189+ < MemoedInternalDiffView
190+ key = { diffFile . getId ( ) }
191+ { ...restProps }
192+ wrapperRef = { ref }
193+ diffFile = { diffFile }
194+ isMounted = { isMounted }
195+ diffViewTheme = { diffViewTheme }
196+ diffViewMode = { restProps . diffViewMode || DiffModeEnum . SplitGitHub }
197+ />
198+ </ Box >
178199 ) ;
179200} ;
180201
181202// type helper function
182203function ReactDiffView ( props : DiffViewProps_1 ) : JSX . Element ;
183204function ReactDiffView ( props : DiffViewProps_2 ) : JSX . Element ;
184205function ReactDiffView ( props : DiffViewProps ) {
185- return < DiffViewWithRef { ...props } /> ;
206+ return < DiffViewContainer { ...props } /> ;
186207}
187208
188209export const DiffView = ReactDiffView ;
0 commit comments