@@ -71,7 +71,6 @@ type Props = {
7171}
7272
7373export 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