ui-router-signals
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
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:
| Field | Value |
|---|---|
route | the whole snapshot, replaced per transition |
current | the current state declaration |
params | the current parameter values |
transition | the most recent successful transition |
includes() on a snapshot evaluates StateService.includes against that snapshot, not the live router.
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.
pendingholds the newest transition still in flight, andundefinedwhen 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.errorholds the newest transition's failure as core'sRejection, andundefinedotherwise. Only aRejectType.ERRORrejection, such as a resolve that throws, sets it; superseded, redirected, aborted and ignored transitions leave itundefined. The next transition clears it as it starts, so a retry clears the error as it begins. A failure changespendinganderrortogether, and a superseded transition that fails late changes neither.
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.
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.
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:
Further reading
- API reference
- TC39 Signals proposal
signal-polyfill- On the Altitude Atlas — this package as a brick in sheet 2's model in the round, sized by its authored files and seated on what it peers