@@ -6,27 +6,22 @@ import * as React from 'react';
6
6
import cn from 'classnames' ;
7
7
import { RouteItem } from 'components/Layout/useRouteMeta' ;
8
8
import { useRouter } from 'next/router' ;
9
+ import { useActiveSection } from 'hooks/useActiveSection' ;
9
10
import { SidebarRouteTree } from '../Sidebar' ;
10
11
import sidebarHome from '../../../sidebarHome.json' ;
11
12
import sidebarLearn from '../../../sidebarLearn.json' ;
12
13
import sidebarReference from '../../../sidebarReference.json' ;
13
14
14
- function inferSection ( pathname : string ) : 'learn' | 'apis' | 'home' {
15
- if ( pathname . startsWith ( '/learn' ) ) {
16
- return 'learn' ;
17
- } else if ( pathname . startsWith ( '/apis' ) ) {
18
- return 'apis' ;
19
- } else {
20
- return 'home' ;
21
- }
22
- }
23
-
24
15
export function MobileNav ( ) {
25
- const { pathname} = useRouter ( ) ;
26
- const [ section , setSection ] = React . useState ( ( ) => inferSection ( pathname ) ) ;
16
+ // This is where we actually are according to the router.
17
+ const section = useActiveSection ( ) ;
18
+
19
+ // Let the user switch tabs there and back without navigating.
20
+ // Seed the tab state from the router, but keep it independent.
21
+ const [ tab , setTab ] = React . useState ( section ) ;
27
22
28
23
let tree = null ;
29
- switch ( section ) {
24
+ switch ( tab ) {
30
25
case 'home' :
31
26
tree = sidebarHome . routes [ 0 ] ;
32
27
break ;
@@ -41,19 +36,13 @@ export function MobileNav() {
41
36
return (
42
37
< >
43
38
< div className = "sticky top-0 px-5 mb-2 bg-wash dark:bg-wash-dark flex justify-end border-b border-border dark:border-border-dark items-center self-center w-full z-10" >
44
- < TabButton
45
- isActive = { section === 'home' }
46
- onClick = { ( ) => setSection ( 'home' ) } >
39
+ < TabButton isActive = { tab === 'home' } onClick = { ( ) => setTab ( 'home' ) } >
47
40
Home
48
41
</ TabButton >
49
- < TabButton
50
- isActive = { section === 'learn' }
51
- onClick = { ( ) => setSection ( 'learn' ) } >
42
+ < TabButton isActive = { tab === 'learn' } onClick = { ( ) => setTab ( 'learn' ) } >
52
43
Learn
53
44
</ TabButton >
54
- < TabButton
55
- isActive = { section === 'apis' }
56
- onClick = { ( ) => setSection ( 'apis' ) } >
45
+ < TabButton isActive = { tab === 'apis' } onClick = { ( ) => setTab ( 'apis' ) } >
57
46
API
58
47
</ TabButton >
59
48
</ div >
0 commit comments