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

main.tsx

214 lines6.0 KB

1import React, { StrictMode } from 'react'
2import ReactDOM from 'react-dom/client'
3import {
4 Link,
5 Outlet,
6 RouterProvider,
7 createRootRoute,
8 createRoute,
9 createRouter,
10 useElementScrollRestoration,
11} from '@tanstack/react-router'
12import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
13import { useVirtualizer } from '@tanstack/react-virtual'
14import './styles.css'
15​
16const rootRoute = createRootRoute({
17 component: RootComponent,
18})
19​
20function RootComponent() {
21 return (
22 <>
23 <div className="p-2 flex gap-2 sticky top-0 border-b bg-gray-100 dark:bg-gray-900">
24 <Link to="/" className="[&.active]:font-bold">
25 Home
26 </Link>{' '}
27 <Link to="/about" className="[&.active]:font-bold">
28 About
29 </Link>
30 <Link to="/about" resetScroll={false}>
31 About (No Reset)
32 </Link>
33 <Link to="/by-element" className="[&.active]:font-bold">
34 By-Element
35 </Link>
36 </div>
37 <Outlet />
38 <TanStackRouterDevtools />
39 </>
40 )
42​
43const indexRoute = createRoute({
44 getParentRoute: () => rootRoute,
45 path: '/',
46 loader: () => new Promise<void>((r) => setTimeout(r, 500)),
47 component: IndexComponent,
48})
49​
50function IndexComponent() {
51 return (
52 <div className="p-2">
53 <h3>Welcome Home!</h3>
54 <div className="space-y-2">
55 {Array.from({ length: 50 }).map((_, i) => (
56 <div
57 key={i}
58 className="h-[100px] p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border"
59 >
60 Home Item {i + 1}
61 </div>
62 ))}
63 </div>
64 </div>
65 )
67​
68const aboutRoute = createRoute({
69 getParentRoute: () => rootRoute,
70 path: '/about',
71 loader: () => new Promise<void>((r) => setTimeout(r, 500)),
72 component: AboutComponent,
73})
74​
75function AboutComponent() {
76 return (
77 <div className="p-2">
78 <div>Hello from About!</div>
79 <div className="space-y-2">
80 {Array.from({ length: 50 }).map((_, i) => (
81 <div
82 key={i}
83 className="h-[100px] p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border"
84 >
85 About Item {i + 1}
86 </div>
87 ))}
88 </div>
89 </div>
90 )
92​
93const byElementRoute = createRoute({
94 getParentRoute: () => rootRoute,
95 path: '/by-element',
96 loader: () => new Promise<void>((r) => setTimeout(r, 500)),
97 component: ByElementComponent,
98})
99​
100function ByElementComponent() {
101 // We need a unique ID for manual scroll restoration on a specific element
102 // It should be as unique as possible for this element across your app
103 const scrollRestorationId = 'myVirtualizedContent'
104​
105 // We use that ID to get the scroll entry for this element
106 const scrollEntry = useElementScrollRestoration({
107 id: scrollRestorationId,
108 })
109​
110 // Let's use TanStack Virtual to virtualize some content!
111 const virtualizerParentRef = React.useRef<HTMLDivElement>(null)
112 const virtualizer = useVirtualizer({
113 count: 10000,
114 getScrollElement: () => virtualizerParentRef.current,
115 estimateSize: () => 100,
116 // We pass the scrollY from the scroll restoration entry to the virtualizer
117 // as the initial offset
118 initialOffset: scrollEntry?.scrollY,
119 })
120​
121 return (
122 <div className="p-2 h-[calc(100vh-41px)] flex flex-col">
123 <div>Hello from By-Element!</div>
124 <div className="h-full min-h-0 flex gap-4">
125 <div className="border rounded-lg p-2 overflow-auto flex-1 space-y-2">
126 {Array.from({ length: 50 }).map((_, i) => (
127 <div
128 key={i}
129 className="h-[100px] p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border"
130 >
131 About Item {i + 1}
132 </div>
133 ))}
134 </div>
135 <div className="flex-1 overflow-auto flex flex-col gap-4">
136 {Array.from({ length: 2 }).map((_, i) => (
137 <div key={i} className="flex-1 border rounded-lg p-2 overflow-auto">
138 <div className="space-y-2">
139 {Array.from({ length: 50 }).map((_, i) => (
140 <div
141 key={i}
142 className="h-[100px] p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border"
143 >
144 About Item {i + 1}
145 </div>
146 ))}
147 </div>
148 </div>
149 ))}
150 <div className="flex-1 flex flex-col min-h-0">
151 <div className="font-bold">Virtualized</div>
152 <div
153 ref={virtualizerParentRef}
154 // We pass the scroll restoration ID to the element
155 // as a custom attribute that will get picked up by the
156 // scroll restoration watcher
157 data-scroll-restoration-id={scrollRestorationId}
158 className="flex-1 border rounded-lg overflow-auto relative"
159 >
160 <div
161 style={{
162 height: `${virtualizer.getTotalSize()}px`,
163 }}
164 >
165 {virtualizer.getVirtualItems().map((item) => (
166 <div
167 key={item.index}
168 className="absolute p-2 pb-0 w-full"
169 style={{
170 height: item.size,
171 top: item.start,
172 }}
173 >
174 <div className="p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border h-full">
175 Virtualized Item {item.index + 1}
176 </div>
177 </div>
178 ))}
179 </div>
180 </div>
181 </div>
182 </div>
183 </div>
184 </div>
185 )
187​
188const routeTree = rootRoute.addChildren([
189 indexRoute,
190 aboutRoute,
191 byElementRoute,
192])
193​
194const router = createRouter({
195 routeTree,
196 defaultPreload: 'intent',
197 scrollRestoration: true,
198})
199​
200declare module '@tanstack/react-router' {
201 interface Register {
202 router: typeof router
203 }
205​
206const rootElement = document.getElementById('app')!
207if (!rootElement.innerHTML) {
208 const root = ReactDOM.createRoot(rootElement)
209 root.render(
210 <StrictMode>
211 <RouterProvider router={router} />
212 </StrictMode>,
213 )