@tanstack/react-router 1.132.0-alpha.8 → 1.132.2

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.
Files changed (67) hide show
  1. package/dist/cjs/Asset.cjs +32 -0
  2. package/dist/cjs/Asset.cjs.map +1 -1
  3. package/dist/cjs/ScriptOnce.cjs +5 -4
  4. package/dist/cjs/ScriptOnce.cjs.map +1 -1
  5. package/dist/cjs/ScriptOnce.d.cts +1 -3
  6. package/dist/cjs/fileRoute.cjs.map +1 -1
  7. package/dist/cjs/fileRoute.d.cts +2 -2
  8. package/dist/cjs/index.cjs +8 -4
  9. package/dist/cjs/index.cjs.map +1 -1
  10. package/dist/cjs/index.d.cts +4 -2
  11. package/dist/cjs/link.cjs +38 -26
  12. package/dist/cjs/link.cjs.map +1 -1
  13. package/dist/cjs/route.cjs +4 -1
  14. package/dist/cjs/route.cjs.map +1 -1
  15. package/dist/cjs/route.d.cts +10 -10
  16. package/dist/cjs/ssr/renderRouterToString.cjs +1 -0
  17. package/dist/cjs/ssr/renderRouterToString.cjs.map +1 -1
  18. package/dist/cjs/typePrimitives.d.cts +6 -0
  19. package/dist/cjs/useNavigate.cjs +2 -6
  20. package/dist/cjs/useNavigate.cjs.map +1 -1
  21. package/dist/cjs/useParams.cjs +3 -2
  22. package/dist/cjs/useParams.cjs.map +1 -1
  23. package/dist/esm/Asset.js +32 -0
  24. package/dist/esm/Asset.js.map +1 -1
  25. package/dist/esm/ScriptOnce.d.ts +1 -3
  26. package/dist/esm/ScriptOnce.js +5 -4
  27. package/dist/esm/ScriptOnce.js.map +1 -1
  28. package/dist/esm/fileRoute.d.ts +2 -2
  29. package/dist/esm/fileRoute.js.map +1 -1
  30. package/dist/esm/index.d.ts +4 -2
  31. package/dist/esm/index.js +3 -2
  32. package/dist/esm/link.js +38 -26
  33. package/dist/esm/link.js.map +1 -1
  34. package/dist/esm/route.d.ts +10 -10
  35. package/dist/esm/route.js +4 -1
  36. package/dist/esm/route.js.map +1 -1
  37. package/dist/esm/ssr/renderRouterToString.js +1 -0
  38. package/dist/esm/ssr/renderRouterToString.js.map +1 -1
  39. package/dist/esm/typePrimitives.d.ts +6 -0
  40. package/dist/esm/useNavigate.js +2 -6
  41. package/dist/esm/useNavigate.js.map +1 -1
  42. package/dist/esm/useParams.js +3 -2
  43. package/dist/esm/useParams.js.map +1 -1
  44. package/dist/llms/rules/guide.d.ts +1 -1
  45. package/dist/llms/rules/guide.js +35 -3
  46. package/dist/llms/rules/routing.d.ts +1 -1
  47. package/dist/llms/rules/routing.js +2 -2
  48. package/dist/llms/rules/setup-and-architecture.d.ts +1 -1
  49. package/dist/llms/rules/setup-and-architecture.js +19 -19
  50. package/package.json +3 -3
  51. package/src/Asset.tsx +44 -0
  52. package/src/ScriptOnce.tsx +6 -8
  53. package/src/fileRoute.ts +10 -3
  54. package/src/index.tsx +5 -2
  55. package/src/link.tsx +42 -40
  56. package/src/route.tsx +62 -23
  57. package/src/ssr/renderRouterToString.tsx +1 -0
  58. package/src/typePrimitives.ts +1 -1
  59. package/src/useNavigate.tsx +2 -8
  60. package/src/useParams.tsx +5 -3
  61. package/dist/cjs/useActiveLocation.cjs +0 -39
  62. package/dist/cjs/useActiveLocation.cjs.map +0 -1
  63. package/dist/cjs/useActiveLocation.d.cts +0 -7
  64. package/dist/esm/useActiveLocation.d.ts +0 -7
  65. package/dist/esm/useActiveLocation.js +0 -39
  66. package/dist/esm/useActiveLocation.js.map +0 -1
  67. package/src/useActiveLocation.ts +0 -57
package/src/route.tsx CHANGED
@@ -159,7 +159,7 @@ export class RouteApi<
159
159
  }
160
160
 
161
161
  export class Route<
162
- in out TRegister extends Register = Register,
162
+ in out TRegister = unknown,
163
163
  in out TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute,
164
164
  in out TPath extends RouteConstraints['TPath'] = '/',
165
165
  in out TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
@@ -182,6 +182,8 @@ export class Route<
182
182
  in out TChildren = unknown,
183
183
  in out TFileRouteTypes = unknown,
184
184
  in out TSSR = unknown,
185
+ in out TServerMiddlewares = unknown,
186
+ in out THandlers = undefined,
185
187
  >
186
188
  extends BaseRoute<
187
189
  TRegister,
@@ -199,7 +201,9 @@ export class Route<
199
201
  TLoaderFn,
200
202
  TChildren,
201
203
  TFileRouteTypes,
202
- TSSR
204
+ TSSR,
205
+ TServerMiddlewares,
206
+ THandlers
203
207
  >
204
208
  implements
205
209
  RouteCore<
@@ -218,7 +222,9 @@ export class Route<
218
222
  TLoaderFn,
219
223
  TChildren,
220
224
  TFileRouteTypes,
221
- TSSR
225
+ TSSR,
226
+ TServerMiddlewares,
227
+ THandlers
222
228
  >
223
229
  {
224
230
  /**
@@ -239,7 +245,9 @@ export class Route<
239
245
  TRouterContext,
240
246
  TRouteContextFn,
241
247
  TBeforeLoadFn,
242
- TSSR
248
+ TSSR,
249
+ TServerMiddlewares,
250
+ THandlers
243
251
  >,
244
252
  ) {
245
253
  super(options)
@@ -300,7 +308,7 @@ export class Route<
300
308
  }
301
309
 
302
310
  export function createRoute<
303
- TRegister extends Register = Register,
311
+ TRegister = unknown,
304
312
  TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute,
305
313
  TPath extends RouteConstraints['TPath'] = '/',
306
314
  TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
@@ -321,6 +329,7 @@ export function createRoute<
321
329
  TLoaderFn = undefined,
322
330
  TChildren = unknown,
323
331
  TSSR = unknown,
332
+ const TServerMiddlewares = unknown,
324
333
  >(
325
334
  options: RouteOptions<
326
335
  TRegister,
@@ -336,7 +345,8 @@ export function createRoute<
336
345
  AnyContext,
337
346
  TRouteContextFn,
338
347
  TBeforeLoadFn,
339
- TSSR
348
+ TSSR,
349
+ TServerMiddlewares
340
350
  >,
341
351
  ): Route<
342
352
  TRegister,
@@ -353,7 +363,8 @@ export function createRoute<
353
363
  TLoaderDeps,
354
364
  TLoaderFn,
355
365
  TChildren,
356
- TSSR
366
+ TSSR,
367
+ TServerMiddlewares
357
368
  > {
358
369
  return new Route<
359
370
  TRegister,
@@ -370,8 +381,12 @@ export function createRoute<
370
381
  TLoaderDeps,
371
382
  TLoaderFn,
372
383
  TChildren,
373
- TSSR
374
- >(options)
384
+ TSSR,
385
+ TServerMiddlewares
386
+ >(
387
+ // TODO: Help us TypeChris, you're our only hope!
388
+ options as any,
389
+ )
375
390
  }
376
391
 
377
392
  export type AnyRootRoute = RootRoute<
@@ -383,18 +398,21 @@ export type AnyRootRoute = RootRoute<
383
398
  any,
384
399
  any,
385
400
  any,
401
+ any,
402
+ any,
386
403
  any
387
404
  >
388
405
 
389
406
  export function createRootRouteWithContext<TRouterContext extends {}>() {
390
407
  return <
391
- TRegister extends Register = Register,
408
+ TRegister = Register,
392
409
  TRouteContextFn = AnyContext,
393
410
  TBeforeLoadFn = AnyContext,
394
411
  TSearchValidator = undefined,
395
412
  TLoaderDeps extends Record<string, any> = {},
396
413
  TLoaderFn = undefined,
397
414
  TSSR = unknown,
415
+ TServerMiddlewares = unknown,
398
416
  >(
399
417
  options?: RootRouteOptions<
400
418
  TRegister,
@@ -404,7 +422,8 @@ export function createRootRouteWithContext<TRouterContext extends {}>() {
404
422
  TBeforeLoadFn,
405
423
  TLoaderDeps,
406
424
  TLoaderFn,
407
- TSSR
425
+ TSSR,
426
+ TServerMiddlewares
408
427
  >,
409
428
  ) => {
410
429
  return createRootRoute<
@@ -415,7 +434,8 @@ export function createRootRouteWithContext<TRouterContext extends {}>() {
415
434
  TBeforeLoadFn,
416
435
  TLoaderDeps,
417
436
  TLoaderFn,
418
- TSSR
437
+ TSSR,
438
+ TServerMiddlewares
419
439
  >(options as any)
420
440
  }
421
441
  }
@@ -426,7 +446,7 @@ export function createRootRouteWithContext<TRouterContext extends {}>() {
426
446
  export const rootRouteWithContext = createRootRouteWithContext
427
447
 
428
448
  export class RootRoute<
429
- in out TRegister extends Register = Register,
449
+ in out TRegister = unknown,
430
450
  in out TSearchValidator = undefined,
431
451
  in out TRouterContext = {},
432
452
  in out TRouteContextFn = AnyContext,
@@ -436,6 +456,8 @@ export class RootRoute<
436
456
  in out TChildren = unknown,
437
457
  in out TFileRouteTypes = unknown,
438
458
  in out TSSR = unknown,
459
+ in out TServerMiddlewares = unknown,
460
+ in out THandlers = undefined,
439
461
  >
440
462
  extends BaseRootRoute<
441
463
  TRegister,
@@ -447,7 +469,9 @@ export class RootRoute<
447
469
  TLoaderFn,
448
470
  TChildren,
449
471
  TFileRouteTypes,
450
- TSSR
472
+ TSSR,
473
+ TServerMiddlewares,
474
+ THandlers
451
475
  >
452
476
  implements
453
477
  RootRouteCore<
@@ -460,7 +484,9 @@ export class RootRoute<
460
484
  TLoaderFn,
461
485
  TChildren,
462
486
  TFileRouteTypes,
463
- TSSR
487
+ TSSR,
488
+ TServerMiddlewares,
489
+ THandlers
464
490
  >
465
491
  {
466
492
  /**
@@ -475,7 +501,9 @@ export class RootRoute<
475
501
  TBeforeLoadFn,
476
502
  TLoaderDeps,
477
503
  TLoaderFn,
478
- TSSR
504
+ TSSR,
505
+ TServerMiddlewares,
506
+ THandlers
479
507
  >,
480
508
  ) {
481
509
  super(options)
@@ -536,7 +564,7 @@ export class RootRoute<
536
564
  }
537
565
 
538
566
  export function createRootRoute<
539
- TRegister extends Register = Register,
567
+ TRegister = Register,
540
568
  TSearchValidator = undefined,
541
569
  TRouterContext = {},
542
570
  TRouteContextFn = AnyContext,
@@ -544,6 +572,8 @@ export function createRootRoute<
544
572
  TLoaderDeps extends Record<string, any> = {},
545
573
  TLoaderFn = undefined,
546
574
  TSSR = unknown,
575
+ const TServerMiddlewares = unknown,
576
+ THandlers = undefined,
547
577
  >(
548
578
  options?: RootRouteOptions<
549
579
  TRegister,
@@ -553,7 +583,9 @@ export function createRootRoute<
553
583
  TBeforeLoadFn,
554
584
  TLoaderDeps,
555
585
  TLoaderFn,
556
- TSSR
586
+ TSSR,
587
+ TServerMiddlewares,
588
+ THandlers
557
589
  >,
558
590
  ): RootRoute<
559
591
  TRegister,
@@ -565,7 +597,9 @@ export function createRootRoute<
565
597
  TLoaderFn,
566
598
  unknown,
567
599
  unknown,
568
- TSSR
600
+ TSSR,
601
+ TServerMiddlewares,
602
+ THandlers
569
603
  > {
570
604
  return new RootRoute<
571
605
  TRegister,
@@ -577,7 +611,9 @@ export function createRootRoute<
577
611
  TLoaderFn,
578
612
  unknown,
579
613
  unknown,
580
- TSSR
614
+ TSSR,
615
+ TServerMiddlewares,
616
+ THandlers
581
617
  >(options)
582
618
  }
583
619
 
@@ -611,7 +647,7 @@ export type ErrorRouteComponent = AsyncRouteComponent<ErrorComponentProps>
611
647
  export type NotFoundRouteComponent = RouteTypes<NotFoundRouteProps>['component']
612
648
 
613
649
  export class NotFoundRoute<
614
- TRegister extends Register,
650
+ TRegister,
615
651
  TParentRoute extends AnyRootRoute,
616
652
  TRouterContext = AnyContext,
617
653
  TRouteContextFn = AnyContext,
@@ -621,6 +657,7 @@ export class NotFoundRoute<
621
657
  TLoaderFn = undefined,
622
658
  TChildren = unknown,
623
659
  TSSR = unknown,
660
+ TServerMiddlewares = unknown,
624
661
  > extends Route<
625
662
  TRegister,
626
663
  TParentRoute,
@@ -636,7 +673,8 @@ export class NotFoundRoute<
636
673
  TLoaderDeps,
637
674
  TLoaderFn,
638
675
  TChildren,
639
- TSSR
676
+ TSSR,
677
+ TServerMiddlewares
640
678
  > {
641
679
  constructor(
642
680
  options: Omit<
@@ -654,7 +692,8 @@ export class NotFoundRoute<
654
692
  TRouterContext,
655
693
  TRouteContextFn,
656
694
  TBeforeLoadFn,
657
- TSSR
695
+ TSSR,
696
+ TServerMiddlewares
658
697
  >,
659
698
  | 'caseSensitive'
660
699
  | 'parseParams'
@@ -13,6 +13,7 @@ export const renderRouterToString = async ({
13
13
  }) => {
14
14
  try {
15
15
  let html = ReactDOMServer.renderToString(children)
16
+ router.serverSsr!.setRenderFinished()
16
17
  const injectedHtml = await Promise.all(router.serverSsr!.injectedHtml).then(
17
18
  (htmls) => htmls.join(''),
18
19
  )
@@ -32,7 +32,7 @@ export type ValidateLinkOptions<
32
32
  >
33
33
 
34
34
  /**
35
- * @internal
35
+ * @private
36
36
  */
37
37
  export type InferStructuralSharing<TOptions> = TOptions extends {
38
38
  structuralSharing: infer TStructuralSharing
@@ -1,6 +1,5 @@
1
1
  import * as React from 'react'
2
2
  import { useRouter } from './useRouter'
3
- import { useActiveLocation } from './useActiveLocation'
4
3
  import type {
5
4
  AnyRouter,
6
5
  FromPathOption,
@@ -17,19 +16,14 @@ export function useNavigate<
17
16
  }): UseNavigateResult<TDefaultFrom> {
18
17
  const router = useRouter()
19
18
 
20
- const { getFromPath, activeLocation } = useActiveLocation()
21
-
22
19
  return React.useCallback(
23
20
  (options: NavigateOptions) => {
24
- const from = getFromPath(options.from ?? _defaultOpts?.from)
25
-
26
21
  return router.navigate({
27
22
  ...options,
28
- from,
23
+ from: options.from ?? _defaultOpts?.from,
29
24
  })
30
25
  },
31
- // eslint-disable-next-line react-hooks/exhaustive-deps
32
- [_defaultOpts?.from, router, getFromPath, activeLocation],
26
+ [_defaultOpts?.from, router],
33
27
  ) as UseNavigateResult<TDefaultFrom>
34
28
  }
35
29
 
package/src/useParams.tsx CHANGED
@@ -83,11 +83,13 @@ export function useParams<
83
83
  > {
84
84
  return useMatch({
85
85
  from: opts.from!,
86
- strict: opts.strict,
87
86
  shouldThrow: opts.shouldThrow,
88
87
  structuralSharing: opts.structuralSharing,
89
- select: (match: any) => {
90
- return opts.select ? opts.select(match.params) : match.params
88
+ strict: opts.strict,
89
+ select: (match) => {
90
+ const params = opts.strict === false ? match.params : match._strictParams
91
+
92
+ return opts.select ? opts.select(params) : params
91
93
  },
92
94
  }) as any
93
95
  }
@@ -1,39 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const routerCore = require("@tanstack/router-core");
4
- const React = require("react");
5
- const useRouter = require("./useRouter.cjs");
6
- const useMatch = require("./useMatch.cjs");
7
- const useRouterState = require("./useRouterState.cjs");
8
- const useActiveLocation = (location) => {
9
- const router = useRouter.useRouter();
10
- const routerLocation = useRouterState.useRouterState({ select: (state) => state.location });
11
- const [activeLocation, setActiveLocation] = React.useState(
12
- routerLocation
13
- );
14
- const [customActiveLocation, setCustomActiveLocation] = React.useState(location);
15
- React.useEffect(() => {
16
- setActiveLocation(customActiveLocation ?? routerLocation);
17
- }, [routerLocation, customActiveLocation]);
18
- const matchIndex = useMatch.useMatch({
19
- strict: false,
20
- select: (match) => match.index
21
- });
22
- const getFromPath = React.useCallback(
23
- (from) => {
24
- const activeLocationMatches = router.matchRoutes(activeLocation, {
25
- _buildLocation: false
26
- });
27
- const activeLocationMatch = routerCore.last(activeLocationMatches);
28
- return from ?? activeLocationMatch?.fullPath ?? router.state.matches[matchIndex].fullPath;
29
- },
30
- [activeLocation, matchIndex, router]
31
- );
32
- return {
33
- activeLocation,
34
- getFromPath,
35
- setActiveLocation: setCustomActiveLocation
36
- };
37
- };
38
- exports.useActiveLocation = useActiveLocation;
39
- //# sourceMappingURL=useActiveLocation.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useActiveLocation.cjs","sources":["../../src/useActiveLocation.ts"],"sourcesContent":["import { last } from '@tanstack/router-core'\nimport { useCallback, useEffect, useState } from 'react'\nimport { useRouter } from './useRouter'\nimport { useMatch } from './useMatch'\nimport { useRouterState } from './useRouterState'\nimport type { ParsedLocation } from '@tanstack/router-core'\n\nexport type UseActiveLocationResult = {\n activeLocation: ParsedLocation\n getFromPath: (from?: string) => string\n setActiveLocation: (location?: ParsedLocation) => void\n}\n\nexport const useActiveLocation = (\n location?: ParsedLocation,\n): UseActiveLocationResult => {\n const router = useRouter()\n const routerLocation = useRouterState({ select: (state) => state.location })\n const [activeLocation, setActiveLocation] = useState<ParsedLocation>(\n location ?? routerLocation,\n )\n const [customActiveLocation, setCustomActiveLocation] = useState<\n ParsedLocation | undefined\n >(location)\n\n useEffect(() => {\n setActiveLocation(customActiveLocation ?? routerLocation)\n }, [routerLocation, customActiveLocation])\n\n const matchIndex = useMatch({\n strict: false,\n select: (match) => match.index,\n })\n\n const getFromPath = useCallback(\n (from?: string) => {\n const activeLocationMatches = router.matchRoutes(activeLocation, {\n _buildLocation: false,\n })\n\n const activeLocationMatch = last(activeLocationMatches)\n\n return (\n from ??\n activeLocationMatch?.fullPath ??\n router.state.matches[matchIndex]!.fullPath\n )\n },\n [activeLocation, matchIndex, router],\n )\n\n return {\n activeLocation,\n getFromPath,\n setActiveLocation: setCustomActiveLocation,\n }\n}\n"],"names":["useRouter","useRouterState","useState","useEffect","useMatch","useCallback","last"],"mappings":";;;;;;;AAaO,MAAM,oBAAoB,CAC/B,aAC4B;AAC5B,QAAM,SAASA,UAAAA,UAAA;AACf,QAAM,iBAAiBC,eAAAA,eAAe,EAAE,QAAQ,CAAC,UAAU,MAAM,UAAU;AAC3E,QAAM,CAAC,gBAAgB,iBAAiB,IAAIC,MAAAA;AAAAA,IAC9B;AAAA,EAAA;AAEd,QAAM,CAAC,sBAAsB,uBAAuB,IAAIA,MAAAA,SAEtD,QAAQ;AAEVC,QAAAA,UAAU,MAAM;AACd,sBAAkB,wBAAwB,cAAc;AAAA,EAC1D,GAAG,CAAC,gBAAgB,oBAAoB,CAAC;AAEzC,QAAM,aAAaC,SAAAA,SAAS;AAAA,IAC1B,QAAQ;AAAA,IACR,QAAQ,CAAC,UAAU,MAAM;AAAA,EAAA,CAC1B;AAED,QAAM,cAAcC,MAAAA;AAAAA,IAClB,CAAC,SAAkB;AACjB,YAAM,wBAAwB,OAAO,YAAY,gBAAgB;AAAA,QAC/D,gBAAgB;AAAA,MAAA,CACjB;AAED,YAAM,sBAAsBC,WAAAA,KAAK,qBAAqB;AAEtD,aACE,QACA,qBAAqB,YACrB,OAAO,MAAM,QAAQ,UAAU,EAAG;AAAA,IAEtC;AAAA,IACA,CAAC,gBAAgB,YAAY,MAAM;AAAA,EAAA;AAGrC,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,mBAAmB;AAAA,EAAA;AAEvB;;"}
@@ -1,7 +0,0 @@
1
- import { ParsedLocation } from '@tanstack/router-core';
2
- export type UseActiveLocationResult = {
3
- activeLocation: ParsedLocation;
4
- getFromPath: (from?: string) => string;
5
- setActiveLocation: (location?: ParsedLocation) => void;
6
- };
7
- export declare const useActiveLocation: (location?: ParsedLocation) => UseActiveLocationResult;
@@ -1,7 +0,0 @@
1
- import { ParsedLocation } from '@tanstack/router-core';
2
- export type UseActiveLocationResult = {
3
- activeLocation: ParsedLocation;
4
- getFromPath: (from?: string) => string;
5
- setActiveLocation: (location?: ParsedLocation) => void;
6
- };
7
- export declare const useActiveLocation: (location?: ParsedLocation) => UseActiveLocationResult;
@@ -1,39 +0,0 @@
1
- import { last } from "@tanstack/router-core";
2
- import { useState, useEffect, useCallback } from "react";
3
- import { useRouter } from "./useRouter.js";
4
- import { useMatch } from "./useMatch.js";
5
- import { useRouterState } from "./useRouterState.js";
6
- const useActiveLocation = (location) => {
7
- const router = useRouter();
8
- const routerLocation = useRouterState({ select: (state) => state.location });
9
- const [activeLocation, setActiveLocation] = useState(
10
- routerLocation
11
- );
12
- const [customActiveLocation, setCustomActiveLocation] = useState(location);
13
- useEffect(() => {
14
- setActiveLocation(customActiveLocation ?? routerLocation);
15
- }, [routerLocation, customActiveLocation]);
16
- const matchIndex = useMatch({
17
- strict: false,
18
- select: (match) => match.index
19
- });
20
- const getFromPath = useCallback(
21
- (from) => {
22
- const activeLocationMatches = router.matchRoutes(activeLocation, {
23
- _buildLocation: false
24
- });
25
- const activeLocationMatch = last(activeLocationMatches);
26
- return from ?? activeLocationMatch?.fullPath ?? router.state.matches[matchIndex].fullPath;
27
- },
28
- [activeLocation, matchIndex, router]
29
- );
30
- return {
31
- activeLocation,
32
- getFromPath,
33
- setActiveLocation: setCustomActiveLocation
34
- };
35
- };
36
- export {
37
- useActiveLocation
38
- };
39
- //# sourceMappingURL=useActiveLocation.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useActiveLocation.js","sources":["../../src/useActiveLocation.ts"],"sourcesContent":["import { last } from '@tanstack/router-core'\nimport { useCallback, useEffect, useState } from 'react'\nimport { useRouter } from './useRouter'\nimport { useMatch } from './useMatch'\nimport { useRouterState } from './useRouterState'\nimport type { ParsedLocation } from '@tanstack/router-core'\n\nexport type UseActiveLocationResult = {\n activeLocation: ParsedLocation\n getFromPath: (from?: string) => string\n setActiveLocation: (location?: ParsedLocation) => void\n}\n\nexport const useActiveLocation = (\n location?: ParsedLocation,\n): UseActiveLocationResult => {\n const router = useRouter()\n const routerLocation = useRouterState({ select: (state) => state.location })\n const [activeLocation, setActiveLocation] = useState<ParsedLocation>(\n location ?? routerLocation,\n )\n const [customActiveLocation, setCustomActiveLocation] = useState<\n ParsedLocation | undefined\n >(location)\n\n useEffect(() => {\n setActiveLocation(customActiveLocation ?? routerLocation)\n }, [routerLocation, customActiveLocation])\n\n const matchIndex = useMatch({\n strict: false,\n select: (match) => match.index,\n })\n\n const getFromPath = useCallback(\n (from?: string) => {\n const activeLocationMatches = router.matchRoutes(activeLocation, {\n _buildLocation: false,\n })\n\n const activeLocationMatch = last(activeLocationMatches)\n\n return (\n from ??\n activeLocationMatch?.fullPath ??\n router.state.matches[matchIndex]!.fullPath\n )\n },\n [activeLocation, matchIndex, router],\n )\n\n return {\n activeLocation,\n getFromPath,\n setActiveLocation: setCustomActiveLocation,\n }\n}\n"],"names":[],"mappings":";;;;;AAaO,MAAM,oBAAoB,CAC/B,aAC4B;AAC5B,QAAM,SAAS,UAAA;AACf,QAAM,iBAAiB,eAAe,EAAE,QAAQ,CAAC,UAAU,MAAM,UAAU;AAC3E,QAAM,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC9B;AAAA,EAAA;AAEd,QAAM,CAAC,sBAAsB,uBAAuB,IAAI,SAEtD,QAAQ;AAEV,YAAU,MAAM;AACd,sBAAkB,wBAAwB,cAAc;AAAA,EAC1D,GAAG,CAAC,gBAAgB,oBAAoB,CAAC;AAEzC,QAAM,aAAa,SAAS;AAAA,IAC1B,QAAQ;AAAA,IACR,QAAQ,CAAC,UAAU,MAAM;AAAA,EAAA,CAC1B;AAED,QAAM,cAAc;AAAA,IAClB,CAAC,SAAkB;AACjB,YAAM,wBAAwB,OAAO,YAAY,gBAAgB;AAAA,QAC/D,gBAAgB;AAAA,MAAA,CACjB;AAED,YAAM,sBAAsB,KAAK,qBAAqB;AAEtD,aACE,QACA,qBAAqB,YACrB,OAAO,MAAM,QAAQ,UAAU,EAAG;AAAA,IAEtC;AAAA,IACA,CAAC,gBAAgB,YAAY,MAAM;AAAA,EAAA;AAGrC,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,mBAAmB;AAAA,EAAA;AAEvB;"}
@@ -1,57 +0,0 @@
1
- import { last } from '@tanstack/router-core'
2
- import { useCallback, useEffect, useState } from 'react'
3
- import { useRouter } from './useRouter'
4
- import { useMatch } from './useMatch'
5
- import { useRouterState } from './useRouterState'
6
- import type { ParsedLocation } from '@tanstack/router-core'
7
-
8
- export type UseActiveLocationResult = {
9
- activeLocation: ParsedLocation
10
- getFromPath: (from?: string) => string
11
- setActiveLocation: (location?: ParsedLocation) => void
12
- }
13
-
14
- export const useActiveLocation = (
15
- location?: ParsedLocation,
16
- ): UseActiveLocationResult => {
17
- const router = useRouter()
18
- const routerLocation = useRouterState({ select: (state) => state.location })
19
- const [activeLocation, setActiveLocation] = useState<ParsedLocation>(
20
- location ?? routerLocation,
21
- )
22
- const [customActiveLocation, setCustomActiveLocation] = useState<
23
- ParsedLocation | undefined
24
- >(location)
25
-
26
- useEffect(() => {
27
- setActiveLocation(customActiveLocation ?? routerLocation)
28
- }, [routerLocation, customActiveLocation])
29
-
30
- const matchIndex = useMatch({
31
- strict: false,
32
- select: (match) => match.index,
33
- })
34
-
35
- const getFromPath = useCallback(
36
- (from?: string) => {
37
- const activeLocationMatches = router.matchRoutes(activeLocation, {
38
- _buildLocation: false,
39
- })
40
-
41
- const activeLocationMatch = last(activeLocationMatches)
42
-
43
- return (
44
- from ??
45
- activeLocationMatch?.fullPath ??
46
- router.state.matches[matchIndex]!.fullPath
47
- )
48
- },
49
- [activeLocation, matchIndex, router],
50
- )
51
-
52
- return {
53
- activeLocation,
54
- getFromPath,
55
- setActiveLocation: setCustomActiveLocation,
56
- }
57
- }