@matthesketh/utopia-router 0.7.2 → 0.7.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.
- package/dist/index.cjs +34 -13
- package/dist/index.d.cts +8 -5
- package/dist/index.d.ts +8 -5
- package/dist/index.js +34 -13
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -315,6 +315,20 @@ async function navigate(url, options = {}) {
|
|
|
315
315
|
isNavigating.set(true);
|
|
316
316
|
try {
|
|
317
317
|
const fullUrl = new URL(url, window.location.origin);
|
|
318
|
+
const currentUrl = new URL(window.location.href);
|
|
319
|
+
if (fullUrl.pathname === currentUrl.pathname && fullUrl.search === currentUrl.search && fullUrl.hash) {
|
|
320
|
+
scrollPositions.set(navIndex, { x: window.scrollX, y: window.scrollY });
|
|
321
|
+
capScrollPositions();
|
|
322
|
+
navIndex++;
|
|
323
|
+
const state2 = { _utopiaNavIndex: navIndex };
|
|
324
|
+
if (options.replace) {
|
|
325
|
+
history.replaceState(state2, "", fullUrl.href);
|
|
326
|
+
} else {
|
|
327
|
+
history.pushState(state2, "", fullUrl.href);
|
|
328
|
+
}
|
|
329
|
+
scrollToHash(fullUrl.hash.slice(1));
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
318
332
|
const match = matchRoute(fullUrl, routes);
|
|
319
333
|
const hookResult = await runBeforeNavigateHooks(currentRoute.peek(), match);
|
|
320
334
|
if (hookResult === false) {
|
|
@@ -342,19 +356,11 @@ async function navigate(url, options = {}) {
|
|
|
342
356
|
history.pushState(state, "", fullUrl.href);
|
|
343
357
|
}
|
|
344
358
|
currentRoute.set(match);
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
if (el) {
|
|
351
|
-
el.scrollIntoView();
|
|
352
|
-
return;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
window.scrollTo(0, 0);
|
|
357
|
-
});
|
|
359
|
+
if (fullUrl.hash) {
|
|
360
|
+
scrollToHash(fullUrl.hash.slice(1));
|
|
361
|
+
} else {
|
|
362
|
+
requestAnimationFrame(() => window.scrollTo(0, 0));
|
|
363
|
+
}
|
|
358
364
|
} finally {
|
|
359
365
|
redirectDepth--;
|
|
360
366
|
isNavigating.set(false);
|
|
@@ -421,6 +427,18 @@ function capScrollPositions() {
|
|
|
421
427
|
if (firstKey !== void 0) scrollPositions.delete(firstKey);
|
|
422
428
|
}
|
|
423
429
|
}
|
|
430
|
+
function scrollToHash(hashId, maxAttempts = 25) {
|
|
431
|
+
if (!hashId || !VALID_DOM_ID_RE.test(hashId)) return;
|
|
432
|
+
const attempt = (remaining) => {
|
|
433
|
+
const el = document.getElementById(hashId);
|
|
434
|
+
if (el) {
|
|
435
|
+
el.scrollIntoView();
|
|
436
|
+
} else if (remaining > 0) {
|
|
437
|
+
requestAnimationFrame(() => attempt(remaining - 1));
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
requestAnimationFrame(() => attempt(maxAttempts));
|
|
441
|
+
}
|
|
424
442
|
|
|
425
443
|
// src/components.ts
|
|
426
444
|
var import_utopia_core2 = require("@matthesketh/utopia-core");
|
|
@@ -439,6 +457,9 @@ async function preloadRoute() {
|
|
|
439
457
|
}
|
|
440
458
|
}
|
|
441
459
|
function createRouterView() {
|
|
460
|
+
return { render: () => createRouterViewNode() };
|
|
461
|
+
}
|
|
462
|
+
function createRouterViewNode() {
|
|
442
463
|
const container = document.createElement("div");
|
|
443
464
|
container.setAttribute("data-utopia-router-view", "");
|
|
444
465
|
let currentCleanup = null;
|
package/dist/index.d.cts
CHANGED
|
@@ -204,7 +204,8 @@ declare function destroy(): void;
|
|
|
204
204
|
*/
|
|
205
205
|
declare function preloadRoute(): Promise<void>;
|
|
206
206
|
/**
|
|
207
|
-
* Creates a
|
|
207
|
+
* Creates a UtopiaJS-compatible component that renders the current route's
|
|
208
|
+
* component. Can be used as `<RouterView />` in .utopia templates.
|
|
208
209
|
*
|
|
209
210
|
* When the route changes:
|
|
210
211
|
* 1. The new component is lazily loaded (old content stays visible)
|
|
@@ -212,15 +213,17 @@ declare function preloadRoute(): Promise<void>;
|
|
|
212
213
|
* 3. If the route has a layout, the page is wrapped in the layout
|
|
213
214
|
* 4. If loading fails and an error component exists, it is shown instead
|
|
214
215
|
*
|
|
215
|
-
* @returns A
|
|
216
|
+
* @returns A component definition compatible with UtopiaJS's component system
|
|
216
217
|
*
|
|
217
218
|
* @example
|
|
218
219
|
* ```ts
|
|
219
|
-
*
|
|
220
|
-
*
|
|
220
|
+
* import { createRouterView as RouterView } from '@matthesketh/utopia-router';
|
|
221
|
+
* // In template: <RouterView />
|
|
221
222
|
* ```
|
|
222
223
|
*/
|
|
223
|
-
declare function createRouterView():
|
|
224
|
+
declare function createRouterView(): {
|
|
225
|
+
render: () => Node;
|
|
226
|
+
};
|
|
224
227
|
/**
|
|
225
228
|
* Creates an anchor element that performs client-side navigation when clicked.
|
|
226
229
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -204,7 +204,8 @@ declare function destroy(): void;
|
|
|
204
204
|
*/
|
|
205
205
|
declare function preloadRoute(): Promise<void>;
|
|
206
206
|
/**
|
|
207
|
-
* Creates a
|
|
207
|
+
* Creates a UtopiaJS-compatible component that renders the current route's
|
|
208
|
+
* component. Can be used as `<RouterView />` in .utopia templates.
|
|
208
209
|
*
|
|
209
210
|
* When the route changes:
|
|
210
211
|
* 1. The new component is lazily loaded (old content stays visible)
|
|
@@ -212,15 +213,17 @@ declare function preloadRoute(): Promise<void>;
|
|
|
212
213
|
* 3. If the route has a layout, the page is wrapped in the layout
|
|
213
214
|
* 4. If loading fails and an error component exists, it is shown instead
|
|
214
215
|
*
|
|
215
|
-
* @returns A
|
|
216
|
+
* @returns A component definition compatible with UtopiaJS's component system
|
|
216
217
|
*
|
|
217
218
|
* @example
|
|
218
219
|
* ```ts
|
|
219
|
-
*
|
|
220
|
-
*
|
|
220
|
+
* import { createRouterView as RouterView } from '@matthesketh/utopia-router';
|
|
221
|
+
* // In template: <RouterView />
|
|
221
222
|
* ```
|
|
222
223
|
*/
|
|
223
|
-
declare function createRouterView():
|
|
224
|
+
declare function createRouterView(): {
|
|
225
|
+
render: () => Node;
|
|
226
|
+
};
|
|
224
227
|
/**
|
|
225
228
|
* Creates an anchor element that performs client-side navigation when clicked.
|
|
226
229
|
*
|
package/dist/index.js
CHANGED
|
@@ -270,6 +270,20 @@ async function navigate(url, options = {}) {
|
|
|
270
270
|
isNavigating.set(true);
|
|
271
271
|
try {
|
|
272
272
|
const fullUrl = new URL(url, window.location.origin);
|
|
273
|
+
const currentUrl = new URL(window.location.href);
|
|
274
|
+
if (fullUrl.pathname === currentUrl.pathname && fullUrl.search === currentUrl.search && fullUrl.hash) {
|
|
275
|
+
scrollPositions.set(navIndex, { x: window.scrollX, y: window.scrollY });
|
|
276
|
+
capScrollPositions();
|
|
277
|
+
navIndex++;
|
|
278
|
+
const state2 = { _utopiaNavIndex: navIndex };
|
|
279
|
+
if (options.replace) {
|
|
280
|
+
history.replaceState(state2, "", fullUrl.href);
|
|
281
|
+
} else {
|
|
282
|
+
history.pushState(state2, "", fullUrl.href);
|
|
283
|
+
}
|
|
284
|
+
scrollToHash(fullUrl.hash.slice(1));
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
273
287
|
const match = matchRoute(fullUrl, routes);
|
|
274
288
|
const hookResult = await runBeforeNavigateHooks(currentRoute.peek(), match);
|
|
275
289
|
if (hookResult === false) {
|
|
@@ -297,19 +311,11 @@ async function navigate(url, options = {}) {
|
|
|
297
311
|
history.pushState(state, "", fullUrl.href);
|
|
298
312
|
}
|
|
299
313
|
currentRoute.set(match);
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
if (el) {
|
|
306
|
-
el.scrollIntoView();
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
window.scrollTo(0, 0);
|
|
312
|
-
});
|
|
314
|
+
if (fullUrl.hash) {
|
|
315
|
+
scrollToHash(fullUrl.hash.slice(1));
|
|
316
|
+
} else {
|
|
317
|
+
requestAnimationFrame(() => window.scrollTo(0, 0));
|
|
318
|
+
}
|
|
313
319
|
} finally {
|
|
314
320
|
redirectDepth--;
|
|
315
321
|
isNavigating.set(false);
|
|
@@ -376,6 +382,18 @@ function capScrollPositions() {
|
|
|
376
382
|
if (firstKey !== void 0) scrollPositions.delete(firstKey);
|
|
377
383
|
}
|
|
378
384
|
}
|
|
385
|
+
function scrollToHash(hashId, maxAttempts = 25) {
|
|
386
|
+
if (!hashId || !VALID_DOM_ID_RE.test(hashId)) return;
|
|
387
|
+
const attempt = (remaining) => {
|
|
388
|
+
const el = document.getElementById(hashId);
|
|
389
|
+
if (el) {
|
|
390
|
+
el.scrollIntoView();
|
|
391
|
+
} else if (remaining > 0) {
|
|
392
|
+
requestAnimationFrame(() => attempt(remaining - 1));
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
requestAnimationFrame(() => attempt(maxAttempts));
|
|
396
|
+
}
|
|
379
397
|
|
|
380
398
|
// src/components.ts
|
|
381
399
|
import { effect } from "@matthesketh/utopia-core";
|
|
@@ -394,6 +412,9 @@ async function preloadRoute() {
|
|
|
394
412
|
}
|
|
395
413
|
}
|
|
396
414
|
function createRouterView() {
|
|
415
|
+
return { render: () => createRouterViewNode() };
|
|
416
|
+
}
|
|
417
|
+
function createRouterViewNode() {
|
|
397
418
|
const container = document.createElement("div");
|
|
398
419
|
container.setAttribute("data-utopia-router-view", "");
|
|
399
420
|
let currentCleanup = null;
|