Skip to content

Commit 713a0b6

Browse files
committed
Add forward ref support to ListHeader and implement scroll-to-timeline button with intersection observer
1 parent 85aa6a9 commit 713a0b6

4 files changed

Lines changed: 113 additions & 22 deletions

File tree

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { forwardRef } from 'react'
12
import styles from './ListHeader.module.scss'
23
import cn from 'classnames'
34

@@ -10,25 +11,28 @@ export namespace ListHeader {
1011
}
1112
}
1213

13-
export const ListHeader: React.FC<ListHeader.Props> = (props) => {
14-
return (
15-
<div
16-
className={styles.header}
17-
onClick={props.on_toggle_collapsed}
18-
role="button"
19-
>
20-
<div className={styles.header__left}>
21-
<span
22-
className={cn('codicon', {
23-
'codicon-chevron-down': !props.is_collapsed,
24-
'codicon-chevron-right': props.is_collapsed
25-
})}
26-
/>
27-
<span>{props.title}</span>
14+
export const ListHeader = forwardRef<HTMLDivElement, ListHeader.Props>(
15+
(props, ref) => {
16+
return (
17+
<div
18+
ref={ref}
19+
className={styles.header}
20+
onClick={props.on_toggle_collapsed}
21+
role="button"
22+
>
23+
<div className={styles.header__left}>
24+
<span
25+
className={cn('codicon', {
26+
'codicon-chevron-down': !props.is_collapsed,
27+
'codicon-chevron-right': props.is_collapsed
28+
})}
29+
/>
30+
<span>{props.title}</span>
31+
</div>
32+
{props.actions && !props.is_collapsed && (
33+
<div className={styles.header__right}>{props.actions}</div>
34+
)}
2835
</div>
29-
{props.actions && !props.is_collapsed && (
30-
<div className={styles.header__right}>{props.actions}</div>
31-
)}
32-
</div>
33-
)
34-
}
36+
)
37+
}
38+
)

‎packages/ui/src/components/editor/panel/ResponsePreview/ResponsePreview.module.scss‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
font-size: 11px;
107107
color: var(--vscode-descriptionForeground);
108108
margin-right: 4px;
109+
white-space: nowrap;
109110
}
110111

111112
&__file {

‎packages/vscode/src/views/panel/frontend/Home/Home.module.scss‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,39 @@
218218
opacity: 1;
219219
}
220220
}
221+
222+
.scroll-to-timeline {
223+
position: absolute;
224+
bottom: 8px;
225+
right: 12px;
226+
z-index: 1;
227+
display: flex;
228+
align-items: center;
229+
gap: 4px;
230+
padding: 3px 11px 3px 7px;
231+
background-color: var(--vscode-button-background);
232+
color: var(--vscode-button-foreground);
233+
border: none;
234+
border-radius: 20px;
235+
cursor: pointer;
236+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
237+
font-size: 12px;
238+
font-weight: 500;
239+
opacity: 0;
240+
animation: fadeIn 0.2s forwards;
241+
242+
&:hover {
243+
background-color: var(--vscode-button-hoverBackground);
244+
}
245+
}
246+
247+
@keyframes fadeIn {
248+
from {
249+
opacity: 0;
250+
transform: translateY(10px);
251+
}
252+
to {
253+
opacity: 1;
254+
transform: translateY(0);
255+
}
256+
}

‎packages/vscode/src/views/panel/frontend/Home/Home.tsx‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useRef } from 'react'
1+
import { useState, useRef, useEffect } from 'react'
22
import styles from './Home.module.scss'
33
import { Scrollable } from '@ui/components/editor/panel/Scrollable'
44
import { Timeline } from '@ui/components/editor/panel/Timeline'
@@ -45,9 +45,33 @@ type Props = {
4545
export const Home: React.FC<Props> = (props) => {
4646
const { t } = use_translation()
4747
const [is_mode_sticky, set_is_mode_sticky] = useState(false)
48+
const [is_timeline_reached, set_is_timeline_reached] = useState(true)
4849
const full_mode_height_ref = useRef<number>(0)
4950
const responses_ref = useRef<HTMLDivElement>(null)
5051
const mode_ref = useRef<HTMLDivElement>(null)
52+
const timeline_ref = useRef<HTMLDivElement>(null)
53+
54+
useEffect(() => {
55+
const observer = new IntersectionObserver(
56+
([entry]) => {
57+
const is_above_viewport =
58+
entry.boundingClientRect.top <
59+
(entry.rootBounds?.height ?? window.innerHeight) / 2
60+
set_is_timeline_reached(entry.isIntersecting || is_above_viewport)
61+
},
62+
{
63+
threshold: 0.1
64+
}
65+
)
66+
67+
if (timeline_ref.current) {
68+
observer.observe(timeline_ref.current)
69+
}
70+
71+
return () => {
72+
observer.disconnect()
73+
}
74+
}, [])
5175

5276
const {
5377
handle_reorder,
@@ -77,6 +101,15 @@ export const Home: React.FC<Props> = (props) => {
77101
} as FrontendMessage)
78102
}
79103

104+
const handle_scroll_to_timeline = () => {
105+
if (timeline_ref.current) {
106+
timeline_ref.current.style.scrollMarginTop = '55px'
107+
timeline_ref.current.scrollIntoView({
108+
behavior: 'smooth'
109+
})
110+
}
111+
}
112+
80113
return (
81114
<>
82115
<div className={styles.header}>
@@ -247,6 +280,7 @@ export const Home: React.FC<Props> = (props) => {
247280
))}
248281

249282
<ListHeader
283+
ref={timeline_ref}
250284
title="Timeline"
251285
is_collapsed={props.is_timeline_collapsed}
252286
on_toggle_collapsed={() =>
@@ -317,6 +351,22 @@ export const Home: React.FC<Props> = (props) => {
317351
</div>
318352
</div>
319353
</Scrollable>
354+
355+
{!is_timeline_reached && is_mode_sticky && (
356+
<button
357+
className={styles['scroll-to-timeline']}
358+
onClick={handle_scroll_to_timeline}
359+
>
360+
<span
361+
className={cn(
362+
'codicon',
363+
'codicon-arrow-down',
364+
styles['scroll-to-timeline__icon']
365+
)}
366+
/>
367+
<span>Scroll to timeline</span>
368+
</button>
369+
)}
320370
</>
321371
)
322372
}

0 commit comments

Comments
 (0)