22import { DiffFile , _cacheMap , SplitSide } from "@git-diff-view/core" ;
33import { DiffModeEnum } from "@git-diff-view/utils" ;
44import { Box } from "ink" ;
5- import React , { memo , useEffect , useMemo , useRef } from "react" ;
5+ import React , { forwardRef , memo , useEffect , useImperativeHandle , useMemo , useRef } from "react" ;
66
77import { useIsMounted } from "../hooks/useIsMounted" ;
88import { useUnmount } from "../hooks/useUnmount" ;
@@ -14,7 +14,7 @@ import { createDiffConfigStore } from "./tools";
1414
1515import type { DiffHighlighter , DiffHighlighterLang } from "@git-diff-view/core" ;
1616import type { DOMElement } from "ink" ;
17- import type { ReactNode , RefObject } from "react" ;
17+ import type { ForwardedRef , ReactNode , RefObject } from "react" ;
1818
1919_cacheMap . name = "@git-diff-view/cli" ;
2020
@@ -182,10 +182,13 @@ const InternalDiffView = <T extends unknown>(
182182
183183const MemoedInternalDiffView = memo ( InternalDiffView ) ;
184184
185- const DiffViewContainer = < T extends unknown > ( props : DiffViewProps < T > ) => {
185+ const DiffViewContainerWithRef = < T extends unknown > (
186+ props : DiffViewProps < T > ,
187+ ref : ForwardedRef < { getDiffFileInstance : ( ) => DiffFile } >
188+ ) => {
186189 const { registerHighlighter, data, diffViewTheme, diffFile : _diffFile , width, ...restProps } = props ;
187190
188- const ref = useRef < DOMElement > ( null ) ;
191+ const domRef = useRef < DOMElement > ( null ) ;
189192
190193 const diffFile = useMemo ( ( ) => {
191194 if ( _diffFile ) {
@@ -246,19 +249,21 @@ const DiffViewContainer = <T extends unknown>(props: DiffViewProps<T>) => {
246249 // fix react strict mode error
247250 useUnmount ( ( ) => ( __DEV__ ? diffFile ?. _destroy ?.( ) : diffFile ?. clear ?.( ) ) , [ diffFile ] ) ;
248251
252+ useImperativeHandle ( ref , ( ) => ( { getDiffFileInstance : ( ) => diffFile } ) , [ diffFile ] ) ;
253+
249254 if ( ! diffFile ) return null ;
250255
251256 return (
252257 < Box
253- ref = { ref }
258+ ref = { domRef }
254259 width = { width }
255260 flexGrow = { typeof width === "number" ? undefined : 1 }
256261 flexShrink = { typeof width === "number" ? 0 : undefined }
257262 >
258263 < MemoedInternalDiffView
259264 key = { diffFile . getId ( ) }
260265 { ...restProps }
261- wrapperRef = { ref }
266+ wrapperRef = { domRef }
262267 diffFile = { diffFile }
263268 isMounted = { isMounted }
264269 diffViewTheme = { diffViewTheme }
@@ -270,12 +275,21 @@ const DiffViewContainer = <T extends unknown>(props: DiffViewProps<T>) => {
270275} ;
271276
272277// type helper function
273- function ReactDiffView < T > ( props : DiffViewProps_1 < T > ) : JSX . Element ;
274- function ReactDiffView < T > ( props : DiffViewProps_2 < T > ) : JSX . Element ;
275- function ReactDiffView < T > ( props : DiffViewProps < T > ) {
276- return < DiffViewContainer { ...props } /> ;
278+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
279+ function ReactDiffView < T > (
280+ props : DiffViewProps_1 < T > & { ref ?: ForwardedRef < { getDiffFileInstance : ( ) => DiffFile } > }
281+ ) : JSX . Element ;
282+ function ReactDiffView < T > (
283+ props : DiffViewProps_2 < T > & { ref ?: ForwardedRef < { getDiffFileInstance : ( ) => DiffFile } > }
284+ ) : JSX . Element ;
285+ function ReactDiffView < T > ( _props : DiffViewProps < T > & { ref ?: ForwardedRef < { getDiffFileInstance : ( ) => DiffFile } > } ) {
286+ return < > </ > ;
277287}
278288
279- export const DiffView = ReactDiffView ;
289+ const InnerDiffView = forwardRef ( DiffViewContainerWithRef ) ;
290+
291+ InnerDiffView . displayName = "DiffView" ;
292+
293+ export const DiffView = InnerDiffView as typeof ReactDiffView ;
280294
281295export const version = __VERSION__ ;
0 commit comments