@matthesketh/utopia-router 0.7.3 → 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 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
- requestAnimationFrame(() => {
346
- if (fullUrl.hash) {
347
- const hashId = fullUrl.hash.slice(1);
348
- if (hashId && VALID_DOM_ID_RE.test(hashId)) {
349
- const el = document.getElementById(hashId);
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");
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
- requestAnimationFrame(() => {
301
- if (fullUrl.hash) {
302
- const hashId = fullUrl.hash.slice(1);
303
- if (hashId && VALID_DOM_ID_RE.test(hashId)) {
304
- const el = document.getElementById(hashId);
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";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matthesketh/utopia-router",
3
- "version": "0.7.3",
3
+ "version": "0.7.4",
4
4
  "description": "File-based routing for UtopiaJS",
5
5
  "type": "module",
6
6
  "license": "MIT",