Skip to content

Commit 7dcda60

Browse files
committed
Implement scroll position persistence in the response preview by adding initial_scroll_top and on_scroll props to the Scrollable component
1 parent 00d39b6 commit 7dcda60

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

‎packages/ui/src/components/editor/panel/ResponsePreview/ResponsePreview.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, useState } from 'react'
1+
import { FC, useRef, useState } from 'react'
22
import { FileInPreview, ItemInPreview } from '@shared/types/file-in-preview'
33
import cn from 'classnames'
44
import styles from './ResponsePreview.module.scss'
@@ -29,6 +29,7 @@ export const ResponsePreview: FC<Props> = (props) => {
2929
const [expanded_text_items, set_expanded_text_items] = useState<Set<number>>(
3030
new Set()
3131
)
32+
const scroll_top_ref = useRef(0)
3233

3334
const toggle_expanded_text_item = (index: number) => {
3435
set_expanded_text_items((prev) => {
@@ -48,7 +49,15 @@ export const ResponsePreview: FC<Props> = (props) => {
4849
const fallback_count = files_in_preview.filter((f) => f.is_fallback).length
4950

5051
return (
51-
<Scrollable key={expanded_text_items.size}>
52+
<Scrollable
53+
key={expanded_text_items.size}
54+
// Used to restore scroll on key change.
55+
// Key is changed to re-calculate simplebar container height.
56+
initial_scroll_top={scroll_top_ref.current}
57+
on_scroll={(top) => {
58+
scroll_top_ref.current = top
59+
}}
60+
>
5261
<div className={styles.container}>
5362
{props.raw_instructions && (
5463
<div className={styles.instructions} title={props.raw_instructions}>

‎packages/ui/src/components/editor/panel/Scrollable/Scrollable.tsx‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ type Props = {
77
children: ReactNode
88
scroll_to_top_key?: any
99
max_height?: string
10+
initial_scroll_top?: number
11+
on_scroll?: (top: number) => void
1012
}
1113

1214
export const Scrollable: React.FC<Props> = (props) => {
@@ -31,28 +33,42 @@ export const Scrollable: React.FC<Props> = (props) => {
3133
const scroll_element = simplebar_instance.getScrollElement()
3234
if (!scroll_element) return
3335

34-
const update_shadows = () => {
36+
const update = () => {
3537
const { scrollTop, scrollHeight, clientHeight } = scroll_element
3638
const is_scrollable = scrollHeight > clientHeight
37-
3839
set_has_top_shadow(is_scrollable && scrollTop > 0)
40+
if (props.on_scroll) props.on_scroll(scrollTop)
3941
}
4042

41-
update_shadows()
43+
update()
4244

43-
scroll_element.addEventListener('scroll', update_shadows)
45+
scroll_element.addEventListener('scroll', update)
4446

4547
const content_element = simplebar_instance.getContentElement()
46-
const resize_observer = new ResizeObserver(update_shadows)
48+
const resize_observer = new ResizeObserver(update)
4749
if (content_element) {
4850
resize_observer.observe(content_element)
4951
}
5052

5153
return () => {
52-
scroll_element.removeEventListener('scroll', update_shadows)
54+
scroll_element.removeEventListener('scroll', update)
5355
resize_observer.disconnect()
5456
}
55-
}, [])
57+
}, [props.on_scroll])
58+
59+
useEffect(() => {
60+
const simplebar_instance = simplebar_ref.current
61+
if (!simplebar_instance) return
62+
63+
const scroll_element = simplebar_instance.getScrollElement()
64+
if (!scroll_element) return
65+
66+
if (typeof props.initial_scroll_top == 'number') {
67+
requestAnimationFrame(() => {
68+
scroll_element.scrollTop = props.initial_scroll_top
69+
})
70+
}
71+
}, [props.initial_scroll_top])
5672

5773
return (
5874
<div

0 commit comments

Comments
 (0)