@oomfware/stacker 0.1.2 → 0.1.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.
package/dist/index.d.mts CHANGED
@@ -412,6 +412,15 @@ declare class Router<R extends RouteRegistry<unknown>> {
412
412
  * @returns relative URL
413
413
  */
414
414
  build<K extends RouteName<R>>(name: K, ...args: BuildArgs<R, K>): string;
415
+ /**
416
+ * matches a URL against the route registry, without navigating to it.
417
+ *
418
+ * the URL is resolved against the active location, the same way {@link push} resolves it.
419
+ *
420
+ * @param to destination relative URL
421
+ * @returns the match, or undefined when no route matches
422
+ */
423
+ match(to: string): RouteMatch | undefined;
415
424
  /**
416
425
  * navigates to a route.
417
426
  *
@@ -446,6 +455,13 @@ declare class Router<R extends RouteRegistry<unknown>> {
446
455
  attachView(): () => void;
447
456
  /** resolves pending navigation transitions after a view commits. */
448
457
  notifyCommit(): void;
458
+ /**
459
+ * applies the viewport offset the active navigation is waiting on, if any.
460
+ *
461
+ * `RouterView` calls this from a layout effect ordered ahead of every branch's, so a branch measures its
462
+ * own offset rather than the outgoing screen's. a no-op with nothing pending.
463
+ */
464
+ restoreScroll(): void;
449
465
  /** detaches history listeners and disposes resources. */
450
466
  dispose(): void;
451
467
  }
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
- import { i as parsePath, n as decodeRemainder, r as encodeRemainder, t as createPath } from "./url-gf-m_-s3.mjs";
1
+ import { a as resolvePath, i as parsePath, n as decodeRemainder, r as encodeRemainder, t as createPath } from "./url-gf-m_-s3.mjs";
2
2
  import { SimpleEventEmitter } from "@mary-ext/simple-event-emitter";
3
3
  import { Suspense, createContext, useContext, useEffect, useLayoutEffect, useMemo, useSyncExternalStore } from "react";
4
- import { Fragment, jsx } from "react/jsx-runtime";
4
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
5
  //#region src/codec.ts
6
6
  const OPTIONAL = Symbol("stacker.optional");
7
7
  const DEFAULT = Symbol("stacker.default");
@@ -691,6 +691,7 @@ var Router = class {
691
691
  #view;
692
692
  #commitWaiters = [];
693
693
  #viewAttached = false;
694
+ #pendingScroll = null;
694
695
  constructor(options) {
695
696
  this.#history = options.history;
696
697
  this.#routes = options.routes;
@@ -720,14 +721,11 @@ var Router = class {
720
721
  this.#record(next, target);
721
722
  this.#activeKey = next.key;
722
723
  this.#view = this.#recompute();
724
+ this.#pendingScroll = restoring ? target : null;
723
725
  const committed = this.#awaitCommit();
724
726
  this.#updates.emit();
725
727
  await committed;
726
- if (restoring) win.scrollTo({
727
- behavior: "instant",
728
- left: target.x,
729
- top: target.y
730
- });
728
+ this.restoreScroll();
731
729
  });
732
730
  }
733
731
  /** current view structure. */
@@ -789,6 +787,18 @@ var Router = class {
789
787
  return this.#builder.build(name, ...args);
790
788
  }
791
789
  /**
790
+ * matches a URL against the route registry, without navigating to it.
791
+ *
792
+ * the URL is resolved against the active location, the same way {@link push} resolves it.
793
+ *
794
+ * @param to destination relative URL
795
+ * @returns the match, or undefined when no route matches
796
+ */
797
+ match(to) {
798
+ const { hash, pathname, search } = resolvePath(to, this.#history.location);
799
+ return this.#matcher.match(pathname, search, hash);
800
+ }
801
+ /**
792
802
  * navigates to a route.
793
803
  *
794
804
  * @param name route name
@@ -879,6 +889,23 @@ var Router = class {
879
889
  notifyCommit() {
880
890
  for (const resolve of this.#commitWaiters.splice(0)) resolve();
881
891
  }
892
+ /**
893
+ * applies the viewport offset the active navigation is waiting on, if any.
894
+ *
895
+ * `RouterView` calls this from a layout effect ordered ahead of every branch's, so a branch measures its
896
+ * own offset rather than the outgoing screen's. a no-op with nothing pending.
897
+ */
898
+ restoreScroll() {
899
+ const pending = this.#pendingScroll;
900
+ const win = this.#win;
901
+ if (pending === null || win === null) return;
902
+ this.#pendingScroll = null;
903
+ win.scrollTo({
904
+ behavior: "instant",
905
+ left: pending.x,
906
+ top: pending.y
907
+ });
908
+ }
882
909
  /** detaches history listeners and disposes resources. */
883
910
  dispose() {
884
911
  this.#unlisten();
@@ -1015,12 +1042,21 @@ const RouterView = ({ router }) => {
1015
1042
  }, [router, view]);
1016
1043
  return /* @__PURE__ */ jsx(RouterContext, {
1017
1044
  value: router,
1018
- children: /* @__PURE__ */ jsx(ActiveChainContext, {
1045
+ children: /* @__PURE__ */ jsxs(ActiveChainContext, {
1019
1046
  value: view.activePath,
1020
- children: /* @__PURE__ */ jsx(Pool, { nodes: view.roots })
1047
+ children: [/* @__PURE__ */ jsx(ScrollRestorer, {
1048
+ router,
1049
+ view
1050
+ }), /* @__PURE__ */ jsx(Pool, { nodes: view.roots })]
1021
1051
  })
1022
1052
  });
1023
1053
  };
1054
+ const ScrollRestorer = ({ router, view }) => {
1055
+ useLayoutEffect(() => {
1056
+ router.restoreScroll();
1057
+ }, [router, view]);
1058
+ return null;
1059
+ };
1024
1060
  const Pool = ({ nodes }) => {
1025
1061
  const activePath = useContext(ActiveChainContext);
1026
1062
  return /* @__PURE__ */ jsx(Fragment, { children: nodes.map((node) => /* @__PURE__ */ jsx(Branch, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oomfware/stacker",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "type-safe stack router for React",
5
5
  "license": "0BSD",
6
6
  "repository": {