---
url: https://lit-ui-router.dev/v1/tutorial/helloworld.md
description: Getting started with lit-ui-router
---

# Hello World

This tutorial will guide you through building your first lit-ui-router application. We'll create a simple app with two "pages" (`hello` and `about`), each with its own URL. You'll be able to click links to navigate between pages, and the active link will be highlighted.

## Live Demo

The Hello World example: [source on GitHub](https://github.com/simshanith/lit-ui-router/tree/main/examples/helloworld), [open in StackBlitz](https://stackblitz.com/github/simshanith/lit-ui-router/tree/main/examples/helloworld?file=src%2Fmain.ts).

## Start a project

This tutorial's app is the Hello World starter, a Vite + TypeScript project.
Copy it with [tiged](https://github.com/tiged/tiged), then run `npm install`
and `npm run dev` in `my-app`:

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

The source is
[`examples/helloworld`](https://github.com/simshanith/lit-ui-router/tree/main/examples/helloworld)
on GitHub, and it runs in the browser on
[StackBlitz](https://stackblitz.com/github/simshanith/lit-ui-router/tree/main/examples/helloworld?file=src%2Fmain.ts).
Its [`index.html`](https://github.com/simshanith/lit-ui-router/blob/main/examples/helloworld/index.html)
holds the `<div id="root">` host element the app renders into.

To add the router to an existing Vite project, install it with its peers:

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

See [Getting started](../introduction#add-to-an-existing-project) for the
TypeScript settings the code below needs.

## Full Source Code

Here's the complete source code for this tutorial. Don't worry if it looks overwhelming - we'll break it down step by step below.

```typescript check
import { html, LitElement, css } from 'lit';
import { customElement } from 'lit/decorators.js';
import { render } from 'lit';
import { hashLocationPlugin } from '@uirouter/core';
import {
  UIRouterLit,
  uiSref,
  uiSrefActive,
  type LitStateDeclaration,
} from 'lit-ui-router';

@customElement('app-root')
class AppRoot extends LitElement {
  static styles = css`
    nav a {
      padding: 8px 16px;
      text-decoration: none;
      color: #333;
    }
    nav a.active {
      font-weight: bold;
      background: #e0e0e0;
      border-radius: 4px;
    }
  `;

  render() {
    return html`
      <nav aria-label="Hello World">
        <a ${uiSrefActive({ activeClasses: ['active'] })} ${uiSref('hello')}
          >Hello</a
        >
        <a ${uiSrefActive({ activeClasses: ['active'] })} ${uiSref('about')}
          >About</a
        >
      </nav>
      <ui-view></ui-view>
    `;
  }
}

// State definitions
const helloState: LitStateDeclaration = {
  name: 'hello',
  url: '/hello',
  component: () => html`<h3>Hello World!</h3>`,
};

const aboutState: LitStateDeclaration = {
  name: 'about',
  url: '/about',
  component: () =>
    html`<h3>About</h3>
      <p>This is a simple lit-ui-router application.</p>`,
};

// Router setup
const router = new UIRouterLit();
router.plugin(hashLocationPlugin);
router.stateRegistry.register(helloState);
router.stateRegistry.register(aboutState);
router.urlService.rules.initial({ state: 'hello' });
router.start();

// Render
render(
  html`
    <ui-router .uiRouter=${router}>
      <app-root></app-root>
    </ui-router>
  `,
  document.getElementById('root')!,
);
```

***

## Building Hello World

Let's break down the code step by step.

### ES Module Imports

```typescript check
import { html, LitElement, css } from 'lit';
import { customElement } from 'lit/decorators.js';
import { render } from 'lit';
import { hashLocationPlugin } from '@uirouter/core';
import {
  UIRouterLit,
  uiSref,
  uiSrefActive,
  type LitStateDeclaration,
} from 'lit-ui-router';
```

We import the essential dependencies:

* **Lit**: `html`, `LitElement`, `css`, `render`, and `customElement` for building web components
* **@uirouter/core**: `hashLocationPlugin` for hash-based URL routing (e.g., `/#/hello`)
* **lit-ui-router**: The router class and directives

### Creating Components

Each "page" in our application is a simple function that returns a Lit html template:

```typescript
() => html`<h3>Hello World!</h3>`;

() =>
  html`<h3>About</h3>
    <p>This is a simple lit-ui-router application.</p>`;
```

These are standard Lit templates. When navigation occurs, lit-ui-router will render the appropriate component based on the current state.

### State Definitions

States are the fundamental building blocks of UI Router. Each state defines a URL and the component to render:

```typescript
const helloState: LitStateDeclaration = {
  name: 'hello',
  url: '/hello',
  component: () => html`<h3>Hello World!</h3>`,
};

const aboutState: LitStateDeclaration = {
  name: 'about',
  url: '/about',
  component: () =>
    html`<h3>About</h3>
      <p>This is a simple lit-ui-router application.</p>`,
};
```

* **name**: A unique identifier for the state
* **url**: The URL path that activates this state
* **component**: The Lit component to render when this state is active

### Navigation with uiSref

The `uiSref` directive creates navigational links to states:

```typescript
@customElement('app-root')
class AppRoot extends LitElement {
  render() {
    return html`
      <nav aria-label="Hello World">
        <a ${uiSrefActive({ activeClasses: ['active'] })} ${uiSref('hello')}
          >Hello</a
        >
        <a ${uiSrefActive({ activeClasses: ['active'] })} ${uiSref('about')}
          >About</a
        >
      </nav>
      <ui-view></ui-view>
    `;
  }
}
```

* **`uiSref('hello')`**: Creates a link to the `hello` state. The directive sets the `href` attribute and handles click events.
* **`uiSrefActive({ activeClasses: ['active'] })`**: Adds the `active` class when the linked state is currently active. On a link it also sets `aria-current="page"` when that exact state is active, so screen reader users get the same "you are here" cue as the highlight.
* **`<ui-view>`**: The viewport where routed components are rendered.

::: tip What is `sref`?
`sref` stands for "State Reference" - it's similar to an `href` but references a state by name instead of a URL path.
:::

### Router Configuration

Configure and start the router:

```typescript
const router = new UIRouterLit();
router.plugin(hashLocationPlugin);
router.trace.enable('TRANSITION');
router.stateRegistry.register(helloState);
router.stateRegistry.register(aboutState);
router.urlService.rules.initial({ state: 'hello' });
router.start();
```

1. **Create router**: Instantiate `UIRouterLit`
2. **Add plugins**: `hashLocationPlugin` enables hash-based URLs (`/#/hello`)
3. **Trace transitions**: `router.trace.enable('TRANSITION')` logs each transition to the browser console, a cheap way to watch the router work
4. **Register states**: Add each state to the registry
5. **Set initial state**: Define where to navigate when the app loads with no URL
6. **Start**: Begin listening for URL changes and processing state transitions

### Bootstrap

Finally, render the application:

```typescript
render(
  html`
    <ui-router .uiRouter=${router}>
      <app-root></app-root>
    </ui-router>
  `,
  document.getElementById('root')!,
);
```

* **`<ui-router>`**: The root element that provides the router context to all descendants
* **`.uiRouter=${router}`**: Pass the configured router instance
* **`<app-root>`**: Your main application component containing navigation and `<ui-view>`

***

## Next Steps

Now that you understand the basics, continue to [Hello Solar System](./hellosolarsystem) to learn about:

* Fetching data with **resolves**
* Using **state parameters** in URLs
* Passing parameters when navigating
