Skip to content

Commit f56ecb2

Browse files
fix
1 parent 9113e45 commit f56ecb2

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

packages/react/src/components/DiffView.tsx

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { createDiffConfigStore } from "./tools";
1616

1717
import type { createDiffWidgetStore } from "./tools";
1818
import type { DiffHighlighter, DiffHighlighterLang } from "@git-diff-view/core";
19-
import type { CSSProperties, ForwardedRef, ReactNode } from "react";
19+
import type { CSSProperties, ForwardedRef, ReactNode, RefObject } from "react";
2020

2121
_cacheMap.name = "@git-diff-view/react";
2222

@@ -88,15 +88,15 @@ type DiffViewProps_2<T> = Omit<DiffViewProps<T>, "data"> & {
8888
};
8989

9090
const InternalDiffView = <T extends unknown>(
91-
props: Omit<DiffViewProps<T>, "data" | "registerHighlighter"> & { isMounted: boolean }
91+
props: Omit<DiffViewProps<T>, "data" | "registerHighlighter"> & { isMounted: boolean, wrapperRef?: RefObject<HTMLDivElement> }
9292
) => {
9393
const {
9494
diffFile,
9595
className,
9696
style,
97+
wrapperRef,
9798
diffViewMode,
9899
diffViewWrap,
99-
diffViewTheme,
100100
diffViewFontSize,
101101
diffViewHighlight,
102102
renderWidgetLine,
@@ -110,8 +110,6 @@ const InternalDiffView = <T extends unknown>(
110110

111111
const diffFileId = useMemo(() => diffFile.getId(), [diffFile]);
112112

113-
const wrapperRef = useRef<HTMLDivElement>();
114-
115113
// performance optimization
116114
const useDiffContext = useMemo(
117115
() => createDiffConfigStore(props, diffFileId),
@@ -209,19 +207,6 @@ const InternalDiffView = <T extends unknown>(
209207
onCreateUseWidgetHook,
210208
]);
211209

212-
useEffect(() => {
213-
const init = () => {
214-
wrapperRef.current?.setAttribute("data-theme", diffFile._getTheme() || "light");
215-
wrapperRef.current?.setAttribute("data-highlighter", diffFile._getHighlighterName());
216-
}
217-
218-
init();
219-
220-
const cb = diffFile.subscribe(init);
221-
222-
return cb;
223-
}, [diffFile, diffViewTheme]);
224-
225210
const value = useMemo(() => ({ useDiffContext }), [useDiffContext]);
226211

227212
return (
@@ -290,6 +275,8 @@ const DiffViewWithRef = <T extends unknown>(
290275

291276
const diffFileRef = useRef(diffFile);
292277

278+
const wrapperRef = useRef<HTMLDivElement>();
279+
293280
if (diffFileRef.current && diffFileRef.current !== diffFile) {
294281
diffFileRef.current.clear?.();
295282
diffFileRef.current = diffFile;
@@ -322,6 +309,19 @@ const DiffViewWithRef = <T extends unknown>(
322309
}
323310
}, [diffFile, props.diffViewHighlight, registerHighlighter]);
324311

312+
useEffect(() => {
313+
const init = () => {
314+
wrapperRef.current?.setAttribute("data-theme", diffFile._getTheme() || "light");
315+
wrapperRef.current?.setAttribute("data-highlighter", diffFile._getHighlighterName());
316+
};
317+
318+
init();
319+
320+
const cb = diffFile.subscribe(init);
321+
322+
return cb;
323+
}, [diffFile, diffViewTheme]);
324+
325325
// fix react strict mode error
326326
useUnmount(() => (__DEV__ ? diffFile?._destroy?.() : diffFile?.clear?.()), [diffFile]);
327327

@@ -335,6 +335,7 @@ const DiffViewWithRef = <T extends unknown>(
335335
{...restProps}
336336
diffFile={diffFile}
337337
isMounted={isMounted}
338+
wrapperRef={wrapperRef}
338339
diffViewTheme={diffViewTheme}
339340
diffViewMode={restProps.diffViewMode || DiffModeEnum.SplitGitHub}
340341
diffViewFontSize={restProps.diffViewFontSize || 14}

0 commit comments

Comments
 (0)