@tschk/moonshine-waku 0.4.49 → 0.4.80

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/README.md CHANGED
@@ -33,7 +33,9 @@ import { json } from "@tschk/moonshine-waku/server";
33
33
  (IntersectionObserver at 0.1 threshold), `unstable_startTransition`.
34
34
  Modifier-clicks, external URLs, fragments, non-self targets and downloads
35
35
  fall through to the browser without route prefetch. Internal navigation runs
36
- inside `startTransition`.
36
+ inside `startTransition`, then scrolls to the element the URL's fragment
37
+ names (`/#pricing`) when the page has it, else to the top; `useRouter`'s
38
+ `push` and `replace` do the same.
37
39
  - `useRouter_UNSTABLE` / `useRouter` — spreads the current `{ path, query, hash }`
38
40
  route plus `push`, `replace`, `reload`, `back`, `forward`, `prefetch`.
39
41
  - `useRoute` — Waku's route shape, with the trailing-slash `path` convention.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tschk/moonshine-waku",
3
- "version": "0.4.49",
3
+ "version": "0.4.80",
4
4
  "description": "Waku client router API reimplemented on moonshine — drop waku, keep the imports",
5
5
  "license": "ISC",
6
6
  "type": "module",
@@ -15,8 +15,8 @@
15
15
  "./aliases": "./src/aliases.ts"
16
16
  },
17
17
  "dependencies": {
18
- "@tschk/moonshine": "^0.4.48",
19
- "@tschk/moonshine-router": "^0.4.48"
18
+ "@tschk/moonshine": "^0.4.80",
19
+ "@tschk/moonshine-router": "^0.4.80"
20
20
  },
21
21
  "peerDependencies": {
22
22
  "react": ">=18",
package/src/link.ts CHANGED
@@ -22,6 +22,7 @@ import { isExternal, useNavigate } from "@tschk/moonshine/router";
22
22
  import {
23
23
  NavigationStatusContext,
24
24
  prefetchRoute,
25
+ scrollAfterNavigation,
25
26
  toHref,
26
27
  type LinkTo,
27
28
  } from "./router";
@@ -118,7 +119,7 @@ export function Link({
118
119
  warm();
119
120
  const go = () => {
120
121
  navigate(href);
121
- if (scroll !== false) globalThis.scrollTo?.(0, 0);
122
+ if (scroll !== false) scrollAfterNavigation(href);
122
123
  };
123
124
  if (unstable_instant) go();
124
125
  else run(go);
package/src/router.ts CHANGED
@@ -123,8 +123,9 @@ export function useRouter_UNSTABLE(): RouterInstance {
123
123
 
124
124
  const go = useCallback(
125
125
  (to: LinkTo, replace: boolean, scroll?: boolean) => {
126
- navigate(toHref(to), { replace });
127
- if (scroll !== false) globalThis.scrollTo?.(0, 0);
126
+ const href = toHref(to);
127
+ navigate(href, { replace });
128
+ if (scroll !== false) scrollAfterNavigation(href);
128
129
  rerender();
129
130
  return Promise.resolve();
130
131
  },
@@ -151,6 +152,20 @@ export function useRouter_UNSTABLE(): RouterInstance {
151
152
  /** Alias — Waku ships the hook under both names. */
152
153
  export const useRouter = useRouter_UNSTABLE;
153
154
 
155
+ /**
156
+ * Scrolls after a navigation the way the browser and Waku do: to the element
157
+ * the URL's fragment names when the page has it, else to the top.
158
+ */
159
+ export function scrollAfterNavigation(href: string): void {
160
+ if (typeof window === "undefined") return;
161
+ const { hash } = new URL(href, window.location.href);
162
+ const target = hash
163
+ ? document.getElementById(decodeURIComponent(hash.slice(1)))
164
+ : null;
165
+ if (target) target.scrollIntoView();
166
+ else window.scrollTo?.(0, 0);
167
+ }
168
+
154
169
  const prefetched = new Set<string>();
155
170
 
156
171
  /** Warms the browser cache for `url` once, the way Waku prefetches a route. */