@tanstack/react-router 1.45.15 → 1.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/router.ts CHANGED
@@ -153,218 +153,271 @@ export interface RouterOptions<
153
153
  > {
154
154
  /**
155
155
  * The history object that will be used to manage the browser history.
156
+ *
156
157
  * If not provided, a new createBrowserHistory instance will be created and used.
158
+ *
157
159
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#history-property)
158
160
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/history-types)
159
161
  */
160
162
  history?: RouterHistory
161
163
  /**
162
164
  * A function that will be used to stringify search params when generating links.
163
- * Defaults to `defaultStringifySearch`.
165
+ *
166
+ * @default defaultStringifySearch
164
167
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#stringifysearch-method)
165
168
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/custom-search-param-serialization)
166
169
  */
167
170
  stringifySearch?: SearchSerializer
168
171
  /**
169
172
  * A function that will be used to parse search params when parsing the current location.
170
- * Defaults to `defaultParseSearch`.
173
+ *
174
+ * @default defaultParseSearch
171
175
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#parsesearch-method)
172
176
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/custom-search-param-serialization)
173
177
  */
174
178
  parseSearch?: SearchParser
175
179
  /**
176
- * Defaults to `false`
177
180
  * If `false`, routes will not be preloaded by default in any way.
181
+ *
178
182
  * If `'intent'`, routes will be preloaded by default when the user hovers over a link or a `touchstart` event is detected on a `<Link>`.
183
+ *
184
+ * If `'viewport'`, routes will be preloaded by default when they are within the viewport.
185
+ *
186
+ * @default false
179
187
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultpreload-property)
180
188
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/preloading)
181
189
  */
182
- defaultPreload?: false | 'intent'
190
+ defaultPreload?: false | 'intent' | 'viewport'
183
191
  /**
184
- * Defaults to 50
185
192
  * The delay in milliseconds that a route must be hovered over or touched before it is preloaded.
193
+ *
194
+ * @default 50
186
195
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultpreloaddelay-property)
187
196
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/preloading#preload-delay)
188
197
  */
189
198
  defaultPreloadDelay?: number
190
199
  /**
191
- * Defaults to `Outlet`
192
200
  * The default `component` a route should use if no component is provided.
201
+ *
202
+ * @default Outlet
193
203
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultcomponent-property)
194
204
  */
195
205
  defaultComponent?: RouteComponent
196
206
  /**
197
- * Defaults to `ErrorComponent`
198
207
  * The default `errorComponent` a route should use if no error component is provided.
208
+ *
209
+ * @default ErrorComponent
199
210
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaulterrorcomponent-property)
200
211
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#handling-errors-with-routeoptionserrorcomponent)
201
212
  */
202
213
  defaultErrorComponent?: ErrorRouteComponent
203
214
  /**
204
215
  * The default `pendingComponent` a route should use if no pending component is provided.
216
+ *
205
217
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultpendingcomponent-property)
206
218
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#showing-a-pending-component)
207
219
  */
208
220
  defaultPendingComponent?: RouteComponent
209
221
  /**
210
- * Defaults to `1000`
211
222
  * The default `pendingMs` a route should use if no pendingMs is provided.
223
+ *
224
+ * @default 1000
212
225
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultpendingms-property)
213
226
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#avoiding-pending-component-flash)
214
227
  */
215
228
  defaultPendingMs?: number
216
229
  /**
217
- * Defaults to `500`
218
230
  * The default `pendingMinMs` a route should use if no pendingMinMs is provided.
231
+ *
232
+ * @default 500
219
233
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultpendingminms-property)
220
234
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#avoiding-pending-component-flash)
221
235
  */
222
236
  defaultPendingMinMs?: number
223
237
  /**
224
- * Defaults to `0`
225
- * The default `staleTime` a route should use if no staleTime is
238
+ * The default `staleTime` a route should use if no staleTime is provided. This is the time in milliseconds that a route will be considered fresh.
239
+ *
240
+ * @default 0
226
241
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultstaletime-property)
227
242
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#key-options)
228
243
  */
229
244
  defaultStaleTime?: number
230
245
  /**
231
- * Defaults to `30_000` ms (30 seconds)
232
246
  * The default `preloadStaleTime` a route should use if no preloadStaleTime is provided.
247
+ *
248
+ * @default 30_000 `(30 seconds)`
233
249
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultpreloadstaletime-property)
234
250
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/preloading)
235
251
  */
236
252
  defaultPreloadStaleTime?: number
237
253
  /**
238
254
  * Defaults to `routerOptions.defaultGcTime`, which defaults to 30 minutes.
255
+ *
239
256
  * The default `defaultPreloadGcTime` a route should use if no preloadGcTime is provided.
257
+ *
240
258
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultpreloadgctime-property)
241
259
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/preloading)
242
260
  */
243
261
  defaultPreloadGcTime?: number
244
262
  /**
245
263
  * The default `onCatch` handler for errors caught by the Router ErrorBoundary
264
+ *
246
265
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultoncatch-property)
247
266
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#handling-errors-with-routeoptionsoncatch)
248
267
  */
249
268
  defaultOnCatch?: (error: Error, errorInfo: React.ErrorInfo) => void
269
+ /**
270
+ * If set to `true`, all navigation actions will wrapped in `document.startViewTransition()`.
271
+ */
250
272
  defaultViewTransition?: boolean
251
273
  /**
252
274
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/not-found-errors#the-notfoundmode-option)
253
275
  */
254
276
  notFoundMode?: 'root' | 'fuzzy'
255
277
  /**
256
- * Defaults to 30 minutes.
257
278
  * The default `gcTime` a route should use if no
279
+ *
280
+ * @default 1_800_000 `(30 minutes)`
258
281
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultgctime-property)
259
282
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#key-options)
260
283
  */
261
284
  defaultGcTime?: number
262
285
  /**
263
- * Defaults to `false`
264
286
  * If `true`, all routes will be matched as case-sensitive.
287
+ *
288
+ * @default false
265
289
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#casesensitive-property)
266
290
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/route-trees#case-sensitivity)
267
291
  */
268
292
  caseSensitive?: boolean
269
293
  /**
270
- * Required
294
+ * __Required*__
295
+ *
271
296
  * The route tree that will be used to configure the router instance.
297
+ *
272
298
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#routetree-property)
273
299
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/route-trees)
274
300
  */
275
301
  routeTree?: TRouteTree
276
302
  /**
277
- * Defaults to `/`
278
303
  * The basepath for then entire router. This is useful for mounting a router instance at a subpath.
304
+ *
305
+ * @default '/'
279
306
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#basepath-property)
280
307
  */
281
308
  basepath?: string
282
309
  /**
283
- * Optional or required if the root route was created with [`createRootRouteWithContext()`](https://tanstack.com/router/latest/docs/framework/react/api/router/createRootRouteWithContextFunction).
284
310
  * The root context that will be provided to all routes in the route tree.
311
+ *
285
312
  * This can be used to provide a context to all routes in the tree without having to provide it to each route individually.
313
+ *
314
+ * Optional or required if the root route was created with [`createRootRouteWithContext()`](https://tanstack.com/router/latest/docs/framework/react/api/router/createRootRouteWithContextFunction).
315
+ *
286
316
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#context-property)
287
317
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/router-context)
288
318
  */
289
319
  context?: InferRouterContext<TRouteTree>
290
320
  /**
291
321
  * A function that will be called when the router is dehydrated.
322
+ *
292
323
  * The return value of this function will be serialized and stored in the router's dehydrated state.
324
+ *
293
325
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#dehydrate-method)
294
326
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/external-data-loading#critical-dehydrationhydration)
295
327
  */
296
328
  dehydrate?: () => TDehydrated
297
329
  /**
298
330
  * A function that will be called when the router is hydrated.
331
+ *
299
332
  * The return value of this function will be serialized and stored in the router's dehydrated state.
333
+ *
300
334
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#hydrate-method)
301
335
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/external-data-loading#critical-dehydrationhydration)
302
336
  */
303
337
  hydrate?: (dehydrated: TDehydrated) => void
304
338
  /**
305
339
  * An array of route masks that will be used to mask routes in the route tree.
340
+ *
306
341
  * Route masking is when you display a route at a different path than the one it is configured to match, like a modal popup that when shared will unmask to the modal's content instead of the modal's context.
342
+ *
307
343
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#routemasks-property)
308
344
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/route-masking)
309
345
  */
310
346
  routeMasks?: Array<RouteMask<TRouteTree>>
311
347
  /**
312
- * Defaults to `false`
313
348
  * If `true`, route masks will, by default, be removed when the page is reloaded.
349
+ *
314
350
  * This can be overridden on a per-mask basis by setting the `unmaskOnReload` option on the mask, or on a per-navigation basis by setting the `unmaskOnReload` option in the `Navigate` options.
351
+ *
352
+ * @default false
315
353
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#unmaskonreload-property)
316
354
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/route-masking#unmasking-on-page-reload)
317
355
  */
318
356
  unmaskOnReload?: boolean
319
357
  /**
320
358
  * A component that will be used to wrap the entire router.
359
+ *
321
360
  * This is useful for providing a context to the entire router.
361
+ *
322
362
  * Only non-DOM-rendering components like providers should be used, anything else will cause a hydration error.
363
+ *
323
364
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#wrap-property)
324
365
  */
325
366
  Wrap?: (props: { children: any }) => React.JSX.Element
326
367
  /**
327
368
  * A component that will be used to wrap the inner contents of the router.
369
+ *
328
370
  * This is useful for providing a context to the inner contents of the router where you also need access to the router context and hooks.
371
+ *
329
372
  * Only non-DOM-rendering components like providers should be used, anything else will cause a hydration error.
373
+ *
330
374
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#innerwrap-property)
331
375
  */
332
376
  InnerWrap?: (props: { children: any }) => React.JSX.Element
333
377
  /**
334
- * @deprecated
335
378
  * Use `notFoundComponent` instead.
379
+ *
380
+ * @deprecated
336
381
  * See https://tanstack.com/router/v1/docs/guide/not-found-errors#migrating-from-notfoundroute for more info.
337
382
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#notfoundroute-property)
338
383
  */
339
384
  notFoundRoute?: AnyRoute
340
385
  /**
341
- * Defaults to `NotFound`
342
386
  * The default `notFoundComponent` a route should use if no notFound component is provided.
387
+ *
388
+ * @default NotFound
343
389
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultnotfoundcomponent-property)
344
390
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/not-found-errors#default-router-wide-not-found-handling)
345
391
  */
346
392
  defaultNotFoundComponent?: NotFoundRouteComponent
347
393
  /**
348
394
  * The transformer that will be used when sending data between the server and the client during SSR.
395
+ *
349
396
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#transformer-property)
350
397
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/ssr#data-transformers)
351
398
  */
352
399
  transformer?: RouterTransformer
353
400
  /**
354
401
  * The serializer object that will be used to determine how errors are serialized and deserialized between the server and the client.
402
+ *
355
403
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#errorserializer-property)
356
404
  */
357
405
  errorSerializer?: RouterErrorSerializer<TSerializedError>
358
406
  /**
359
- * Defaults to `never`
360
407
  * Configures how trailing slashes are treated.
361
- * `'always'` will add a trailing slash if not present, `'never'` will remove the trailing slash if present and `'preserve'` will not modify the trailing slash.
408
+ *
409
+ * - `'always'` will add a trailing slash if not present
410
+ * - `'never'` will remove the trailing slash if present
411
+ * - `'preserve'` will not modify the trailing slash.
412
+ *
413
+ * @default 'never'
362
414
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#trailingslash-property)
363
415
  */
364
416
  trailingSlash?: TTrailingSlashOption
365
417
  /**
366
- * Defaults to `typeof document !== 'undefined'`
367
418
  * While usually automatic, sometimes it can be useful to force the router into a server-side state, e.g. when using the router in a non-browser environment that has access to a global.document object.
419
+ *
420
+ * @default typeof document !== 'undefined'
368
421
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#isserver property)
369
422
  */
370
423
  isServer?: boolean
package/src/utils.ts CHANGED
@@ -372,3 +372,87 @@ export function usePrevious<T>(value: T): T | null {
372
372
  // return the previous value only
373
373
  return ref.current.prev
374
374
  }
375
+
376
+ /**
377
+ * React hook to wrap `IntersectionObserver`.
378
+ *
379
+ * This hook will create an `IntersectionObserver` and observe the ref passed to it.
380
+ *
381
+ * When the intersection changes, the callback will be called with the `IntersectionObserverEntry`.
382
+ *
383
+ * @param ref - The ref to observe
384
+ * @param intersectionObserverOptions - The options to pass to the IntersectionObserver
385
+ * @param options - The options to pass to the hook
386
+ * @param callback - The callback to call when the intersection changes
387
+ * @returns The IntersectionObserver instance
388
+ * @example
389
+ * ```tsx
390
+ * const MyComponent = () => {
391
+ * const ref = React.useRef<HTMLDivElement>(null)
392
+ * useIntersectionObserver(ref, { rootMargin: '10px' }, {}, (entry) => {
393
+ * return <div ref={ref} />
394
+ * })
395
+ * ```
396
+ */
397
+ export function useIntersectionObserver<T extends Element>(
398
+ ref: React.RefObject<T>,
399
+ intersectionObserverOptions: IntersectionObserverInit = {},
400
+ options: { disabled?: boolean } = {},
401
+ callback: (entry: IntersectionObserverEntry | undefined) => void,
402
+ ): IntersectionObserver | null {
403
+ const isIntersectionObserverAvailable = React.useRef(
404
+ typeof IntersectionObserver === 'function',
405
+ )
406
+
407
+ const observerRef = React.useRef<IntersectionObserver | null>(null)
408
+
409
+ React.useEffect(() => {
410
+ if (
411
+ !ref.current ||
412
+ !isIntersectionObserverAvailable.current ||
413
+ options.disabled
414
+ ) {
415
+ return
416
+ }
417
+
418
+ observerRef.current = new IntersectionObserver(([entry]) => {
419
+ callback(entry)
420
+ }, intersectionObserverOptions)
421
+
422
+ observerRef.current.observe(ref.current)
423
+
424
+ return () => {
425
+ observerRef.current?.disconnect()
426
+ }
427
+ }, [intersectionObserverOptions, options, ref, callback])
428
+
429
+ return observerRef.current
430
+ }
431
+
432
+ /**
433
+ * React hook to take a `React.ForwardedRef` and returns a `ref` that can be used on a DOM element.
434
+ *
435
+ * @param ref - The forwarded ref
436
+ * @returns The inner ref returned by `useRef`
437
+ * @example
438
+ * ```tsx
439
+ * const MyComponent = React.forwardRef((props, ref) => {
440
+ * const innerRef = useForwardedRef(ref)
441
+ * return <div ref={innerRef} />
442
+ * })
443
+ * ```
444
+ */
445
+ export function useForwardedRef<T>(ref?: React.ForwardedRef<T>) {
446
+ const innerRef = React.useRef<T>(null)
447
+
448
+ React.useEffect(() => {
449
+ if (!ref) return
450
+ if (typeof ref === 'function') {
451
+ ref(innerRef.current)
452
+ } else {
453
+ ref.current = innerRef.current
454
+ }
455
+ })
456
+
457
+ return innerRef
458
+ }