What is Lit UI Router?
Lit UI Router (lit-ui-router) is a client-side routing framework for Lit web components. It is an implementation of UI-Router, the state-based router that has powered large AngularJS, Angular, and React applications for over a decade — built directly on the framework-agnostic @uirouter/core.
import { render, html } from 'lit';
import { hashLocationPlugin } from '@uirouter/core';
import { UIRouterLit, type LitStateDeclaration } from 'lit-ui-router';
const router = new UIRouterLit();
router.plugin(hashLocationPlugin);
// Simple routes using template functions - no LitElement classes needed!
const states: LitStateDeclaration[] = [
{ name: 'home', url: '/home', component: () => html`<h1>Home</h1>` },
{ name: 'about', url: '/about', component: () => html`<h1>About</h1>` },
];
states.forEach((state) => router.stateRegistry.register(state));
router.start();
render(
html`<ui-router .uiRouter=${router}>
<ui-view></ui-view>
</ui-router>`,
document.getElementById('root')!,
);Getting started
Start a project
The Hello World starter is a Vite + TypeScript app. Copy it with tiged:
npx tiged simshanith/lit-ui-router/examples/helloworld my-appThen run npm install and npm run dev in my-app. The source is examples/helloworld on GitHub, and it runs in the browser on StackBlitz.
Add to an existing project
Install the router with its peers, lit and @uirouter/core:
npm install lit-ui-router lit @uirouter/coreThe app renders into a host element, <div id="root"> in the starter's index.html. Vite's lit-ts template (npm create vite@latest my-app -- --template lit-ts) already sets the decorator options the tutorial uses.
Then start with the Hello World tutorial.
Why state-based routing?
Most routers map URLs to components. UI-Router instead models your application as a hierarchical tree of states, where each state may carry a URL, a component, parameters, and data requirements. The URL is a serialization of the active state — not the other way around.
This inversion pays off as applications grow:
- Transitions are transactions. Navigating from one state to another either fully succeeds or is rolled back. A half-loaded page is never shown.
- Data arrives before the view. Resolves fetch data during the transition, so components render with their data already available — no loading-state plumbing in every view.
- Hierarchy is first-class. Child states render inside parent states via nested
<ui-view>viewports, and inherit their parents' parameters and resolved data. - Navigation is interceptable. Transition hooks implement cross-cutting concerns like authentication guards, analytics, and redirects in one place.
- States don't need URLs. Dialogs, wizards, and error views (like a 404 state) can participate in routing without claiming an address.
What lit-ui-router adds
@uirouter/core provides the state machine, URL handling, transitions, and resolves. lit-ui-router layers the Lit integration on top:
UIRouterLit— aUIRoutersubclass that teaches the view system to render Lit templates and elements<ui-router>and<ui-view>— custom elements providing router context and rendering viewportsuiSref/uiSrefActive— Lit directives for declarative navigation links and active-link styling- Flexible components — define a routed view as an inline template function, a template with injected props, or a full
LitElementclass TransitionController— a zero-dependency ReactiveController that keeps any component synchronized with router transitions
The packages
This repository publishes a small family of packages. The core router is all you need to start; the companions are optional layers.
| Package | What it is |
|---|---|
lit-ui-router | The router: state machine, <ui-router>/<ui-view> elements, navigation directives, TransitionController. API → |
lit-ui-router-mobx | MobX bindings: an observable RouterStore and reaction-based controllers. Guide → |
lit-ui-router-effect | Effect bindings: a SubscriptionRef of the route and ref-following controllers. Guide → |
lit-ui-router-signals | TC39 Signals bindings: watcher-based controllers over ui-router-signals. Guide → |
ui-router-navigation-location-plugin | A location plugin built on the modern browser Navigation API. Guide → |
ui-router-server | Server-side routing verdicts at the edge — honest 404/302/200 for static SPAs (early 0.x). Guide → |
lit-ui-router-ssr | Static prerendering: a ui-router-server mount table in, an emitted site out. Guide → |
ui-router-signals | TC39 Signals for UI-Router: a framework-agnostic mirror of the router's state. Guide → |
ui-router-effect | Effect for UI-Router: a framework-agnostic SubscriptionRef of the router's state. Guide → |
ui-router-mobx | MobX for UI-Router: a framework-agnostic observable store of the router's state. Guide → |
ui-router-registry | Typed state names and params for UI-Router: a types-only registry for @uirouter/core. Guide → |
eslint-plugin-lit-ui-router | ESLint rules that understand the router's directives — hrefless anchors, inert hrefs, aria-current conflicts and misplaced directives. Guide → |
Because lit-ui-router is a @uirouter/core implementation, the wider UI-Router plugin ecosystem works too. The sample app uses @uirouter/sticky-states, @uirouter/dsr (deep state redirect), and the @uirouter/visualizer developer tool.
How these docs are organized
- Tutorial — a three-part introduction: states and links (Hello World), resolves and parameters (Hello Solar System), nested states and views (Hello Galaxy). Each step runs live on StackBlitz.
- Guides — focused, task-oriented pages on URLs and location, transitions and guards, and reactivity.
- API — an overview plus a generated reference for each runtime package; the ESLint plugin's rules are documented on its package page.
- Sample app — one non-trivial application written in four reactivity idioms, vanilla Lit, MobX, Effect and Signals, with authentication, lazy loading, sticky states, and 404 handling. More mounts, such as hash location, serve it behind other server configurations. The source compares the reactivity styles file-by-file.