Open History to see the last commit
1import React, { useLayoutEffect } 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 )
41}
42β
43const indexRoute = createRoute({
44 getParentRoute: () => rootRoute,
45 path: '/',
46 loader: () => new Promise<any>((r) => setTimeout(r, 500)),
47 component: IndexComponent,
48})
49β
50function IndexComponent() {
51 useLayoutEffect(() => {
52 window.invokeOrders.push('index-useLayoutEffect')
53 }, [])
54β
55 return (
56 <div className="p-2">
57 <h3 id="greeting" className="bg-red-600">
58 Welcome Home!
59 </h3>
60 <div id="top-message" />
61 <div className="space-y-2">
62 {Array.from({ length: 50 }).map((_, i) => (
63 <div
64 key={i}
65 className="h-[100px] p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border"
66 >
67 Home Item {i + 1}
68 </div>
69 ))}
70 </div>
71 </div>
72 )
73}
74β
75const aboutRoute = createRoute({
76 getParentRoute: () => rootRoute,
77 path: '/about',
78 loader: () => new Promise<any>((r) => setTimeout(r, 500)),
79 component: AboutComponent,
80})
81β
82function AboutComponent() {
83 useLayoutEffect(() => {
84 window.invokeOrders.push('about-useLayoutEffect')
85 }, [])
86 return (
87 <div className="p-2">
88 <h3 id="greeting">Hello from About!</h3>
89 <div className="space-y-2">
90 {Array.from({ length: 50 }).map((_, i) => (
91 <div
92 key={i}
93 className="h-[100px] p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border"
94 >
95 About Item {i + 1}
96 </div>
97 ))}
98 </div>
99 </div>
100 )
101}
102β
103const byElementRoute = createRoute({
104 getParentRoute: () => rootRoute,
105 path: '/by-element',
106 loader: () => new Promise<any>((r) => setTimeout(r, 500)),
107 component: ByElementComponent,
108})
109β
110function ByElementComponent() {
111 // We need a unique ID for manual scroll restoration on a specific element
112 // It should be as unique as possible for this element across your app
113 const scrollRestorationId = 'myVirtualizedContent'
114β
115 // We use that ID to get the scroll entry for this element
116 const scrollEntry = useElementScrollRestoration({
117 id: scrollRestorationId,
118 })
119β
120 // Let's use TanStack Virtual to virtualize some content!
121 const virtualizerParentRef = React.useRef<HTMLDivElement>(null)
122 const virtualizer = useVirtualizer({
123 count: 10000,
124 getScrollElement: () => virtualizerParentRef.current,
125 estimateSize: () => 100,
126 // We pass the scrollY from the scroll restoration entry to the virtualizer
127 // as the initial offset
128 initialOffset: scrollEntry?.scrollY,
129 })
130β
131 return (
132 <div className="p-2 h-[calc(100vh-41px)] flex flex-col">
133 <div>Hello from By-Element!</div>
134 <div className="h-full min-h-0 flex gap-4">
135 <div
136 id="RegularList"
137 className="border rounded-lg p-2 overflow-auto flex-1 space-y-2"
138 >
139 <div className="h-[100px] p-2 rounded-lg bg-red-600 border">
140 First Regular List Item
141 <div id="first-regular-list-item" />
142 </div>
143 {Array.from({ length: 50 }).map((_, i) => (
144 <div
145 key={i}
146 className="h-[100px] p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border"
147 >
148 Regular List Item {i + 1}
149 </div>
150 ))}
151 </div>
152 <div className="flex-1 overflow-auto flex flex-col gap-4">
153 {Array.from({ length: 2 }).map((_, i) => (
154 <div key={i} className="flex-1 border rounded-lg p-2 overflow-auto">
155 <div className="space-y-2">
156 {Array.from({ length: 50 }).map((_, i) => (
157 <div
158 key={i}
159 className="h-[100px] p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border"
160 >
161 About Item {i + 1}
162 </div>
163 ))}
164 </div>
165 </div>
166 ))}
167 <div className="flex-1 flex flex-col min-h-0">
168 <div className="font-bold">Virtualized</div>
169 <div
170 ref={virtualizerParentRef}
171 // We pass the scroll restoration ID to the element
172 // as a custom attribute that will get picked up by the
173 // scroll restoration watcher
174 data-scroll-restoration-id={scrollRestorationId}
175 className="flex-1 border rounded-lg overflow-auto relative"
176 >
177 <div
178 style={{
179 height: `${virtualizer.getTotalSize()}px`,
180 }}
181 >
182 {virtualizer.getVirtualItems().map((item) => (
183 <div
184 key={item.index}
185 className="absolute p-2 pb-0 w-full"
186 style={{
187 height: item.size,
188 top: item.start,
189 }}
190 >
191 <div className="p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border h-full">
192 Virtualized Item {item.index + 1}
193 </div>
194 </div>
195 ))}
196 </div>
197 </div>
198 </div>
199 </div>
200 </div>
201 </div>
202 )
203}
204β
205const fooRoute = createRoute({
206 getParentRoute: () => rootRoute,
207 path: '/foo',
208 loader: () => new Promise<any>((r) => setTimeout(r, 500)),
209 component: FooComponent,
210})
211β
212function FooComponent() {
213 return (
214 <div data-testid="foo-route-component" className="p-2">
215 <h3 id="greeting">Hello from Foo!</h3>
216 <Link to="/bar" data-testid="go-to-bar-link">
217 Go to Bar
218 </Link>
219 <div className="space-y-2">
220 {Array.from({ length: 50 }).map((_, i) => (
221 <div
222 key={i}
223 className="h-[100px] p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border"
224 >
225 Foo Item {i + 1}
226 </div>
227 ))}
228 </div>
229 </div>
230 )
231}
232β
233const barRoute = createRoute({
234 getParentRoute: () => rootRoute,
235 path: '/bar',
236 loader: () => new Promise<any>((r) => setTimeout(r, 500)),
237 component: BarComponent,
238})
239β
240function BarComponent() {
241 return (
242 <div data-testid="bar-route-component" className="p-2">
243 <h3 id="greeting">Hello from Bar!</h3>
244 <div className="space-y-2">
245 {Array.from({ length: 50 }).map((_, i) => (
246 <div
247 key={i}
248 className="h-[100px] p-2 rounded-lg bg-gray-200 dark:bg-gray-800 border"
249 >
250 Bar Item {i + 1}
251 </div>
252 ))}
253 </div>
254 </div>
255 )
256}
257β
258const routeTree = rootRoute.addChildren([
259 indexRoute,
260 aboutRoute,
261 byElementRoute,
262 fooRoute,
263 barRoute,
264])
265β
266const router = createRouter({
267 routeTree,
268 defaultPreload: 'intent',
269 scrollRestoration: true,
270 getScrollRestorationKey: (location) => location.pathname,
271})
272β
273declare global {
274 interface Window {
275 invokeOrders: Array<string>
276 }
277}
278window.invokeOrders = []
279router.subscribe('onBeforeRouteMount', (event) => {
280 window.invokeOrders.push(event.type)
281})
282β
283router.subscribe('onResolved', (event) => {
284 window.invokeOrders.push(event.type)
285})
286β
287declare module '@tanstack/react-router' {
288 interface Register {
289 router: typeof router
290 }
291}
292β
293const rootElement = document.getElementById('app')!
294if (!rootElement.innerHTML) {
295 const root = ReactDOM.createRoot(rootElement)
296 root.render(<RouterProvider router={router} />)
297}