@diso.io

groutcho @diso.io/groutcho

Guide

Routing for hype: the groutcho router as signals, with <diso-outlet>, <diso-link> and <diso-nav-link> elements.

Routes, redirects and what a match holds are the same in every groutcho package, and are described in groutcho's concepts.

npm install @diso.io/groutcho @diso.io/hype
import { useRouter } from '@diso.io/groutcho';
import { define, html } from '@diso.io/hype';

const routes = {
  Home: { pattern: '/', page: () => html`<h1>Home</h1>`, title: 'Home' },
  Show: {
    pattern: '/show/:title',
    page: (match) => html`<h1>${match.params.title}</h1>`
  }
};

define('my-app', () => {
  const { match } = useRouter({ routes });
  return html`
    <nav>
      <diso-nav-link to="Home">Home</diso-nav-link>
      <diso-link to="Show" .params=${{ title: 'hi' }}>Show</diso-link>
    </nav>
    <p>${() => match.value.url}</p>
    <diso-outlet></diso-outlet>
  `;
});

useRouter

Call it in an element's setup.

  • useRouter(config, options?) creates the router and makes it the page's router. It keeps document.title and scroll in step with it, and destroys it when the element leaves the page. Pass an existing store in place of a config to use one made elsewhere.
  • useRouter() returns the page's router, from any element inside.

Both return { match, title, go, get, store }. match and title are signals; store is the underlying RouterStore.

Options: manageTitle (default true) and scrollBehavior ("top" by default, "preserve", or a function given (prev, current)).

Elements

All three use the page's router unless given another with .store.

  • <diso-outlet> draws route.page for the current match, called as (match) => content when it is a function, inside route.layout if present. When a page throws, it draws the route's or the router's errorPage. It draws directly inside itself, so the surrounding CSS applies to the page. Replace the default handling with .renderPage=${() => fn}.
  • <diso-link to="…"> is an <a> that goes through the router. to is a url or a route name; .params fills a named route; .input takes a full input. Modifier and middle clicks are left to the browser. Style the <a> with diso-link::part(a).
  • <diso-nav-link> is a link that puts active-class (default active) on its <a> when the current match is its route, or is under its path. active-exact requires the path to be equal.

Rendering to a string

Give the router a memory history at the address to render:

import { createMemoryHistory, useRouter } from '@diso.io/groutcho';

useRouter({ routes, history: createMemoryHistory('/show/hi') });

renderToString(html<my-app></my-app>) then writes that page, links and outlet included.

The groutcho packages

Package What
@diso.io/groutcho This one. Routing for hype: useRouter, <diso-outlet> / <diso-link>. What the diso sites use
@diso.io/groutcho-react Hooks (useMatch/useGo), RouterProvider, Link
@diso.io/groutcho-lit Lit ReactiveController, context, <diso-outlet> / <diso-link>
@diso.io/groutcho-core Core: Router, Route, MatchResult, createRouter store, history

Use @diso.io/groutcho with hype. Use -lit or -react to route a Lit or React app without the rest of diso, and -core on its own anywhere else. Every package re-exports the core, so an app imports from one package.

This page as Markdown