1
- import { Fragment , useEffect , useRef , useState } from 'react'
1
+ import { Fragment , useEffect , useState } from 'react'
2
2
import Tippy from '@tippyjs/react'
3
3
4
4
import { ReactComponent as ICSortArrowDown } from '@Icons/ic-sort-arrow-down.svg'
@@ -38,10 +38,6 @@ export const DeploymentConfigDiffMain = ({
38
38
// STATES
39
39
const [ expandedView , setExpandedView ] = useState < Record < string | number , boolean > > ( { } )
40
40
41
- // REFS
42
- /** Ref to track if the element should scroll into view after expanding */
43
- const scrollIntoViewAfterExpand = useRef ( false )
44
-
45
41
const handleAccordionClick = ( id : string ) => ( ) => {
46
42
setExpandedView ( {
47
43
...expandedView ,
@@ -52,11 +48,12 @@ export const DeploymentConfigDiffMain = ({
52
48
const onTransitionEnd =
53
49
( id : string ) : DeploymentConfigDiffAccordionProps [ 'onTransitionEnd' ] =>
54
50
( e ) => {
55
- if ( scrollIntoViewAfterExpand . current && scrollIntoViewId === id && e . target === e . currentTarget ) {
56
- const element = document . querySelector ( `#${ scrollIntoViewId } ` )
57
- element ?. scrollIntoView ( { block : 'start' } )
58
- // Reset ref after scrolling into view
59
- scrollIntoViewAfterExpand . current = false
51
+ if ( scrollIntoViewId === id && e . target === e . currentTarget ) {
52
+ // Using setTimeout to ensure React state updates and DOM rendering are complete before scrolling to element.
53
+ setTimeout ( ( ) => {
54
+ const element = document . querySelector ( `#${ scrollIntoViewId } ` )
55
+ element ?. scrollIntoView ( { block : 'start' } )
56
+ } )
60
57
}
61
58
}
62
59
@@ -76,7 +73,6 @@ export const DeploymentConfigDiffMain = ({
76
73
77
74
useEffect ( ( ) => {
78
75
if ( scrollIntoViewId ) {
79
- scrollIntoViewAfterExpand . current = true
80
76
setExpandedView ( ( prev ) => ( { ...prev , [ scrollIntoViewId ] : true } ) )
81
77
}
82
78
} , [ scrollIntoViewId ] )
0 commit comments