Skip to content

Commit 769c3b1

Browse files
committed
Remove automatic active navigation item tracking based on scroll position and refactor component ref handling
1 parent a7c90f6 commit 769c3b1

4 files changed

Lines changed: 33 additions & 59 deletions

File tree

‎packages/ui/src/components/editor/settings/Layout/Layout.tsx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ export const Layout = forwardRef<HTMLDivElement, Props>((props, ref) => {
2323

2424
const last_child_height = last_child.offsetHeight
2525
const viewport_height = window.innerHeight
26-
const calculated_padding =
27-
viewport_height - Math.floor(last_child_height) - 60
26+
const calculated_padding = viewport_height - Math.floor(last_child_height)
2827

2928
set_padding_bottom(
3029
calculated_padding > 0 ? `${calculated_padding}px` : '0px'

‎packages/ui/src/components/editor/settings/NavigationItem/NavigationItem.module.scss‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212
background-color: var(--vscode-list-hoverBackground);
1313
text-decoration: none;
1414
}
15+
16+
&--active {
17+
background-color: var(--vscode-list-hoverBackground);
18+
}
1519
}

‎packages/ui/src/components/editor/settings/Section/Section.tsx‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Props = {
1010

1111
export const Section = forwardRef<HTMLDivElement, Props>((props, ref) => {
1212
const marker_ref = useRef<HTMLDivElement>(null)
13-
const container_ref = useRef<HTMLDivElement>(null)
13+
const container_ref = useRef<HTMLDivElement | null>(null)
1414
const [is_stuck, set_is_stuck] = useState(false)
1515

1616
useEffect(() => {
@@ -51,7 +51,17 @@ export const Section = forwardRef<HTMLDivElement, Props>((props, ref) => {
5151
}, [])
5252

5353
return (
54-
<div ref={container_ref} className={styles.container}>
54+
<div
55+
ref={(node) => {
56+
container_ref.current = node
57+
if (typeof ref == 'function') {
58+
ref(node)
59+
} else if (ref) {
60+
ref.current = node
61+
}
62+
}}
63+
className={styles.container}
64+
>
5565
<div
5666
className={cn(styles.header, { [styles['header--stuck']]: is_stuck })}
5767
>

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

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ type Props = {
7171
}
7272

7373
export const Home: React.FC<Props> = (props) => {
74-
const [active_item, set_active_item] = useState<NavItem>('general')
7574
const scroll_container_ref = useRef<HTMLDivElement>(null)
7675
const section_refs = useRef<Record<NavItem, HTMLDivElement | null>>({
7776
general: null,
@@ -81,8 +80,6 @@ export const Home: React.FC<Props> = (props) => {
8180
'intelligent-update': null,
8281
'commit-messages': null
8382
})
84-
const is_scrolling_from_click = useRef(false)
85-
8683
const [commit_instructions, set_commit_instructions] = useState('')
8784

8885
useEffect(() => {
@@ -106,64 +103,28 @@ export const Home: React.FC<Props> = (props) => {
106103
])
107104

108105
const handle_scroll_to_section = (item_id: NavItem) => {
109-
is_scrolling_from_click.current = true
110-
set_active_item(item_id)
111-
section_refs.current[item_id]?.scrollIntoView({
112-
behavior: 'smooth',
113-
block: 'start'
114-
})
115-
setTimeout(() => {
116-
is_scrolling_from_click.current = false
117-
}, 1000)
118-
}
119-
120-
useEffect(() => {
121-
if (props.scroll_to_section_on_load) {
122-
handle_scroll_to_section(props.scroll_to_section_on_load)
123-
}
124-
}, [props.scroll_to_section_on_load])
125-
126-
useEffect(() => {
127-
const handle_scroll = () => {
128-
if (is_scrolling_from_click.current) return
106+
const section = section_refs.current[item_id]
107+
const scroll_container = scroll_container_ref.current
129108

130-
const THRESHOLD = 200 // 200px from top of viewport
131-
let closest_section: NavItem | null = null
132-
let closest_distance = Infinity
109+
if (section && scroll_container) {
110+
const container_rect = scroll_container.getBoundingClientRect()
111+
const section_rect = section.getBoundingClientRect()
133112

134-
NAV_ITEMS_CONFIG.forEach(({ id }) => {
135-
const element = section_refs.current[id]
136-
if (element) {
137-
const rect = element.getBoundingClientRect()
138-
const distance_from_threshold = Math.abs(rect.top - THRESHOLD)
113+
const offset = section_rect.top - container_rect.top
114+
const target_scroll_top = scroll_container.scrollTop + offset
139115

140-
// Check if top edge is within 200px from top and is closest
141-
if (
142-
rect.top <= THRESHOLD &&
143-
rect.top >= 0 &&
144-
distance_from_threshold < closest_distance
145-
) {
146-
closest_section = id
147-
closest_distance = distance_from_threshold
148-
}
149-
}
116+
scroll_container.scrollTo({
117+
top: target_scroll_top,
118+
behavior: 'smooth'
150119
})
151-
152-
if (closest_section) {
153-
set_active_item(closest_section)
154-
}
155-
}
156-
157-
const container = scroll_container_ref.current
158-
if (container) {
159-
container.addEventListener('scroll', handle_scroll)
160-
handle_scroll() // Check initial position
161120
}
121+
}
162122

163-
return () => {
164-
container?.removeEventListener('scroll', handle_scroll)
123+
useEffect(() => {
124+
if (props.scroll_to_section_on_load) {
125+
handle_scroll_to_section(props.scroll_to_section_on_load)
165126
}
166-
}, [])
127+
}, [props.scroll_to_section_on_load])
167128

168129
const handle_nav_click = (
169130
e: React.MouseEvent<HTMLAnchorElement>,
@@ -185,7 +146,7 @@ export const Home: React.FC<Props> = (props) => {
185146
key={id}
186147
href={`#${id}`}
187148
label={label}
188-
is_active={active_item == id}
149+
is_active={false}
189150
on_click={(e) => handle_nav_click(e, id)}
190151
/>
191152
))}

0 commit comments

Comments
 (0)