@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.
@@ -880,6 +880,20 @@ function parsePath(path) {
880
880
  return parsedPath;
881
881
  }
882
882
 
883
+ var isProduction = process.env.NODE_ENV === 'production';
884
+ var prefix = 'Invariant failed';
885
+ function invariant(condition, message) {
886
+ if (condition) {
887
+ return;
888
+ }
889
+ if (isProduction) {
890
+ throw new Error(prefix);
891
+ }
892
+ var provided = typeof message === 'function' ? message() : message;
893
+ var value = provided ? "".concat(prefix, ": ").concat(provided) : prefix;
894
+ throw new Error(value);
895
+ }
896
+
883
897
  // @ts-nocheck
884
898
  // We're inlining qss here for compression's sake, but we've included it as a hard dependency for the MIT license it requires.
885
899
  function encode(obj, pfx) {
@@ -933,7 +947,7 @@ function decode(str) {
933
947
  return out;
934
948
  }
935
949
 
936
- const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId) {
950
+ const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId, parentPath) {
937
951
  if (options === void 0) {
938
952
  options = {};
939
953
  }
@@ -944,8 +958,6 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
944
958
 
945
959
  if (isRoot) {
946
960
  options.path = rootRouteId;
947
- } else {
948
- warning(!options.path, 'Routes must have a path property.');
949
961
  } // Strip the root from parentIds
950
962
 
951
963
 
@@ -953,13 +965,14 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
953
965
  parentId = '';
954
966
  }
955
967
 
956
- let path = String(isRoot ? rootRouteId : options.path); // If the path is anything other than an index path, trim it up
968
+ let path = isRoot ? rootRouteId : options.path; // If the path is anything other than an index path, trim it up
957
969
 
958
- if (path !== '/') {
970
+ if (path && path !== '/') {
959
971
  path = trimPath(path);
960
972
  }
961
973
 
962
- let id = joinPaths([parentId, path]);
974
+ const routeId = path || options.id;
975
+ let id = joinPaths([parentId, routeId]);
963
976
 
964
977
  if (path === rootRouteId) {
965
978
  path = '/';
@@ -969,14 +982,15 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
969
982
  id = joinPaths(['/', id]);
970
983
  }
971
984
 
972
- const fullPath = id === rootRouteId ? '/' : trimPathRight(id);
985
+ const fullPath = id === rootRouteId ? '/' : trimPathRight(joinPaths([parentPath, path]));
973
986
  return {
974
987
  id: id,
988
+ routeId: routeId,
975
989
  path: path,
976
990
  fullPath: fullPath,
977
991
  options: options,
978
992
  children,
979
- addChildren: cb => createRouteConfig(options, cb(childOptions => createRouteConfig(childOptions, undefined, false, id)), false, parentId)
993
+ addChildren: cb => createRouteConfig(options, cb(childOptions => createRouteConfig(childOptions, undefined, false, id, fullPath)), false, parentId, parentPath)
980
994
  };
981
995
  };
982
996
  const rootRouteId = '__root__';
@@ -1352,55 +1366,76 @@ function createRouter(userOptions) {
1352
1366
 
1353
1367
  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 : [])];
1354
1368
 
1355
- const recurse = async (routes, parentMatch) => {
1356
- var _parentMatch$params, _router$options$filte, _router$preloadCache$, _route$childRoutes2;
1369
+ const recurse = async routes => {
1370
+ var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
1357
1371
 
1372
+ const parentMatch = last(matches);
1358
1373
  let params = (_parentMatch$params = parentMatch == null ? void 0 : parentMatch.params) != null ? _parentMatch$params : {};
1359
1374
  const filteredRoutes = (_router$options$filte = router.options.filterRoutes == null ? void 0 : router.options.filterRoutes(routes)) != null ? _router$options$filte : routes;
1360
- const route = filteredRoutes == null ? void 0 : filteredRoutes.find(route => {
1361
- var _route$childRoutes, _route$options$caseSe;
1362
-
1363
- const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length);
1364
- const matchParams = matchPathname(pathname, {
1365
- to: route.fullPath,
1366
- fuzzy,
1367
- caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
1368
- });
1375
+ let foundRoutes = [];
1369
1376
 
1370
- if (matchParams) {
1371
- let parsedParams;
1377
+ const findMatchInRoutes = (parentRoutes, routes) => {
1378
+ routes.some(route => {
1379
+ var _route$childRoutes, _route$childRoutes2, _route$options$caseSe;
1372
1380
 
1373
- try {
1374
- var _route$options$parseP;
1381
+ if (!route.routePath && (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length) {
1382
+ return findMatchInRoutes([...foundRoutes, route], route.childRoutes);
1383
+ }
1375
1384
 
1376
- parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
1377
- } catch (err) {
1378
- if (opts != null && opts.strictParseParams) {
1379
- throw err;
1385
+ const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length);
1386
+ const matchParams = matchPathname(pathname, {
1387
+ to: route.fullPath,
1388
+ fuzzy,
1389
+ caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
1390
+ });
1391
+
1392
+ if (matchParams) {
1393
+ let parsedParams;
1394
+
1395
+ try {
1396
+ var _route$options$parseP;
1397
+
1398
+ parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
1399
+ } catch (err) {
1400
+ if (opts != null && opts.strictParseParams) {
1401
+ throw err;
1402
+ }
1380
1403
  }
1404
+
1405
+ params = _extends$1({}, params, parsedParams);
1381
1406
  }
1382
1407
 
1383
- params = _extends$1({}, params, parsedParams);
1384
- }
1408
+ if (!!matchParams) {
1409
+ foundRoutes = [...parentRoutes, route];
1410
+ }
1385
1411
 
1386
- return !!matchParams;
1387
- });
1412
+ return !!foundRoutes.length;
1413
+ });
1414
+ return !!foundRoutes.length;
1415
+ };
1388
1416
 
1389
- if (!route) {
1417
+ findMatchInRoutes([], filteredRoutes);
1418
+
1419
+ if (!foundRoutes.length) {
1390
1420
  return;
1391
1421
  }
1392
1422
 
1393
- const interpolatedPath = interpolatePath(route.routePath, params);
1394
- const matchId = interpolatePath(route.routeId, params, true);
1395
- const match = existingMatches.find(d => d.matchId === matchId) || ((_router$preloadCache$ = router.preloadCache[matchId]) == null ? void 0 : _router$preloadCache$.match) || createRouteMatch(router, route, {
1396
- matchId,
1397
- params,
1398
- pathname: joinPaths([pathname, interpolatedPath])
1423
+ foundRoutes.forEach(foundRoute => {
1424
+ var _router$preloadCache$;
1425
+
1426
+ const interpolatedPath = interpolatePath(foundRoute.routePath, params);
1427
+ const matchId = interpolatePath(foundRoute.routeId, params, true);
1428
+ const match = existingMatches.find(d => d.matchId === matchId) || ((_router$preloadCache$ = router.preloadCache[matchId]) == null ? void 0 : _router$preloadCache$.match) || createRouteMatch(router, foundRoute, {
1429
+ matchId,
1430
+ params,
1431
+ pathname: joinPaths([pathname, interpolatedPath])
1432
+ });
1433
+ matches.push(match);
1399
1434
  });
1400
- matches.push(match);
1435
+ const foundRoute = last(foundRoutes);
1401
1436
 
1402
- if ((_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length) {
1403
- recurse(route.childRoutes, match);
1437
+ if ((_foundRoute$childRout = foundRoute.childRoutes) != null && _foundRoute$childRout.length) {
1438
+ recurse(foundRoute.childRoutes);
1404
1439
  }
1405
1440
  };
1406
1441
 
@@ -1504,14 +1539,7 @@ function createRouter(userOptions) {
1504
1539
  isExternal = true;
1505
1540
  } catch (e) {}
1506
1541
 
1507
- if (isExternal) {
1508
- if (process.env.NODE_ENV !== 'production') {
1509
- throw new Error('Attempting to navigate to external url with router.navigate!');
1510
- }
1511
-
1512
- return;
1513
- }
1514
-
1542
+ invariant(!isExternal, 'Attempting to navigate to external url with router.navigate!');
1515
1543
  return router._navigate({
1516
1544
  from: fromString,
1517
1545
  to: toString,
@@ -1666,13 +1694,14 @@ function createRoute(routeConfig, options, parent, router) {
1666
1694
  // ]).replace(new RegExp(`^${rootRouteId}`), '')
1667
1695
  // ) as TRouteInfo['id']
1668
1696
  const {
1669
- id: routeId,
1697
+ id,
1698
+ routeId,
1670
1699
  path: routePath,
1671
1700
  fullPath
1672
1701
  } = routeConfig;
1673
1702
 
1674
- const action = router.state.actions[routeId] || (() => {
1675
- router.state.actions[routeId] = {
1703
+ const action = router.state.actions[id] || (() => {
1704
+ router.state.actions[id] = {
1676
1705
  pending: [],
1677
1706
  submit: async (submission, actionOpts) => {
1678
1707
  var _actionOpts$invalidat;
@@ -1723,11 +1752,12 @@ function createRoute(routeConfig, options, parent, router) {
1723
1752
  }
1724
1753
  }
1725
1754
  };
1726
- return router.state.actions[routeId];
1755
+ return router.state.actions[id];
1727
1756
  })();
1728
1757
 
1729
1758
  let route = {
1730
- routeId,
1759
+ routeId: id,
1760
+ routeRouteId: routeId,
1731
1761
  routePath,
1732
1762
  fullPath,
1733
1763
  options,
@@ -2356,151 +2386,143 @@ const useRouterSubscription = router => {
2356
2386
  };
2357
2387
 
2358
2388
  function createReactRouter(opts) {
2389
+ const makeRouteExt = (route, router) => {
2390
+ return {
2391
+ linkProps: options => {
2392
+ var _functionalUpdate, _functionalUpdate2;
2393
+
2394
+ const {
2395
+ // custom props
2396
+ target,
2397
+ activeProps = () => ({
2398
+ className: 'active'
2399
+ }),
2400
+ inactiveProps = () => ({}),
2401
+ disabled,
2402
+ // element props
2403
+ style,
2404
+ className,
2405
+ onClick,
2406
+ onFocus,
2407
+ onMouseEnter,
2408
+ onMouseLeave
2409
+ } = options,
2410
+ rest = _objectWithoutPropertiesLoose(options, _excluded);
2411
+
2412
+ const linkInfo = route.buildLink(options);
2413
+
2414
+ if (linkInfo.type === 'external') {
2415
+ const {
2416
+ href
2417
+ } = linkInfo;
2418
+ return {
2419
+ href
2420
+ };
2421
+ }
2422
+
2423
+ const {
2424
+ handleClick,
2425
+ handleFocus,
2426
+ handleEnter,
2427
+ handleLeave,
2428
+ isActive,
2429
+ next
2430
+ } = linkInfo;
2431
+
2432
+ const composeHandlers = handlers => e => {
2433
+ e.persist();
2434
+ handlers.forEach(handler => {
2435
+ if (handler) handler(e);
2436
+ });
2437
+ }; // Get the active props
2438
+
2439
+
2440
+ const resolvedActiveProps = isActive ? (_functionalUpdate = functionalUpdate(activeProps)) != null ? _functionalUpdate : {} : {}; // Get the inactive props
2441
+
2442
+ const resolvedInactiveProps = isActive ? {} : (_functionalUpdate2 = functionalUpdate(inactiveProps)) != null ? _functionalUpdate2 : {};
2443
+ return _extends$2({}, resolvedActiveProps, resolvedInactiveProps, rest, {
2444
+ href: disabled ? undefined : next.href,
2445
+ onClick: composeHandlers([handleClick, onClick]),
2446
+ onFocus: composeHandlers([handleFocus, onFocus]),
2447
+ onMouseEnter: composeHandlers([handleEnter, onMouseEnter]),
2448
+ onMouseLeave: composeHandlers([handleLeave, onMouseLeave]),
2449
+ target,
2450
+ style: _extends$2({}, style, resolvedActiveProps.style, resolvedInactiveProps.style),
2451
+ className: [className, resolvedActiveProps.className, resolvedInactiveProps.className].filter(Boolean).join(' ') || undefined
2452
+ }, disabled ? {
2453
+ role: 'link',
2454
+ 'aria-disabled': true
2455
+ } : undefined, {
2456
+ ['data-status']: isActive ? 'active' : undefined
2457
+ });
2458
+ },
2459
+ Link: /*#__PURE__*/React.forwardRef((props, ref) => {
2460
+ const linkProps = route.linkProps(props);
2461
+ useRouterSubscription(router);
2462
+ return /*#__PURE__*/React.createElement("a", _extends$2({
2463
+ ref: ref
2464
+ }, linkProps, {
2465
+ children: typeof props.children === 'function' ? props.children({
2466
+ isActive: linkProps['data-status'] === 'active'
2467
+ }) : props.children
2468
+ }));
2469
+ }),
2470
+ MatchRoute: opts => {
2471
+ const {
2472
+ pending,
2473
+ caseSensitive
2474
+ } = opts,
2475
+ rest = _objectWithoutPropertiesLoose(opts, _excluded2);
2476
+
2477
+ const params = route.matchRoute(rest, {
2478
+ pending,
2479
+ caseSensitive
2480
+ }); // useRouterSubscription(router)
2481
+
2482
+ if (!params) {
2483
+ return null;
2484
+ }
2485
+
2486
+ return typeof opts.children === 'function' ? opts.children(params) : opts.children;
2487
+ }
2488
+ };
2489
+ };
2490
+
2359
2491
  const coreRouter = createRouter(_extends$2({}, opts, {
2360
2492
  createRouter: router => {
2361
2493
  const routerExt = {
2362
2494
  useRoute: routeId => {
2363
2495
  const route = router.getRoute(routeId);
2364
2496
  useRouterSubscription(router);
2365
-
2366
- if (!route) {
2367
- throw new Error("Could not find a route for route \"" + routeId + "\"! Did you forget to add it to your route config?");
2368
- }
2369
-
2497
+ invariant(route, "Could not find a route for route \"" + routeId + "\"! Did you forget to add it to your route config?");
2370
2498
  return route;
2371
2499
  },
2372
2500
  useMatch: routeId => {
2373
- if (routeId === rootRouteId) {
2374
- throw new Error("\"" + rootRouteId + "\" cannot be used with useMatch! Did you mean to useRoute(\"" + rootRouteId + "\")?");
2375
- }
2501
+ invariant(routeId !== rootRouteId, "\"" + rootRouteId + "\" cannot be used with useMatch! Did you mean to useRoute(\"" + rootRouteId + "\")?");
2376
2502
 
2377
2503
  const runtimeMatch = _useMatch();
2378
2504
 
2379
2505
  const match = router.state.matches.find(d => d.routeId === routeId);
2380
-
2381
- if (!match) {
2382
- throw new Error("Could not find a match for route \"" + routeId + "\" being rendered in this component!");
2383
- }
2384
-
2385
- if (runtimeMatch.routeId !== (match == null ? void 0 : match.routeId)) {
2386
- 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?");
2387
- }
2388
-
2506
+ invariant(match, "Could not find a match for route \"" + routeId + "\" being rendered in this component!");
2507
+ 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?");
2389
2508
  useRouterSubscription(router);
2390
2509
 
2391
2510
  if (!match) {
2392
- throw new Error('Match not found!');
2511
+ invariant('Match not found!');
2393
2512
  }
2394
2513
 
2395
2514
  return match;
2396
2515
  }
2397
2516
  };
2398
- Object.assign(router, routerExt);
2517
+ const routeExt = makeRouteExt(router.getRoute('/'), router);
2518
+ Object.assign(router, routerExt, routeExt);
2399
2519
  },
2400
2520
  createRoute: _ref => {
2401
2521
  let {
2402
2522
  router,
2403
2523
  route
2404
2524
  } = _ref;
2405
- const routeExt = {
2406
- linkProps: options => {
2407
- var _functionalUpdate, _functionalUpdate2;
2408
-
2409
- const {
2410
- // custom props
2411
- target,
2412
- activeProps = () => ({
2413
- className: 'active'
2414
- }),
2415
- inactiveProps = () => ({}),
2416
- disabled,
2417
- // element props
2418
- style,
2419
- className,
2420
- onClick,
2421
- onFocus,
2422
- onMouseEnter,
2423
- onMouseLeave
2424
- } = options,
2425
- rest = _objectWithoutPropertiesLoose(options, _excluded);
2426
-
2427
- const linkInfo = route.buildLink(options);
2428
-
2429
- if (linkInfo.type === 'external') {
2430
- const {
2431
- href
2432
- } = linkInfo;
2433
- return {
2434
- href
2435
- };
2436
- }
2437
-
2438
- const {
2439
- handleClick,
2440
- handleFocus,
2441
- handleEnter,
2442
- handleLeave,
2443
- isActive,
2444
- next
2445
- } = linkInfo;
2446
-
2447
- const composeHandlers = handlers => e => {
2448
- e.persist();
2449
- handlers.forEach(handler => {
2450
- if (handler) handler(e);
2451
- });
2452
- }; // Get the active props
2453
-
2454
-
2455
- const resolvedActiveProps = isActive ? (_functionalUpdate = functionalUpdate(activeProps)) != null ? _functionalUpdate : {} : {}; // Get the inactive props
2456
-
2457
- const resolvedInactiveProps = isActive ? {} : (_functionalUpdate2 = functionalUpdate(inactiveProps)) != null ? _functionalUpdate2 : {};
2458
- return _extends$2({}, resolvedActiveProps, resolvedInactiveProps, rest, {
2459
- href: disabled ? undefined : next.href,
2460
- onClick: composeHandlers([handleClick, onClick]),
2461
- onFocus: composeHandlers([handleFocus, onFocus]),
2462
- onMouseEnter: composeHandlers([handleEnter, onMouseEnter]),
2463
- onMouseLeave: composeHandlers([handleLeave, onMouseLeave]),
2464
- target,
2465
- style: _extends$2({}, style, resolvedActiveProps.style, resolvedInactiveProps.style),
2466
- className: [className, resolvedActiveProps.className, resolvedInactiveProps.className].filter(Boolean).join(' ') || undefined
2467
- }, disabled ? {
2468
- role: 'link',
2469
- 'aria-disabled': true
2470
- } : undefined, {
2471
- ['data-status']: isActive ? 'active' : undefined
2472
- });
2473
- },
2474
- Link: /*#__PURE__*/React.forwardRef((props, ref) => {
2475
- const linkProps = route.linkProps(props);
2476
- useRouterSubscription(router);
2477
- return /*#__PURE__*/React.createElement("a", _extends$2({
2478
- ref: ref
2479
- }, linkProps, {
2480
- children: typeof props.children === 'function' ? props.children({
2481
- isActive: linkProps['data-status'] === 'active'
2482
- }) : props.children
2483
- }));
2484
- }),
2485
- MatchRoute: opts => {
2486
- const {
2487
- pending,
2488
- caseSensitive
2489
- } = opts,
2490
- rest = _objectWithoutPropertiesLoose(opts, _excluded2);
2491
-
2492
- const params = route.matchRoute(rest, {
2493
- pending,
2494
- caseSensitive
2495
- }); // useRouterSubscription(router)
2496
-
2497
- if (!params) {
2498
- return null;
2499
- }
2500
-
2501
- return typeof opts.children === 'function' ? opts.children(params) : opts.children;
2502
- }
2503
- };
2525
+ const routeExt = makeRouteExt(route, router);
2504
2526
  Object.assign(route, routeExt);
2505
2527
  }
2506
2528
  }));
@@ -2682,5 +2704,5 @@ function DefaultCatchBoundary(_ref6) {
2682
2704
  }, "If you are the owner of this website, it's highly recommended that you configure your own custom Catch/Error boundaries for the router. You can optionally configure a boundary for each route."));
2683
2705
  }
2684
2706
 
2685
- export { DefaultCatchBoundary, MatchesProvider, Outlet, RouterProvider, createBrowserHistory, createHashHistory, createMemoryHistory, createReactRouter, createRoute, createRouteConfig, createRouteMatch, createRouter, defaultParseSearch, defaultStringifySearch, functionalUpdate, last, matchByPath, matchPathname, parsePathname, parseSearchWith, replaceEqualDeep, _resolvePath as resolvePath, rootRouteId, stringifySearchWith, _useMatch as useMatch, useMatches, useParentMatches, useRouter, warning };
2707
+ export { DefaultCatchBoundary, MatchesProvider, Outlet, RouterProvider, createBrowserHistory, createHashHistory, createMemoryHistory, createReactRouter, createRoute, createRouteConfig, createRouteMatch, createRouter, defaultParseSearch, defaultStringifySearch, functionalUpdate, invariant, last, matchByPath, matchPathname, parsePathname, parseSearchWith, replaceEqualDeep, _resolvePath as resolvePath, rootRouteId, stringifySearchWith, _useMatch as useMatch, useMatches, useParentMatches, useRouter, warning };
2686
2708
  //# sourceMappingURL=index.js.map