lit-ui-router / provideContext
Variable: provideContext
const provideContext: <T>(root, key, value) => () => void;Defined in: packages/lit-ui-router/src/context.ts:362
Answers context-requests for key that reach root, so code with no element of its own is still handed a value.
The provider is the element-free twin of a provider element: it listens for context-request, guards with isContextRequest, calls stopImmediatePropagation() so an outer provider does not answer twice, and delivers the value synchronously. A subscribe: true request gets a no-op unsubscribe — the provider holds one value 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.
Type Parameters
| Type Parameter | Description |
|---|---|
T extends UnknownContext | the context key's type, branded with the value it references |
Parameters
| Parameter | Type | Description |
|---|---|---|
root | EventTarget | the event target requests travel to |
key | T | the context key to answer for, matched by identity |
value | ContextType<T> | the value to answer with, as the key's brand promises |
Returns
a function that uninstalls this provider
() => void
Example
import { provideContext, routerContext } from 'lit-ui-router/context';
const uninstall = provideContext(globalThis.litServerRoot, routerContext, router);
try {
// …render…
} finally {
uninstall();
}