Skip to content

lit-ui-router / provideRouter

Variable: provideRouter

ts
const provideRouter: (root, router) => () => void;

Defined in: packages/lit-ui-router/src/context.ts:276

Answers routerContext requests that reach root, so code with no element of its own is still handed a router.

The provider is the element-free twin of <ui-router>: it listens for context-request, guards with isRouterContextRequest, calls stopImmediatePropagation() so an outer provider does not answer twice, and delivers the router synchronously. A subscribe: true request gets a no-op unsubscribe — the provider holds one router for its whole lifetime.

root is any EventTarget. Under @lit-labs/ssr that is globalThis.litServerRoot, the bottom of the renderer's event-target stack, which every element event on the server reaches.

Installing twice installs two listeners; the first one still wins, because it stops immediate propagation. Uninstalling is exact — each call's returned function removes only the listener that call added.

The parameter is UIRouterLit, not UIRouter, because the key this answers is branded with UIRouterLit, and a provider may only answer with the value its key promises. The slot (`withRouterSync`) takes either.

Parameters

ParameterTypeDescription
rootEventTargetthe event target requests travel to
routerUIRouterLitthe router to answer with

Returns

a function that uninstalls this provider

() => void

Example

ts
import { provideRouter } from 'lit-ui-router/context';

const uninstall = provideRouter(globalThis.litServerRoot, router);
try {
  // …render…
} finally {
  uninstall();
}

See

withRouterSync for the slot the same render sets instead