@tanstack/react-router 0.0.1-beta.267 → 0.0.1-beta.268

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.
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { LinkInfo, LinkOptions, NavigateOptions, ResolveRelativePath, ToOptions } from './link';
2
+ import { NavigateOptions, ResolveRelativePath, ToOptions } from './link';
3
3
  import { ParsedLocation } from './location';
4
4
  import { AnyRoute } from './route';
5
5
  import { RouteById, RoutePaths } from './routeInfo';
@@ -18,7 +18,6 @@ export interface MatchLocation {
18
18
  caseSensitive?: boolean;
19
19
  from?: string;
20
20
  }
21
- export type BuildLinkFn<TRouteTree extends AnyRoute> = <TFrom extends RoutePaths<TRouteTree> = '/', TTo extends string = ''>(dest: LinkOptions<TRouteTree, TFrom, TTo>) => LinkInfo;
22
21
  export type NavigateFn<TRouteTree extends AnyRoute> = <TFrom extends RoutePaths<TRouteTree> = '/', TTo extends string = '', TMaskFrom extends RoutePaths<TRouteTree> = TFrom, TMaskTo extends string = ''>(opts: NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>) => Promise<void>;
23
22
  export type MatchRouteFn<TRouteTree extends AnyRoute> = <TFrom extends RoutePaths<TRouteTree> = '/', TTo extends string = '', TResolved = ResolveRelativePath<TFrom, NoInfer<TTo>>>(location: ToOptions<TRouteTree, TFrom, TTo>, opts?: MatchRouteOptions) => false | RouteById<TRouteTree, TResolved>['types']['allParams'];
24
23
  export type BuildLocationFn<TRouteTree extends AnyRoute> = (opts: BuildNextOptions) => ParsedLocation;
@@ -1,25 +1,11 @@
1
1
  import * as React from 'react';
2
2
  import { Trim } from './fileRoute';
3
- import { LocationState, ParsedLocation } from './location';
3
+ import { LocationState } from './location';
4
4
  import { AnyRoute, ReactNode } from './route';
5
5
  import { AllParams, FullSearchSchema, RouteByPath, RouteIds, RoutePaths } from './routeInfo';
6
6
  import { RegisteredRouter } from './router';
7
7
  import { MakeLinkOptions, MakeLinkPropsOptions } from './useNavigate';
8
8
  import { Expand, NoInfer, NonNullableUpdater, PickRequired, UnionToIntersection, Updater } from './utils';
9
- export type LinkInfo = {
10
- type: 'external';
11
- href: string;
12
- } | {
13
- type: 'internal';
14
- next: ParsedLocation;
15
- handleFocus: (e: any) => void;
16
- handleClick: (e: any) => void;
17
- handleEnter: (e: any) => void;
18
- handleLeave: (e: any) => void;
19
- handleTouchStart: (e: any) => void;
20
- isActive: boolean;
21
- disabled?: boolean;
22
- };
23
9
  export type CleanPath<T extends string> = T extends `${infer L}//${infer R}` ? CleanPath<`${CleanPath<L>}/${CleanPath<R>}`> : T extends `${infer L}//` ? `${CleanPath<L>}/` : T extends `//${infer L}` ? `/${CleanPath<L>}` : T;
24
10
  export type Split<S, TIncludeTrailingSlash = true> = S extends unknown ? string extends S ? string[] : S extends string ? CleanPath<S> extends '' ? [] : TIncludeTrailingSlash extends true ? CleanPath<S> extends `${infer T}/` ? [...Split<T>, '/'] : CleanPath<S> extends `/${infer U}` ? Split<U> : CleanPath<S> extends `${infer T}/${infer U}` ? [...Split<T>, ...Split<U>] : [S] : CleanPath<S> extends `${infer T}/${infer U}` ? [...Split<T>, ...Split<U>] : S extends string ? [S] : never : never : never;
25
11
  export type ParsePathParams<T extends string> = keyof {
@@ -1,8 +1,9 @@
1
1
  import { ReactNode } from './route';
2
- export declare function useBlocker(message: string, condition?: boolean | any): void;
3
- export declare function Block({ message, condition, children }: PromptProps): any;
2
+ import { BlockerFn } from '@tanstack/history';
3
+ export declare function useBlocker(blockerFn: BlockerFn, condition?: boolean | any): void;
4
+ export declare function Block({ blocker, condition, children }: PromptProps): any;
4
5
  export type PromptProps = {
5
- message: string;
6
+ blocker: BlockerFn;
6
7
  condition?: boolean | any;
7
8
  children?: ReactNode;
8
9
  };
@@ -68,10 +68,11 @@
68
68
  location = opts.getLocation();
69
69
  subscribers.forEach(subscriber => subscriber());
70
70
  };
71
- const tryNavigation = task => {
71
+ const tryNavigation = async task => {
72
72
  if (typeof document !== 'undefined' && blockers.length) {
73
73
  for (let blocker of blockers) {
74
- if (!window.confirm(blocker.message)) {
74
+ const allowed = await blocker();
75
+ if (!allowed) {
75
76
  opts.onBlocked?.(onUpdate);
76
77
  return;
77
78
  }
@@ -117,18 +118,15 @@
117
118
  });
118
119
  },
119
120
  createHref: str => opts.createHref(str),
120
- block: message => {
121
- const payload = {
122
- message
123
- };
124
- blockers.push(payload);
121
+ block: blocker => {
122
+ blockers.push(blocker);
125
123
  if (blockers.length === 1) {
126
124
  addEventListener(beforeUnloadEvent, beforeUnloadListener, {
127
125
  capture: true
128
126
  });
129
127
  }
130
128
  return () => {
131
- blockers = blockers.filter(b => b !== payload);
129
+ blockers = blockers.filter(b => b !== blocker);
132
130
  if (!blockers.length) {
133
131
  stopBlocking();
134
132
  }
@@ -3154,21 +3152,21 @@
3154
3152
  return `${path.join(' > ')}`.toLowerCase();
3155
3153
  }
3156
3154
 
3157
- function useBlocker(message, condition = true) {
3155
+ function useBlocker(blockerFn, condition = true) {
3158
3156
  const {
3159
3157
  history
3160
3158
  } = useRouter();
3161
3159
  React__namespace.useEffect(() => {
3162
3160
  if (!condition) return;
3163
- return history.block(message);
3161
+ return history.block(blockerFn);
3164
3162
  });
3165
3163
  }
3166
3164
  function Block({
3167
- message,
3165
+ blocker,
3168
3166
  condition,
3169
3167
  children
3170
3168
  }) {
3171
- useBlocker(message, condition);
3169
+ useBlocker(blocker, condition);
3172
3170
  return children ?? null;
3173
3171
  }
3174
3172