@@ -20,15 +20,15 @@ export const enableTabs = () => {
2020
2121 // Handle arrow navigation between tabs in the tab list
2222 function handleKeydown ( this : HTMLElement , e : KeyboardEvent ) {
23- if ( e . keyCode === 39 || e . keyCode === 37 ) {
23+ if ( e . code === "ArrowRight" || e . code === "ArrowLeft" ) {
2424 const tabs = this . children ;
2525 let tabfocus = Number ( this . dataset . tabfocus || 0 ) ;
2626 tabs [ tabfocus ] . setAttribute ( "tabindex" , "-1" ) ;
27- if ( e . keyCode === 39 ) {
27+ if ( e . code === "ArrowRight" ) {
2828 // Move right
2929 // Increase tab index, wrap by # of tabs
3030 tabfocus = ( tabfocus + 1 ) % tabs . length ;
31- } else if ( e . keyCode === 37 ) {
31+ } else if ( e . code === "ArrowLeft" ) {
3232 // Move left
3333 tabfocus -- ;
3434 // If we're at the start, move to the end
@@ -39,21 +39,33 @@ export const enableTabs = () => {
3939
4040 // Update tabfocus values
4141 this . dataset . tabfocus = tabfocus + "" ;
42- // Focus + click selected tab
42+ // Focus selected tab
4343 const tab = tabs [ tabfocus ] as HTMLElement ;
4444 tab . setAttribute ( "tabindex" , "0" ) ;
4545 tab . focus ( ) ;
46- tab . click ( ) ;
47-
48- // Redundant: Scroll handled by `handleClick` on `tab.click()`
49- // setTimeout(() => {
50- // tab.scrollIntoView &&
51- // tab.scrollIntoView({
52- // behavior: "auto",
53- // block: "center",
54- // inline: "center",
55- // });
56- // }, 0);
46+
47+ // Check if tab list is within viewport
48+ const header = document . querySelector ( "#header-bar" ) ;
49+ const headerOffset = ( header ?. getBoundingClientRect ( ) . height ?? 0 ) + 20 ;
50+ const tabYPosition = tab . getBoundingClientRect ( ) . y ;
51+ const tabHeight = tab . getBoundingClientRect ( ) . height ;
52+ const isWithinViewport =
53+ tabYPosition > headerOffset &&
54+ tabYPosition < window . innerHeight - tabHeight ;
55+
56+ // Preserve scroll position if within viewport
57+ // Else scroll to 20px below header
58+ const offsetBeforeChangeTabs = tab . offsetTop - window . scrollY ;
59+ const offset = isWithinViewport ? offsetBeforeChangeTabs : headerOffset ;
60+
61+ const tabName = tab . dataset . tabname ;
62+ if ( ! tabName ) return ;
63+ changeTabs ( tabName ) ;
64+
65+ // Ensure tab list is within viewport after switching tabs
66+ setTimeout ( ( ) => {
67+ window . scroll ( 0 , tab . offsetTop - offset ) ;
68+ } , 0 ) ;
5769 }
5870 }
5971
0 commit comments