@tanstack/react-router 0.0.1-beta.77 → 0.0.1-beta.79

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.
@@ -7,7 +7,7 @@
7
7
  "name": "index.production.js",
8
8
  "children": [
9
9
  {
10
- "uid": "5d99-109",
10
+ "uid": "9710-109",
11
11
  "name": "\u0000rollupPluginBabelHelpers.js"
12
12
  },
13
13
  {
@@ -15,11 +15,11 @@
15
15
  "children": [
16
16
  {
17
17
  "name": "tiny-invariant@1.3.1/node_modules/tiny-invariant/dist/esm/tiny-invariant.js",
18
- "uid": "5d99-111"
18
+ "uid": "9710-111"
19
19
  },
20
20
  {
21
21
  "name": "tiny-warning@1.0.3/node_modules/tiny-warning/dist/tiny-warning.esm.js",
22
- "uid": "5d99-113"
22
+ "uid": "9710-113"
23
23
  }
24
24
  ]
25
25
  },
@@ -28,19 +28,19 @@
28
28
  "children": [
29
29
  {
30
30
  "name": "store/build/esm/index.js",
31
- "uid": "5d99-115"
31
+ "uid": "9710-115"
32
32
  },
33
33
  {
34
34
  "name": "router/build/esm/index.js",
35
- "uid": "5d99-117"
35
+ "uid": "9710-117"
36
36
  },
37
37
  {
38
38
  "name": "react-store/build/esm/index.js",
39
- "uid": "5d99-119"
39
+ "uid": "9710-119"
40
40
  },
41
41
  {
42
42
  "name": "react-router/src/index.tsx",
43
- "uid": "5d99-121"
43
+ "uid": "9710-121"
44
44
  }
45
45
  ]
46
46
  }
@@ -50,176 +50,176 @@
50
50
  "isRoot": true
51
51
  },
52
52
  "nodeParts": {
53
- "5d99-109": {
53
+ "9710-109": {
54
54
  "renderedLength": 429,
55
55
  "gzipLength": 238,
56
56
  "brotliLength": 0,
57
- "mainUid": "5d99-108"
57
+ "mainUid": "9710-108"
58
58
  },
59
- "5d99-111": {
59
+ "9710-111": {
60
60
  "renderedLength": 181,
61
61
  "gzipLength": 129,
62
62
  "brotliLength": 0,
63
- "mainUid": "5d99-110"
63
+ "mainUid": "9710-110"
64
64
  },
65
- "5d99-113": {
65
+ "9710-113": {
66
66
  "renderedLength": 44,
67
67
  "gzipLength": 62,
68
68
  "brotliLength": 0,
69
- "mainUid": "5d99-112"
69
+ "mainUid": "9710-112"
70
70
  },
71
- "5d99-115": {
71
+ "9710-115": {
72
72
  "renderedLength": 1288,
73
73
  "gzipLength": 497,
74
74
  "brotliLength": 0,
75
- "mainUid": "5d99-114"
75
+ "mainUid": "9710-114"
76
76
  },
77
- "5d99-117": {
78
- "renderedLength": 48257,
79
- "gzipLength": 11461,
77
+ "9710-117": {
78
+ "renderedLength": 49433,
79
+ "gzipLength": 11720,
80
80
  "brotliLength": 0,
81
- "mainUid": "5d99-116"
81
+ "mainUid": "9710-116"
82
82
  },
83
- "5d99-119": {
83
+ "9710-119": {
84
84
  "renderedLength": 1571,
85
85
  "gzipLength": 594,
86
86
  "brotliLength": 0,
87
- "mainUid": "5d99-118"
87
+ "mainUid": "9710-118"
88
88
  },
89
- "5d99-121": {
90
- "renderedLength": 12591,
91
- "gzipLength": 3262,
89
+ "9710-121": {
90
+ "renderedLength": 12277,
91
+ "gzipLength": 3110,
92
92
  "brotliLength": 0,
93
- "mainUid": "5d99-120"
93
+ "mainUid": "9710-120"
94
94
  }
95
95
  },
96
96
  "nodeMetas": {
97
- "5d99-108": {
97
+ "9710-108": {
98
98
  "id": "\u0000rollupPluginBabelHelpers.js",
99
99
  "moduleParts": {
100
- "index.production.js": "5d99-109"
100
+ "index.production.js": "9710-109"
101
101
  },
102
102
  "imported": [],
103
103
  "importedBy": [
104
104
  {
105
- "uid": "5d99-120"
105
+ "uid": "9710-120"
106
106
  }
107
107
  ]
108
108
  },
109
- "5d99-110": {
109
+ "9710-110": {
110
110
  "id": "/node_modules/.pnpm/tiny-invariant@1.3.1/node_modules/tiny-invariant/dist/esm/tiny-invariant.js",
111
111
  "moduleParts": {
112
- "index.production.js": "5d99-111"
112
+ "index.production.js": "9710-111"
113
113
  },
114
114
  "imported": [],
115
115
  "importedBy": [
116
116
  {
117
- "uid": "5d99-116"
117
+ "uid": "9710-116"
118
118
  }
119
119
  ]
120
120
  },
121
- "5d99-112": {
121
+ "9710-112": {
122
122
  "id": "/node_modules/.pnpm/tiny-warning@1.0.3/node_modules/tiny-warning/dist/tiny-warning.esm.js",
123
123
  "moduleParts": {
124
- "index.production.js": "5d99-113"
124
+ "index.production.js": "9710-113"
125
125
  },
126
126
  "imported": [],
127
127
  "importedBy": [
128
128
  {
129
- "uid": "5d99-116"
129
+ "uid": "9710-116"
130
130
  }
131
131
  ]
132
132
  },
133
- "5d99-114": {
133
+ "9710-114": {
134
134
  "id": "/packages/store/build/esm/index.js",
135
135
  "moduleParts": {
136
- "index.production.js": "5d99-115"
136
+ "index.production.js": "9710-115"
137
137
  },
138
138
  "imported": [],
139
139
  "importedBy": [
140
140
  {
141
- "uid": "5d99-116"
141
+ "uid": "9710-116"
142
142
  }
143
143
  ]
144
144
  },
145
- "5d99-116": {
145
+ "9710-116": {
146
146
  "id": "/packages/router/build/esm/index.js",
147
147
  "moduleParts": {
148
- "index.production.js": "5d99-117"
148
+ "index.production.js": "9710-117"
149
149
  },
150
150
  "imported": [
151
151
  {
152
- "uid": "5d99-110"
152
+ "uid": "9710-110"
153
153
  },
154
154
  {
155
- "uid": "5d99-112"
155
+ "uid": "9710-112"
156
156
  },
157
157
  {
158
- "uid": "5d99-114"
158
+ "uid": "9710-114"
159
159
  }
160
160
  ],
161
161
  "importedBy": [
162
162
  {
163
- "uid": "5d99-120"
163
+ "uid": "9710-120"
164
164
  }
165
165
  ]
166
166
  },
167
- "5d99-118": {
167
+ "9710-118": {
168
168
  "id": "/packages/react-store/build/esm/index.js",
169
169
  "moduleParts": {
170
- "index.production.js": "5d99-119"
170
+ "index.production.js": "9710-119"
171
171
  },
172
172
  "imported": [
173
173
  {
174
- "uid": "5d99-123"
174
+ "uid": "9710-123"
175
175
  }
176
176
  ],
177
177
  "importedBy": [
178
178
  {
179
- "uid": "5d99-120"
179
+ "uid": "9710-120"
180
180
  }
181
181
  ]
182
182
  },
183
- "5d99-120": {
183
+ "9710-120": {
184
184
  "id": "/packages/react-router/src/index.tsx",
185
185
  "moduleParts": {
186
- "index.production.js": "5d99-121"
186
+ "index.production.js": "9710-121"
187
187
  },
188
188
  "imported": [
189
189
  {
190
- "uid": "5d99-108"
190
+ "uid": "9710-108"
191
191
  },
192
192
  {
193
- "uid": "5d99-122"
193
+ "uid": "9710-122"
194
194
  },
195
195
  {
196
- "uid": "5d99-116"
196
+ "uid": "9710-116"
197
197
  },
198
198
  {
199
- "uid": "5d99-118"
199
+ "uid": "9710-118"
200
200
  }
201
201
  ],
202
202
  "importedBy": [],
203
203
  "isEntry": true
204
204
  },
205
- "5d99-122": {
205
+ "9710-122": {
206
206
  "id": "react",
207
207
  "moduleParts": {},
208
208
  "imported": [],
209
209
  "importedBy": [
210
210
  {
211
- "uid": "5d99-120"
211
+ "uid": "9710-120"
212
212
  }
213
213
  ],
214
214
  "isExternal": true
215
215
  },
216
- "5d99-123": {
216
+ "9710-123": {
217
217
  "id": "use-sync-external-store/shim/with-selector",
218
218
  "moduleParts": {},
219
219
  "imported": [],
220
220
  "importedBy": [
221
221
  {
222
- "uid": "5d99-118"
222
+ "uid": "9710-118"
223
223
  }
224
224
  ],
225
225
  "isExternal": true
@@ -106,5 +106,7 @@ declare function Outlet(): JSX.Element | null;
106
106
  declare function ErrorComponent({ error }: {
107
107
  error: any;
108
108
  }): JSX.Element;
109
+ declare function useBlocker(message: string, when?: boolean | any): void;
110
+ declare function Block({ message, when, children }: PromptProps): any;
109
111
 
110
- export { ErrorComponent, Link, LinkFn, LinkPropsOptions, MakeLinkOptions, MakeLinkPropsOptions, MakeMatchRouteOptions, MakeUseMatchRouteOptions, MatchRoute, MatchesProviderProps, Navigate, Outlet, PromptProps, ReactRouter, RouteComponent, RouterProps, RouterProvider, SyncRouteComponent, lazy, matchesContext, routerContext, useLinkProps, useMatch, useMatchRoute, useMatches, useNavigate, useParams, useRoute, useRouter, useRouterContext, useSearch };
112
+ export { Block, ErrorComponent, Link, LinkFn, LinkPropsOptions, MakeLinkOptions, MakeLinkPropsOptions, MakeMatchRouteOptions, MakeUseMatchRouteOptions, MatchRoute, MatchesProviderProps, Navigate, Outlet, PromptProps, ReactRouter, RouteComponent, RouterProps, RouterProvider, SyncRouteComponent, lazy, matchesContext, routerContext, useBlocker, useLinkProps, useMatch, useMatchRoute, useMatches, useNavigate, useParams, useRoute, useRouter, useRouterContext, useSearch };
@@ -142,10 +142,40 @@
142
142
  // making assumptions about the way TanStack Router works
143
143
 
144
144
  const popStateEvent = 'popstate';
145
+ const beforeUnloadEvent = 'beforeunload';
146
+ const beforeUnloadListener = event => {
147
+ event.preventDefault();
148
+ // @ts-ignore
149
+ return event.returnValue = '';
150
+ };
151
+ const stopBlocking = () => {
152
+ removeEventListener(beforeUnloadEvent, beforeUnloadListener, {
153
+ capture: true
154
+ });
155
+ };
145
156
  function createHistory(opts) {
146
157
  let currentLocation = opts.getLocation();
147
158
  let unsub = () => {};
148
159
  let listeners = new Set();
160
+ let blockers = [];
161
+ let queue = [];
162
+ const tryFlush = () => {
163
+ if (blockers.length) {
164
+ blockers[0]?.(tryFlush, () => {
165
+ blockers = [];
166
+ stopBlocking();
167
+ });
168
+ return;
169
+ }
170
+ while (queue.length) {
171
+ queue.shift()?.();
172
+ }
173
+ onUpdate();
174
+ };
175
+ const queueTask = task => {
176
+ queue.push(task);
177
+ tryFlush();
178
+ };
149
179
  const onUpdate = () => {
150
180
  currentLocation = opts.getLocation();
151
181
  listeners.forEach(listener => listener());
@@ -167,26 +197,45 @@
167
197
  };
168
198
  },
169
199
  push: (path, state) => {
170
- opts.pushState(path, state);
171
- onUpdate();
200
+ queueTask(() => {
201
+ opts.pushState(path, state);
202
+ });
172
203
  },
173
204
  replace: (path, state) => {
174
- opts.replaceState(path, state);
175
- onUpdate();
205
+ queueTask(() => {
206
+ opts.replaceState(path, state);
207
+ });
176
208
  },
177
209
  go: index => {
178
- opts.go(index);
179
- onUpdate();
210
+ queueTask(() => {
211
+ opts.go(index);
212
+ });
180
213
  },
181
214
  back: () => {
182
- opts.back();
183
- onUpdate();
215
+ queueTask(() => {
216
+ opts.back();
217
+ });
184
218
  },
185
219
  forward: () => {
186
- opts.forward();
187
- onUpdate();
220
+ queueTask(() => {
221
+ opts.forward();
222
+ });
188
223
  },
189
- createHref: str => opts.createHref(str)
224
+ createHref: str => opts.createHref(str),
225
+ block: cb => {
226
+ blockers.push(cb);
227
+ if (blockers.length === 1) {
228
+ addEventListener(beforeUnloadEvent, beforeUnloadListener, {
229
+ capture: true
230
+ });
231
+ }
232
+ return () => {
233
+ blockers = blockers.filter(b => b !== cb);
234
+ if (!blockers.length) {
235
+ stopBlocking();
236
+ }
237
+ };
238
+ }
190
239
  };
191
240
  }
192
241
  function createBrowserHistory(opts) {
@@ -2136,34 +2185,31 @@
2136
2185
  }
2137
2186
  }, error.message ? /*#__PURE__*/React__namespace.createElement("code", null, error.message) : null)));
2138
2187
  }
2188
+ function useBlocker(message, when = true) {
2189
+ const router = useRouter();
2190
+ React__namespace.useEffect(() => {
2191
+ if (!when) return;
2192
+ let unblock = router.history.block((retry, cancel) => {
2193
+ if (window.confirm(message)) {
2194
+ unblock();
2195
+ retry();
2196
+ } else {
2197
+ cancel();
2198
+ }
2199
+ });
2200
+ return unblock;
2201
+ });
2202
+ }
2203
+ function Block({
2204
+ message,
2205
+ when,
2206
+ children
2207
+ }) {
2208
+ useBlocker(message, when);
2209
+ return children ?? null;
2210
+ }
2139
2211
 
2140
- // TODO: While we migrate away from the history package, these need to be disabled
2141
- // export function usePrompt(message: string, when: boolean | any): void {
2142
- // const router = useRouter()
2143
-
2144
- // React.useEffect(() => {
2145
- // if (!when) return
2146
-
2147
- // let unblock = router.getHistory().block((transition) => {
2148
- // if (window.confirm(message)) {
2149
- // unblock()
2150
- // transition.retry()
2151
- // } else {
2152
- // router.setStore((s) => {
2153
- // s.currentLocation.pathname = window.location.pathname
2154
- // })
2155
- // }
2156
- // })
2157
-
2158
- // return unblock
2159
- // }, [when, message])
2160
- // }
2161
-
2162
- // export function Prompt({ message, when, children }: PromptProps) {
2163
- // usePrompt(message, when ?? true)
2164
- // return (children ?? null) as ReactNode
2165
- // }
2166
-
2212
+ exports.Block = Block;
2167
2213
  exports.ErrorComponent = ErrorComponent;
2168
2214
  exports.Link = Link;
2169
2215
  exports.MatchRoute = MatchRoute;
@@ -2208,6 +2254,7 @@
2208
2254
  exports.trimPath = trimPath;
2209
2255
  exports.trimPathLeft = trimPathLeft;
2210
2256
  exports.trimPathRight = trimPathRight;
2257
+ exports.useBlocker = useBlocker;
2211
2258
  exports.useLinkProps = useLinkProps;
2212
2259
  exports.useMatch = useMatch;
2213
2260
  exports.useMatchRoute = useMatchRoute;