Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

Layout.tsx

139 lines4.7 KB

1import { useState } from 'react'
2import { Link, Outlet, useRouterState } from '@tanstack/react-router'
3​
4const navItems = [
5 { path: '/', label: 'Home', description: 'Overview of all demos' },
6 {
7 path: '/pokemon-rsc',
8 label: 'Pokemon RSC',
9 description: 'Async server components',
10 },
11 {
12 path: '/e-commerce',
13 label: 'eCommerce Demo',
14 description: 'Composite RSC pattern',
15 },
16 {
17 path: '/low-level-api',
18 label: 'Low-Level API',
19 description: 'RSC primitives',
20 },
22​
23export function Layout() {
24 const [sidebarOpen, setSidebarOpen] = useState(false)
25 const routerState = useRouterState()
26 const currentPath = routerState.location.pathname
27​
28 return (
29 <div className="min-h-screen bg-gray-100 flex flex-col">
30 {/* Top Navigation Bar */}
31 <nav className="bg-white shadow-sm border-b border-gray-200 sticky top-0 z-40">
32 <div className="max-w-6xl mx-auto px-4">
33 <div className="flex items-center justify-between h-14">
34 <div className="flex items-center gap-3">
35 <button
36 onClick={() => setSidebarOpen(!sidebarOpen)}
37 className="p-2 rounded-lg hover:bg-gray-100 transition-colors"
38 aria-label="Toggle navigation menu"
39 >
40 <svg
41 className="w-6 h-6 text-gray-700"
42 fill="none"
43 stroke="currentColor"
44 viewBox="0 0 24 24"
45 >
46 {sidebarOpen ? (
47 <path
48 strokeLinecap="round"
49 strokeLinejoin="round"
50 strokeWidth={2}
51 d="M6 18L18 6M6 6l12 12"
52 />
53 ) : (
54 <path
55 strokeLinecap="round"
56 strokeLinejoin="round"
57 strokeWidth={2}
58 d="M4 6h16M4 12h16M4 18h16"
59 />
60 )}
61 </svg>
62 </button>
63 <Link to="/" className="flex items-center gap-2">
64 <span className="text-xl font-bold text-gray-900">
65 TanStack RSC Examples
66 </span>
67 </Link>
68 </div>
69 </div>
70 </div>
71 </nav>
72​
73 <div className="flex flex-1">
74 {/* Sidebar Overlay */}
75 {sidebarOpen && (
76 <div
77 className="fixed inset-0 bg-black/20 z-30 lg:hidden"
78 onClick={() => setSidebarOpen(false)}
79 />
80 )}
81​
82 {/* Sidebar */}
83 <aside
84 className={`fixed lg:sticky top-14 left-0 h-[calc(100vh-3.5rem)] w-64 bg-white border-r border-gray-200 z-40 transform transition-transform duration-200 ease-in-out ${
85 sidebarOpen ? 'translate-x-0' : '-translate-x-full lg:translate-x-0'
86 }`}
87 >
88 <nav className="p-4 space-y-1">
89 {navItems.map((item) => (
90 <Link
91 key={item.path}
92 to={item.path}
93 onClick={() => setSidebarOpen(false)}
94 className={`block px-4 py-3 rounded-lg transition-colors ${
95 currentPath === item.path
96 ? 'bg-indigo-50 text-indigo-700 border-l-4 border-indigo-500'
97 : 'text-gray-700 hover:bg-gray-50'
98 }`}
99 >
100 <div className="font-medium">{item.label}</div>
101 <div className="text-xs text-gray-500 mt-0.5">
102 {item.description}
103 </div>
104 </Link>
105 ))}
106 </nav>
107 </aside>
108​
109 {/* Main Content */}
110 <main className="flex-1 min-w-0">
111 <Outlet />
112​
113 {/* Legend Footer */}
114 <footer className="mt-12 py-8 border-t border-gray-200 bg-white">
115 <div className="max-w-4xl mx-auto px-4">
116 <div className="flex flex-wrap gap-6 justify-center">
117 <div className="flex items-center gap-2">
118 <div className="w-6 h-6 rounded border-4 border-solid border-blue-500" />
119 <span className="text-gray-600 font-medium">
120 Server Component
121 </span>
122 </div>
123 <div className="flex items-center gap-2">
124 <div className="w-6 h-6 rounded border-4 border-dashed border-green-500" />
125 <span className="text-gray-600 font-medium">
126 Client Component
127 </span>
128 </div>
129 </div>
130 <p className="text-center text-gray-400 text-sm mt-4">
131 TanStack Start RSC Examples
132 </p>
133 </div>
134 </footer>
135 </main>
136 </div>
137 </div>
138 )