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

main.tsx

280 lines7.7 KB

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