1- import { useCallback , useEffect } from "react" ;
1+ import { useEffect } from "react" ;
22
33import { useDiffViewContext } from "../components/DiffViewContext" ;
44
@@ -16,59 +16,50 @@ export const useSyncHeight = ({
1616} ) => {
1717 const { useDiffContext } = useDiffViewContext ( ) ;
1818
19- const id = useDiffContext ( useCallback ( ( s ) => s . id , [ ] ) ) ;
19+ const { id , mounted } = useDiffContext . useShallowStableSelector ( ( s ) => ( { id : s . id , mounted : s . mounted } ) ) ;
2020
2121 useEffect ( ( ) => {
22- if ( enable ) {
23- let clean = ( ) => { } ;
24- // fix the dom delay update
25- const timer = setTimeout ( ( ) => {
26- const container = document . querySelector ( `#diff-root${ id } ` ) ;
22+ if ( enable && mounted ) {
23+ const container = document . querySelector ( `#diff-root${ id } ` ) ;
2724
28- const elements = Array . from ( container ?. querySelectorAll ( selector ) || [ ] ) ;
25+ const elements = Array . from ( container ?. querySelectorAll ( selector ) || [ ] ) ;
2926
30- const wrappers = wrapper ? Array . from ( container ?. querySelectorAll ( wrapper ) || [ ] ) : elements ;
27+ const wrappers = wrapper ? Array . from ( container ?. querySelectorAll ( wrapper ) || [ ] ) : elements ;
3128
32- if ( elements . length === 2 && wrappers . length === 2 ) {
33- const ele1 = elements [ 0 ] as HTMLElement ;
34- const ele2 = elements [ 1 ] as HTMLElement ;
29+ if ( elements . length === 2 && wrappers . length === 2 ) {
30+ const ele1 = elements [ 0 ] as HTMLElement ;
31+ const ele2 = elements [ 1 ] as HTMLElement ;
3532
36- const wrapper1 = wrappers [ 0 ] as HTMLElement ;
37- const wrapper2 = wrappers [ 1 ] as HTMLElement ;
33+ const wrapper1 = wrappers [ 0 ] as HTMLElement ;
34+ const wrapper2 = wrappers [ 1 ] as HTMLElement ;
3835
39- const target = ele1 . getAttribute ( "data-side" ) === side ? ele1 : ele2 ;
36+ const target = ele1 . getAttribute ( "data-side" ) === side ? ele1 : ele2 ;
4037
41- const cb = ( ) => {
42- ele1 . style . height = "auto" ;
43- ele2 . style . height = "auto" ;
44- const rect1 = ele1 . getBoundingClientRect ( ) ;
45- const rect2 = ele2 . getBoundingClientRect ( ) ;
46- const maxHeight = Math . max ( rect1 . height , rect2 . height ) ;
47- wrapper1 . style . height = maxHeight + "px" ;
48- wrapper2 . style . height = maxHeight + "px" ;
49- wrapper1 . setAttribute ( "data-sync-height" , String ( maxHeight ) ) ;
50- wrapper2 . setAttribute ( "data-sync-height" , String ( maxHeight ) ) ;
51- } ;
38+ const cb = ( ) => {
39+ ele1 . style . height = "auto" ;
40+ ele2 . style . height = "auto" ;
41+ const rect1 = ele1 . getBoundingClientRect ( ) ;
42+ const rect2 = ele2 . getBoundingClientRect ( ) ;
43+ const maxHeight = Math . max ( rect1 . height , rect2 . height ) ;
44+ wrapper1 . style . height = maxHeight + "px" ;
45+ wrapper2 . style . height = maxHeight + "px" ;
46+ wrapper1 . setAttribute ( "data-sync-height" , String ( maxHeight ) ) ;
47+ wrapper2 . setAttribute ( "data-sync-height" , String ( maxHeight ) ) ;
48+ } ;
5249
53- cb ( ) ;
50+ cb ( ) ;
5451
55- const observer = new ResizeObserver ( cb ) ;
52+ const observer = new ResizeObserver ( cb ) ;
5653
57- observer . observe ( target ) ;
54+ observer . observe ( target ) ;
5855
59- target . setAttribute ( "data-observe" , "height" ) ;
56+ target . setAttribute ( "data-observe" , "height" ) ;
6057
61- clean = ( ) => {
62- observer . disconnect ( ) ;
63- target ?. removeAttribute ( "data-observe" ) ;
64- } ;
65- }
66- } ) ;
67-
68- return ( ) => {
69- clean ( ) ;
70- clearTimeout ( timer ) ;
71- } ;
58+ return ( ) => {
59+ observer . disconnect ( ) ;
60+ target ?. removeAttribute ( "data-observe" ) ;
61+ } ;
62+ }
7263 }
73- } , [ selector , enable , side , id , wrapper ] ) ;
64+ } , [ selector , enable , side , id , wrapper , mounted ] ) ;
7465} ;
0 commit comments