Skip to content
Eskais UI

Guides

Routing and the URL

Blocks that keep state in the URL go through a small router adapter, so a hash router keeps its route and an app with its own route contract keeps control.

The router adapter

MasterDetail url, SettingsPage url and ServerDataTable urlState never assume a router. They take an adapter, and the adapters can go through a UrlRouter from url-router: four functions over the app's location.

MemberWhat it does
read()The location now: { path, params }.
navigate(to, { replace })Goes there, adding a history entry unless replace.
href(to)The href of a location, for a link that opens in a new tab.
subscribe(listener)Calls listener when the location may have changed; returns the unsubscribe.
RouterLocationUse it with
hashRouter()#/devices?device=a1A router that reads location.hash: the path stays, the query changes, hrefs are #/path?query.
historyRouter()/devices?device=a1A router that reads the path and query string; the hash is kept on writes.
memoryRouter(initial)/devices?device=a1Tests and server rendering; it records entries.
import { createUrlParam } from "@/lib/url-param"import { hashRouter } from "@/lib/url-router"
// #/devices?device=a1: the route stays /devices, only its query changes.const device = createUrlParam("device", { router: hashRouter() })
<MasterDetail label="Devices" url={device} … />

createUrlParam ships in the url-param item, which master-detail and settings-page install. Without router, createUrlParam(name) keeps its old behaviour: the value in location.search (or { in: "hash" } for a hash route's query).

Your router's own API

When the app's router rewrites URLs it didn't write, or a query on every route is unwelcome, wrap the router in the same shape. The blocks then navigate through it, and its links, guards and scroll handling stay in charge.

import type { UrlRouter } from "@/lib/url-router"
// Your router's own API, so it never sees a URL it didn't write.const appRouter: UrlRouter = {  read: () => ({ path: router.current.path, params: new URLSearchParams(router.current.query) }),  navigate: (to, { replace }) => router.go(to.path, Object.fromEntries(to.params), { replace }),  href: (to) => router.href(to.path, Object.fromEntries(to.params)),  subscribe: (listener) => router.onChange(listener),}
const device = createUrlParam("device", { router: appRouter })const tasks = routerUrlState(appRouter) // for ServerDataTable urlState

Settings section links

A SettingsPage navigation entry is a link, so it can open in a new tab. Its href comes from the url adapter: through hashRouter() it is #/settings?section=security, which fast links follow as a hash route on press and a new tab opens on the same route. Without url it is #security, an in-page anchor that stays native. getHref overrides both, and navigation="buttons" renders buttons that only scroll, for sections that have no address of their own.

<SettingsPage url={createUrlParam("section", { router: hashRouter() })}>…</SettingsPage><SettingsPage navigation="buttons">…</SettingsPage>

Controlled selection

A route contract of the app's own, like a path segment per item, is not a query param. Skip the adapter and control the block from the route:

// The route is #/devices/<id>: the app owns it, the block follows it.const id = useRoute().params.id ?? null
<MasterDetail  label="Devices"  selectedId={id}  onSelectedIdChange={(next) => navigate(next === null ? "/devices" : `/devices/${next}`)}  …/>
  • MasterDetail selectedId onSelectedIdChange: the route opens the item; a press, Close, Back or an agent calls back, and details.cancel() refuses.
  • SettingsPage currentSection onSectionChange: the same for the section being read.