Skip to content

Commit add977f

Browse files
authored
Merge pull request #473 from devtron-labs/fix/config-compare-scrolling
fix: DeploymentConfigDiff - refactored logic for scrolling selected item into view after loading or any context change
2 parents 97d0945 + cdf7611 commit add977f

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/Shared/Components/DeploymentConfigDiff/DeploymentConfigDiffMain.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fragment, useEffect, useRef, useState } from 'react'
1+
import { Fragment, useEffect, useState } from 'react'
22
import Tippy from '@tippyjs/react'
33

44
import { ReactComponent as ICSortArrowDown } from '@Icons/ic-sort-arrow-down.svg'
@@ -38,10 +38,6 @@ export const DeploymentConfigDiffMain = ({
3838
// STATES
3939
const [expandedView, setExpandedView] = useState<Record<string | number, boolean>>({})
4040

41-
// REFS
42-
/** Ref to track if the element should scroll into view after expanding */
43-
const scrollIntoViewAfterExpand = useRef(false)
44-
4541
const handleAccordionClick = (id: string) => () => {
4642
setExpandedView({
4743
...expandedView,
@@ -52,11 +48,12 @@ export const DeploymentConfigDiffMain = ({
5248
const onTransitionEnd =
5349
(id: string): DeploymentConfigDiffAccordionProps['onTransitionEnd'] =>
5450
(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+
})
6057
}
6158
}
6259

@@ -76,7 +73,6 @@ export const DeploymentConfigDiffMain = ({
7673

7774
useEffect(() => {
7875
if (scrollIntoViewId) {
79-
scrollIntoViewAfterExpand.current = true
8076
setExpandedView((prev) => ({ ...prev, [scrollIntoViewId]: true }))
8177
}
8278
}, [scrollIntoViewId])

0 commit comments

Comments
 (0)