---
url: https://lit-ui-router.dev/packages/ui-router-signals.md
description: >-
  A framework-agnostic TC39 Signals mirror of the router's current state with
  ui-router-signals
---

# ui-router-signals

[`ui-router-signals`](https://npmx.dev/package/ui-router-signals) mirrors a
`@uirouter/core` router's current state into
[TC39 Signals](https://github.com/tc39/proposal-signals), through
[`signal-polyfill`](https://github.com/proposal-signals/signal-polyfill). It
imports neither Lit nor `lit-ui-router`, so any signal consumer can track
route state. [`lit-ui-router-signals`](/packages/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)`](/reference/ui-router-signals/classes/RouterSignals#for)
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.Computed`s 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.

```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.Computed`s 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`](/reference/ui-router-signals/interfaces/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)`](/reference/ui-router-signals/functions/watchSelection)
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

## Further reading

* [API reference](/reference/ui-router-signals/)
* [Releases](/releases/ui-router-signals/){target="\_self"} — every version, newest first, with its release notes
* [TC39 Signals proposal](https://github.com/tc39/proposal-signals)
* [`signal-polyfill`](https://github.com/proposal-signals/signal-polyfill)
* [Altitude Atlas](https://atlas.lit-ui-router.dev/bricks/?focus=ui-router-signals) — package size and dependency map
