Skip to content

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.

ts
import { render, html } from 'lit';
import { hashLocationPlugin } from '@uirouter/core';
import {
  UIRouterLit,
  uiSref,
  uiSrefActive,
  type LitStateDeclaration,
} from 'lit-ui-router';

const router = new UIRouterLit();
router.plugin(hashLocationPlugin);

// A state can render a template function; no LitElement class is required.
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.urlService.rules.initial({ state: 'home' });

router.start();

render(
  html`<ui-router .uiRouter=${router}>
    <nav aria-label="Main">
      <a ${uiSref('home')} ${uiSrefActive({ activeClasses: ['active'] })}
        >Home</a
      >
      <a ${uiSref('about')} ${uiSrefActive({ activeClasses: ['active'] })}
        >About</a
      >
    </nav>
    <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:

bash
npx tiged simshanith/lit-ui-router/examples/helloworld my-app

Then 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:

bash
npm install lit-ui-router lit @uirouter/core

The app renders into a host element, <div id="root"> in the starter's index.html. The tutorial uses TypeScript decorators (@customElement, @property), so set "experimentalDecorators": true and "useDefineForClassFields": false in tsconfig.json; Vite's lit-ts template (npm create vite@latest my-app -- --template lit-ts) already sets both.

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 — a UIRouter subclass that teaches the view system to render Lit templates and elements
  • <ui-router> and <ui-view> — custom elements providing router context and rendering viewports
  • uiSref / 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 LitElement class
  • 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.

PackageWhat it is
lit-ui-routerThe Lit renderer: <ui-router>/<ui-view> elements, navigation directives, and TransitionController, built on @uirouter/core. Package → API →
lit-ui-router-mobxMobX bindings: an observable RouterStore and reaction-based controllers. Package →
lit-ui-router-effectEffect bindings: a SubscriptionRef of the route and ref-following controllers. Package →
lit-ui-router-signalsTC39 Signals bindings: watcher-based controllers over ui-router-signals. Package →
ui-router-navigation-location-pluginA location plugin built on the modern browser Navigation API. Package →
ui-router-serverServer-side routing verdicts at the edge — honest 404/302/200 for static SPAs (early 0.x). Package →
lit-ui-router-ssrStatic prerendering from a ui-router-server mount table, or per-request HTML from a Lit template and settled router. Package →
ui-router-signalsTC39 Signals for UI-Router: a framework-agnostic mirror of the router's state. Package →
ui-router-effectEffect for UI-Router: a framework-agnostic SubscriptionRef of the router's state. Package →
ui-router-mobxMobX for UI-Router: a framework-agnostic observable store of the router's state. Package →
ui-router-registryTyped state names and params for UI-Router: a types-only registry for @uirouter/core. Package →
ui-router-devtoolsDevelopment tooling for UI-Router: a transition recorder, a Lit panel of the state tree and transition log, and Performance and console reporters. Package →
eslint-plugin-lit-ui-routerESLint rules that understand the router's directives — hrefless anchors, inert hrefs, aria-current conflicts and misplaced directives. Package →
ui-router-otelOpenTelemetry for UI-Router: reports each transition, with its phases and resolves, as spans on the tracer your app already has. Package →

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 its own devtools panel, which shows the state tree and each transition (see the Sample App overview for the @uirouter/visualizer query flag). The Hello Solar System and Hello Galaxy examples load the ui-router-devtools panel when the page url has ?devtools, as in Hello Galaxy.

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, views and links, reactivity, server-side routing, and builds.
  • Packages — installation, entry points and usage for lit-ui-router and every companion package.
  • Examples — live runnable examples from the tutorials and guides.
  • Releases — release notes and version diffs across every published package.
  • API — 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.