@prunellejs/spa 0.7.2 → 0.7.3

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/client.ts +27 -20
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/spa",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "Opt-in SPA navigation with persistent islands plugin for Prunelle",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -26,6 +26,6 @@
26
26
  "directory": "packages/plugin-spa"
27
27
  },
28
28
  "peerDependencies": {
29
- "prunelle": "^0.7.2"
29
+ "prunelle": "^0.7.3"
30
30
  }
31
31
  }
package/src/client.ts CHANGED
@@ -105,6 +105,25 @@ function resolveSuspenseTemplates(doc: Document): void {
105
105
  }
106
106
  }
107
107
 
108
+ function scrollToHash(hash: string): HTMLElement | null {
109
+ if (hash.length > 1) {
110
+ let id = hash.slice(1)
111
+ try {
112
+ id = decodeURIComponent(id)
113
+ }
114
+ catch {
115
+ // Malformed percent-encoding in hash: use the raw fragment id.
116
+ }
117
+ const anchor = document.getElementById(id)
118
+ if (anchor !== null) {
119
+ anchor.scrollIntoView()
120
+ return anchor
121
+ }
122
+ }
123
+ window.scrollTo(0, 0)
124
+ return null
125
+ }
126
+
108
127
  // Focus goes where a full page load would start it: the section a hash names, else the main landmark.
109
128
  function focusPrimaryLandmark(anchor: HTMLElement | null): void {
110
129
  const target = anchor ?? document.querySelector<HTMLElement>('main') ?? document.querySelector<HTMLElement>('h1')
@@ -361,26 +380,10 @@ export async function navigate(url: string, push = true, restore?: EntryScroll,
361
380
  }
362
381
 
363
382
  let anchor: HTMLElement | null = null
364
- if (restore !== undefined) {
383
+ if (restore !== undefined)
365
384
  restoreScroll(restore)
366
- }
367
- else if (hash.length > 1) {
368
- let anchorId = hash.slice(1)
369
- try {
370
- anchorId = decodeURIComponent(anchorId)
371
- }
372
- catch {
373
- // Malformed percent-encoding in hash: use the raw fragment id.
374
- }
375
- anchor = document.getElementById(anchorId)
376
- if (anchor !== null)
377
- anchor.scrollIntoView()
378
- else
379
- window.scrollTo(0, 0)
380
- }
381
- else {
382
- window.scrollTo(0, 0)
383
- }
385
+ else
386
+ anchor = scrollToHash(hash)
384
387
 
385
388
  focusPrimaryLandmark(anchor)
386
389
  announce(document.title)
@@ -417,8 +420,10 @@ if (typeof document !== 'undefined') {
417
420
  const url = isEligibleLink(link)
418
421
  if (url === undefined)
419
422
  return
420
- if (url.pathname === location.pathname && url.search === location.search && url.hash)
423
+ if (url.pathname === location.pathname && url.search === location.search && url.hash) {
424
+ saveScroll()
421
425
  return
426
+ }
422
427
 
423
428
  event.preventDefault()
424
429
  void navigate(url.href, true)
@@ -453,6 +458,8 @@ if (typeof document !== 'undefined') {
453
458
  const saved = entryScroll(st)
454
459
  if (saved !== undefined)
455
460
  restoreScroll(saved)
461
+ else
462
+ scrollToHash(location.hash)
456
463
  return
457
464
  }
458
465
  // Only same-document region swaps belong to regions(); cross-document entries (even with __pruIdx) belong to spa().