@tanstack/react-router 0.0.1-alpha.2 → 0.0.1-alpha.4

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.
@@ -900,6 +900,15 @@
900
900
 
901
901
  return parsedPath;
902
902
  }
903
+ var prefix = 'Invariant failed';
904
+ function invariant(condition, message) {
905
+ if (condition) {
906
+ return;
907
+ }
908
+ var provided = typeof message === 'function' ? message() : message;
909
+ var value = provided ? "".concat(prefix, ": ").concat(provided) : prefix;
910
+ throw new Error(value);
911
+ }
903
912
 
904
913
  // @ts-nocheck
905
914
  // We're inlining qss here for compression's sake, but we've included it as a hard dependency for the MIT license it requires.
@@ -954,7 +963,7 @@
954
963
  return out;
955
964
  }
956
965
 
957
- const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId) {
966
+ const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId, parentPath) {
958
967
  if (options === void 0) {
959
968
  options = {};
960
969
  }
@@ -965,8 +974,6 @@
965
974
 
966
975
  if (isRoot) {
967
976
  options.path = rootRouteId;
968
- } else {
969
- warning(!options.path, 'Routes must have a path property.');
970
977
  } // Strip the root from parentIds
971
978
 
972
979
 
@@ -974,13 +981,14 @@
974
981
  parentId = '';
975
982
  }
976
983
 
977
- let path = String(isRoot ? rootRouteId : options.path); // If the path is anything other than an index path, trim it up
984
+ let path = isRoot ? rootRouteId : options.path; // If the path is anything other than an index path, trim it up
978
985
 
979
- if (path !== '/') {
986
+ if (path && path !== '/') {
980
987
  path = trimPath(path);
981
988
  }
982
989
 
983
- let id = joinPaths([parentId, path]);
990
+ const routeId = path || options.id;
991
+ let id = joinPaths([parentId, routeId]);
984
992
 
985
993
  if (path === rootRouteId) {
986
994
  path = '/';
@@ -990,14 +998,15 @@
990
998
  id = joinPaths(['/', id]);
991
999
  }
992
1000
 
993
- const fullPath = id === rootRouteId ? '/' : trimPathRight(id);
1001
+ const fullPath = id === rootRouteId ? '/' : trimPathRight(joinPaths([parentPath, path]));
994
1002
  return {
995
1003
  id: id,
1004
+ routeId: routeId,
996
1005
  path: path,
997
1006
  fullPath: fullPath,
998
1007
  options: options,
999
1008
  children,
1000
- addChildren: cb => createRouteConfig(options, cb(childOptions => createRouteConfig(childOptions, undefined, false, id)), false, parentId)
1009
+ addChildren: cb => createRouteConfig(options, cb(childOptions => createRouteConfig(childOptions, undefined, false, id, fullPath)), false, parentId, parentPath)
1001
1010
  };
1002
1011
  };
1003
1012
  const rootRouteId = '__root__';
@@ -1373,55 +1382,76 @@
1373
1382
 
1374
1383
  const existingMatches = [...router.state.matches, ...((_router$state$pending3 = (_router$state$pending4 = router.state.pending) == null ? void 0 : _router$state$pending4.matches) != null ? _router$state$pending3 : [])];
1375
1384
 
1376
- const recurse = async (routes, parentMatch) => {
1377
- var _parentMatch$params, _router$options$filte, _router$preloadCache$, _route$childRoutes2;
1385
+ const recurse = async routes => {
1386
+ var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
1378
1387
 
1388
+ const parentMatch = last(matches);
1379
1389
  let params = (_parentMatch$params = parentMatch == null ? void 0 : parentMatch.params) != null ? _parentMatch$params : {};
1380
1390
  const filteredRoutes = (_router$options$filte = router.options.filterRoutes == null ? void 0 : router.options.filterRoutes(routes)) != null ? _router$options$filte : routes;
1381
- const route = filteredRoutes == null ? void 0 : filteredRoutes.find(route => {
1382
- var _route$childRoutes, _route$options$caseSe;
1383
-
1384
- const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length);
1385
- const matchParams = matchPathname(pathname, {
1386
- to: route.fullPath,
1387
- fuzzy,
1388
- caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
1389
- });
1391
+ let foundRoutes = [];
1390
1392
 
1391
- if (matchParams) {
1392
- let parsedParams;
1393
+ const findMatchInRoutes = (parentRoutes, routes) => {
1394
+ routes.some(route => {
1395
+ var _route$childRoutes, _route$childRoutes2, _route$options$caseSe;
1393
1396
 
1394
- try {
1395
- var _route$options$parseP;
1397
+ if (!route.routePath && (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length) {
1398
+ return findMatchInRoutes([...foundRoutes, route], route.childRoutes);
1399
+ }
1396
1400
 
1397
- parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
1398
- } catch (err) {
1399
- if (opts != null && opts.strictParseParams) {
1400
- throw err;
1401
+ const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length);
1402
+ const matchParams = matchPathname(pathname, {
1403
+ to: route.fullPath,
1404
+ fuzzy,
1405
+ caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
1406
+ });
1407
+
1408
+ if (matchParams) {
1409
+ let parsedParams;
1410
+
1411
+ try {
1412
+ var _route$options$parseP;
1413
+
1414
+ parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
1415
+ } catch (err) {
1416
+ if (opts != null && opts.strictParseParams) {
1417
+ throw err;
1418
+ }
1401
1419
  }
1420
+
1421
+ params = _extends$1({}, params, parsedParams);
1402
1422
  }
1403
1423
 
1404
- params = _extends$1({}, params, parsedParams);
1405
- }
1424
+ if (!!matchParams) {
1425
+ foundRoutes = [...parentRoutes, route];
1426
+ }
1406
1427
 
1407
- return !!matchParams;
1408
- });
1428
+ return !!foundRoutes.length;
1429
+ });
1430
+ return !!foundRoutes.length;
1431
+ };
1409
1432
 
1410
- if (!route) {
1433
+ findMatchInRoutes([], filteredRoutes);
1434
+
1435
+ if (!foundRoutes.length) {
1411
1436
  return;
1412
1437
  }
1413
1438
 
1414
- const interpolatedPath = interpolatePath(route.routePath, params);
1415
- const matchId = interpolatePath(route.routeId, params, true);
1416
- const match = existingMatches.find(d => d.matchId === matchId) || ((_router$preloadCache$ = router.preloadCache[matchId]) == null ? void 0 : _router$preloadCache$.match) || createRouteMatch(router, route, {
1417
- matchId,
1418
- params,
1419
- pathname: joinPaths([pathname, interpolatedPath])
1439
+ foundRoutes.forEach(foundRoute => {
1440
+ var _router$preloadCache$;
1441
+
1442
+ const interpolatedPath = interpolatePath(foundRoute.routePath, params);
1443
+ const matchId = interpolatePath(foundRoute.routeId, params, true);
1444
+ const match = existingMatches.find(d => d.matchId === matchId) || ((_router$preloadCache$ = router.preloadCache[matchId]) == null ? void 0 : _router$preloadCache$.match) || createRouteMatch(router, foundRoute, {
1445
+ matchId,
1446
+ params,
1447
+ pathname: joinPaths([pathname, interpolatedPath])
1448
+ });
1449
+ matches.push(match);
1420
1450
  });
1421
- matches.push(match);
1451
+ const foundRoute = last(foundRoutes);
1422
1452
 
1423
- if ((_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length) {
1424
- recurse(route.childRoutes, match);
1453
+ if ((_foundRoute$childRout = foundRoute.childRoutes) != null && _foundRoute$childRout.length) {
1454
+ recurse(foundRoute.childRoutes);
1425
1455
  }
1426
1456
  };
1427
1457
 
@@ -1525,12 +1555,7 @@
1525
1555
  isExternal = true;
1526
1556
  } catch (e) {}
1527
1557
 
1528
- if (isExternal) {
1529
- {
1530
- throw new Error('Attempting to navigate to external url with router.navigate!');
1531
- }
1532
- }
1533
-
1558
+ invariant(!isExternal, 'Attempting to navigate to external url with router.navigate!');
1534
1559
  return router._navigate({
1535
1560
  from: fromString,
1536
1561
  to: toString,
@@ -1685,13 +1710,14 @@
1685
1710
  // ]).replace(new RegExp(`^${rootRouteId}`), '')
1686
1711
  // ) as TRouteInfo['id']
1687
1712
  const {
1688
- id: routeId,
1713
+ id,
1714
+ routeId,
1689
1715
  path: routePath,
1690
1716
  fullPath
1691
1717
  } = routeConfig;
1692
1718
 
1693
- const action = router.state.actions[routeId] || (() => {
1694
- router.state.actions[routeId] = {
1719
+ const action = router.state.actions[id] || (() => {
1720
+ router.state.actions[id] = {
1695
1721
  pending: [],
1696
1722
  submit: async (submission, actionOpts) => {
1697
1723
  var _actionOpts$invalidat;
@@ -1742,11 +1768,12 @@
1742
1768
  }
1743
1769
  }
1744
1770
  };
1745
- return router.state.actions[routeId];
1771
+ return router.state.actions[id];
1746
1772
  })();
1747
1773
 
1748
1774
  let route = {
1749
- routeId,
1775
+ routeId: id,
1776
+ routeRouteId: routeId,
1750
1777
  routePath,
1751
1778
  fullPath,
1752
1779
  options,
@@ -2375,151 +2402,143 @@
2375
2402
  };
2376
2403
 
2377
2404
  function createReactRouter(opts) {
2405
+ const makeRouteExt = (route, router) => {
2406
+ return {
2407
+ linkProps: options => {
2408
+ var _functionalUpdate, _functionalUpdate2;
2409
+
2410
+ const {
2411
+ // custom props
2412
+ target,
2413
+ activeProps = () => ({
2414
+ className: 'active'
2415
+ }),
2416
+ inactiveProps = () => ({}),
2417
+ disabled,
2418
+ // element props
2419
+ style,
2420
+ className,
2421
+ onClick,
2422
+ onFocus,
2423
+ onMouseEnter,
2424
+ onMouseLeave
2425
+ } = options,
2426
+ rest = _objectWithoutPropertiesLoose(options, _excluded);
2427
+
2428
+ const linkInfo = route.buildLink(options);
2429
+
2430
+ if (linkInfo.type === 'external') {
2431
+ const {
2432
+ href
2433
+ } = linkInfo;
2434
+ return {
2435
+ href
2436
+ };
2437
+ }
2438
+
2439
+ const {
2440
+ handleClick,
2441
+ handleFocus,
2442
+ handleEnter,
2443
+ handleLeave,
2444
+ isActive,
2445
+ next
2446
+ } = linkInfo;
2447
+
2448
+ const composeHandlers = handlers => e => {
2449
+ e.persist();
2450
+ handlers.forEach(handler => {
2451
+ if (handler) handler(e);
2452
+ });
2453
+ }; // Get the active props
2454
+
2455
+
2456
+ const resolvedActiveProps = isActive ? (_functionalUpdate = functionalUpdate(activeProps)) != null ? _functionalUpdate : {} : {}; // Get the inactive props
2457
+
2458
+ const resolvedInactiveProps = isActive ? {} : (_functionalUpdate2 = functionalUpdate(inactiveProps)) != null ? _functionalUpdate2 : {};
2459
+ return _extends$2({}, resolvedActiveProps, resolvedInactiveProps, rest, {
2460
+ href: disabled ? undefined : next.href,
2461
+ onClick: composeHandlers([handleClick, onClick]),
2462
+ onFocus: composeHandlers([handleFocus, onFocus]),
2463
+ onMouseEnter: composeHandlers([handleEnter, onMouseEnter]),
2464
+ onMouseLeave: composeHandlers([handleLeave, onMouseLeave]),
2465
+ target,
2466
+ style: _extends$2({}, style, resolvedActiveProps.style, resolvedInactiveProps.style),
2467
+ className: [className, resolvedActiveProps.className, resolvedInactiveProps.className].filter(Boolean).join(' ') || undefined
2468
+ }, disabled ? {
2469
+ role: 'link',
2470
+ 'aria-disabled': true
2471
+ } : undefined, {
2472
+ ['data-status']: isActive ? 'active' : undefined
2473
+ });
2474
+ },
2475
+ Link: /*#__PURE__*/React__namespace.forwardRef((props, ref) => {
2476
+ const linkProps = route.linkProps(props);
2477
+ useRouterSubscription(router);
2478
+ return /*#__PURE__*/React__namespace.createElement("a", _extends$2({
2479
+ ref: ref
2480
+ }, linkProps, {
2481
+ children: typeof props.children === 'function' ? props.children({
2482
+ isActive: linkProps['data-status'] === 'active'
2483
+ }) : props.children
2484
+ }));
2485
+ }),
2486
+ MatchRoute: opts => {
2487
+ const {
2488
+ pending,
2489
+ caseSensitive
2490
+ } = opts,
2491
+ rest = _objectWithoutPropertiesLoose(opts, _excluded2);
2492
+
2493
+ const params = route.matchRoute(rest, {
2494
+ pending,
2495
+ caseSensitive
2496
+ }); // useRouterSubscription(router)
2497
+
2498
+ if (!params) {
2499
+ return null;
2500
+ }
2501
+
2502
+ return typeof opts.children === 'function' ? opts.children(params) : opts.children;
2503
+ }
2504
+ };
2505
+ };
2506
+
2378
2507
  const coreRouter = createRouter(_extends$2({}, opts, {
2379
2508
  createRouter: router => {
2380
2509
  const routerExt = {
2381
2510
  useRoute: routeId => {
2382
2511
  const route = router.getRoute(routeId);
2383
2512
  useRouterSubscription(router);
2384
-
2385
- if (!route) {
2386
- throw new Error("Could not find a route for route \"" + routeId + "\"! Did you forget to add it to your route config?");
2387
- }
2388
-
2513
+ invariant(route, "Could not find a route for route \"" + routeId + "\"! Did you forget to add it to your route config?");
2389
2514
  return route;
2390
2515
  },
2391
2516
  useMatch: routeId => {
2392
- if (routeId === rootRouteId) {
2393
- throw new Error("\"" + rootRouteId + "\" cannot be used with useMatch! Did you mean to useRoute(\"" + rootRouteId + "\")?");
2394
- }
2517
+ invariant(routeId !== rootRouteId, "\"" + rootRouteId + "\" cannot be used with useMatch! Did you mean to useRoute(\"" + rootRouteId + "\")?");
2395
2518
 
2396
2519
  const runtimeMatch = _useMatch();
2397
2520
 
2398
2521
  const match = router.state.matches.find(d => d.routeId === routeId);
2399
-
2400
- if (!match) {
2401
- throw new Error("Could not find a match for route \"" + routeId + "\" being rendered in this component!");
2402
- }
2403
-
2404
- if (runtimeMatch.routeId !== (match == null ? void 0 : match.routeId)) {
2405
- throw new Error("useMatch('" + (match == null ? void 0 : match.routeId) + "') is being called in a component that is meant to render the '" + runtimeMatch.routeId + "' route. Did you mean to 'useRoute(" + (match == null ? void 0 : match.routeId) + ")' instead?");
2406
- }
2407
-
2522
+ invariant(match, "Could not find a match for route \"" + routeId + "\" being rendered in this component!");
2523
+ invariant(runtimeMatch.routeId == (match == null ? void 0 : match.routeId), "useMatch('" + (match == null ? void 0 : match.routeId) + "') is being called in a component that is meant to render the '" + runtimeMatch.routeId + "' route. Did you mean to 'useRoute(" + (match == null ? void 0 : match.routeId) + ")' instead?");
2408
2524
  useRouterSubscription(router);
2409
2525
 
2410
2526
  if (!match) {
2411
- throw new Error('Match not found!');
2527
+ invariant('Match not found!');
2412
2528
  }
2413
2529
 
2414
2530
  return match;
2415
2531
  }
2416
2532
  };
2417
- Object.assign(router, routerExt);
2533
+ const routeExt = makeRouteExt(router.getRoute('/'), router);
2534
+ Object.assign(router, routerExt, routeExt);
2418
2535
  },
2419
2536
  createRoute: _ref => {
2420
2537
  let {
2421
2538
  router,
2422
2539
  route
2423
2540
  } = _ref;
2424
- const routeExt = {
2425
- linkProps: options => {
2426
- var _functionalUpdate, _functionalUpdate2;
2427
-
2428
- const {
2429
- // custom props
2430
- target,
2431
- activeProps = () => ({
2432
- className: 'active'
2433
- }),
2434
- inactiveProps = () => ({}),
2435
- disabled,
2436
- // element props
2437
- style,
2438
- className,
2439
- onClick,
2440
- onFocus,
2441
- onMouseEnter,
2442
- onMouseLeave
2443
- } = options,
2444
- rest = _objectWithoutPropertiesLoose(options, _excluded);
2445
-
2446
- const linkInfo = route.buildLink(options);
2447
-
2448
- if (linkInfo.type === 'external') {
2449
- const {
2450
- href
2451
- } = linkInfo;
2452
- return {
2453
- href
2454
- };
2455
- }
2456
-
2457
- const {
2458
- handleClick,
2459
- handleFocus,
2460
- handleEnter,
2461
- handleLeave,
2462
- isActive,
2463
- next
2464
- } = linkInfo;
2465
-
2466
- const composeHandlers = handlers => e => {
2467
- e.persist();
2468
- handlers.forEach(handler => {
2469
- if (handler) handler(e);
2470
- });
2471
- }; // Get the active props
2472
-
2473
-
2474
- const resolvedActiveProps = isActive ? (_functionalUpdate = functionalUpdate(activeProps)) != null ? _functionalUpdate : {} : {}; // Get the inactive props
2475
-
2476
- const resolvedInactiveProps = isActive ? {} : (_functionalUpdate2 = functionalUpdate(inactiveProps)) != null ? _functionalUpdate2 : {};
2477
- return _extends$2({}, resolvedActiveProps, resolvedInactiveProps, rest, {
2478
- href: disabled ? undefined : next.href,
2479
- onClick: composeHandlers([handleClick, onClick]),
2480
- onFocus: composeHandlers([handleFocus, onFocus]),
2481
- onMouseEnter: composeHandlers([handleEnter, onMouseEnter]),
2482
- onMouseLeave: composeHandlers([handleLeave, onMouseLeave]),
2483
- target,
2484
- style: _extends$2({}, style, resolvedActiveProps.style, resolvedInactiveProps.style),
2485
- className: [className, resolvedActiveProps.className, resolvedInactiveProps.className].filter(Boolean).join(' ') || undefined
2486
- }, disabled ? {
2487
- role: 'link',
2488
- 'aria-disabled': true
2489
- } : undefined, {
2490
- ['data-status']: isActive ? 'active' : undefined
2491
- });
2492
- },
2493
- Link: /*#__PURE__*/React__namespace.forwardRef((props, ref) => {
2494
- const linkProps = route.linkProps(props);
2495
- useRouterSubscription(router);
2496
- return /*#__PURE__*/React__namespace.createElement("a", _extends$2({
2497
- ref: ref
2498
- }, linkProps, {
2499
- children: typeof props.children === 'function' ? props.children({
2500
- isActive: linkProps['data-status'] === 'active'
2501
- }) : props.children
2502
- }));
2503
- }),
2504
- MatchRoute: opts => {
2505
- const {
2506
- pending,
2507
- caseSensitive
2508
- } = opts,
2509
- rest = _objectWithoutPropertiesLoose(opts, _excluded2);
2510
-
2511
- const params = route.matchRoute(rest, {
2512
- pending,
2513
- caseSensitive
2514
- }); // useRouterSubscription(router)
2515
-
2516
- if (!params) {
2517
- return null;
2518
- }
2519
-
2520
- return typeof opts.children === 'function' ? opts.children(params) : opts.children;
2521
- }
2522
- };
2541
+ const routeExt = makeRouteExt(route, router);
2523
2542
  Object.assign(route, routeExt);
2524
2543
  }
2525
2544
  }));
@@ -2716,6 +2735,7 @@
2716
2735
  exports.defaultParseSearch = defaultParseSearch;
2717
2736
  exports.defaultStringifySearch = defaultStringifySearch;
2718
2737
  exports.functionalUpdate = functionalUpdate;
2738
+ exports.invariant = invariant;
2719
2739
  exports.last = last;
2720
2740
  exports.matchByPath = matchByPath;
2721
2741
  exports.matchPathname = matchPathname;