Skip to content

Commit b110626

Browse files
committed
Extract timeline scroll logic into a reusable hook
1 parent 802e85e commit b110626

2 files changed

Lines changed: 69 additions & 34 deletions

File tree

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

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useRef, useEffect } from 'react'
1+
import { useState, useRef } 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'
@@ -15,6 +15,7 @@ import { IconButton } from '@ui/components/editor/panel/IconButton'
1515
import { Tasks } from '@ui/components/editor/panel/Tasks'
1616
import { Task } from '@shared/types/task'
1717
import { use_tasks } from './hooks/use-tasks'
18+
import { use_timeline_scroll } from './hooks/use-timeline-scroll'
1819

1920
type Props = {
2021
vscode: any
@@ -45,33 +46,9 @@ type Props = {
4546
export const Home: React.FC<Props> = (props) => {
4647
const { t } = use_translation()
4748
const [is_mode_sticky, set_is_mode_sticky] = useState(false)
48-
const [is_timeline_reached, set_is_timeline_reached] = useState(true)
4949
const full_mode_height_ref = useRef<number>(0)
5050
const responses_ref = useRef<HTMLDivElement>(null)
5151
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-
}, [])
7552

7653
const {
7754
handle_reorder,
@@ -82,6 +59,9 @@ export const Home: React.FC<Props> = (props) => {
8259
handle_copy
8360
} = use_tasks(props.on_tasks_change, props.on_task_delete, props.on_task_copy)
8461

62+
const { is_timeline_reached, timeline_ref, handle_scroll_to_timeline } =
63+
use_timeline_scroll()
64+
8565
const handle_settings_click = () => {
8666
post_message(props.vscode, {
8767
command: 'EXECUTE_COMMAND',
@@ -101,15 +81,6 @@ export const Home: React.FC<Props> = (props) => {
10181
} as FrontendMessage)
10282
}
10383

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-
11384
return (
11485
<>
11586
<div className={styles.header}>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { useState, useRef, useEffect } from 'react'
2+
3+
export const use_timeline_scroll = () => {
4+
const [is_timeline_reached, set_is_timeline_reached] = useState(true)
5+
const timeline_ref = useRef<HTMLDivElement>(null)
6+
7+
useEffect(() => {
8+
const observer = new IntersectionObserver(
9+
([entry]) => {
10+
const is_above_viewport =
11+
entry.boundingClientRect.top <
12+
(entry.rootBounds?.height ?? window.innerHeight) / 2
13+
set_is_timeline_reached(entry.isIntersecting || is_above_viewport)
14+
},
15+
{
16+
threshold: 0.1
17+
}
18+
)
19+
20+
if (timeline_ref.current) {
21+
observer.observe(timeline_ref.current)
22+
}
23+
24+
return () => {
25+
observer.disconnect()
26+
}
27+
}, [])
28+
29+
const handle_scroll_to_timeline = () => {
30+
if (timeline_ref.current) {
31+
let container = timeline_ref.current.parentElement
32+
while (container) {
33+
const style = window.getComputedStyle(container)
34+
if (
35+
(style.overflowY == 'auto' || style.overflowY == 'scroll') &&
36+
container.scrollHeight > container.clientHeight
37+
) {
38+
const container_rect = container.getBoundingClientRect()
39+
const element_rect = timeline_ref.current.getBoundingClientRect()
40+
container.scrollTo({
41+
top:
42+
container.scrollTop +
43+
(element_rect.top - container_rect.top) -
44+
55,
45+
behavior: 'smooth'
46+
})
47+
return
48+
}
49+
container = container.parentElement
50+
}
51+
52+
timeline_ref.current.style.scrollMarginTop = '55px'
53+
timeline_ref.current.scrollIntoView({
54+
behavior: 'smooth'
55+
})
56+
}
57+
}
58+
59+
return {
60+
is_timeline_reached,
61+
timeline_ref,
62+
handle_scroll_to_timeline
63+
}
64+
}

0 commit comments

Comments
 (0)