Skip to content

ui-router-signals ​

NPM VersionGitHub Release

ui-router-signals mirrors a @uirouter/core router's current state into TC39 Signals, through signal-polyfill. It imports neither Lit nor lit-ui-router, so any signal consumer can track route state. lit-ui-router-signals builds Lit controllers on it.

Installation ​

bash
npm install ui-router-signals signal-polyfill

@uirouter/core and signal-polyfill are peer dependencies. Every signal consumer in a bundle needs the same copy of signal-polyfill; two copies keep two separate signal graphs.

RouterSignals ​

RouterSignals.for(router) returns the router's one instance, attaching its transition hooks on first use. An onSuccess hook writes one route snapshot per successful transition, so every value is a settled route. Its fields are read-only Signal.Computeds over that snapshot:

FieldValue
routethe whole snapshot, replaced per transition
currentthe current state declaration
paramsthe current parameter values
transitionthe most recent successful transition

includes() on a snapshot evaluates StateService.includes against that snapshot, not the live router.

ts
import { RouterSignals } from 'ui-router-signals';

const signals = RouterSignals.for(router);

signals.current.get()?.name;

Pending and failed transitions ​

Two more read-only Signal.Computeds sit beside route, not inside it, since every snapshot is a settled route. One onStart hook feeds both.

  • pending holds the newest transition still in flight, and undefined when none is. The transition's promise clears it when that transition settles, whether it succeeds, errors or is aborted. A superseded transition never clears its successor, and a transition already running on first use counts as pending.
  • error holds the newest transition's failure as core's Rejection, and undefined otherwise. Only a RejectType.ERROR rejection, such as a resolve that throws, sets it; superseded, redirected, aborted and ignored transitions leave it undefined. The next transition clears it as it starts, so a retry clears the error as it begins. A failure changes pending and error together, and a superseded transition that fails late changes neither.
ts
const stop = watchSelection(
  () =>
    signals.pending.get() ? 'loading' : signals.error.get() ? 'error' : 'idle',
  undefined,
  (status) => (document.body.dataset.routeStatus = status),
);

Typed snapshots ​

RouteTypes is empty, and route holds a RouteSnapshot. Filling in its snapshot member by module augmentation types route: a snapshot whose includes(name) is a type predicate narrows current and params to that state. The current, params and transition fields keep their untyped shapes.

ts
declare module 'ui-router-signals' {
  interface RouteTypes {
    snapshot: AppRouteSnapshot;
  }
}

watchSelection ​

watchSelection(selector, equals, apply) watches a selector through a Signal.Computed and hands each new value to apply: once synchronously, then in a microtask after any source changes. A watcher's notify callback may not read signals, so it schedules the read, and several synchronous sets deliver one change. It returns a function that stops watching.

ts
import { RouterSignals, watchSelection } from 'ui-router-signals';

const signals = RouterSignals.for(router);

const stop = watchSelection(
  () => signals.current.get()?.name,
  undefined,
  (name) => console.log(name),
);

Releases ​

Every release, with its notes and git tag, is on the ui-router-signals releases page. Compare two versions:

Diff

Further reading ​