Overview

After navigateTo, three URL atoms stay aligned with the address bar: pathname, query object, and hash object. paramsAtom also needs the matched route pattern.

Writes are bidirectional: pathAtom.set / qsAtom.set / paramsAtom.set call navigateTo (with different default replace flags). Prefer navigateTo when setting path and query together.

Which atom for what

Atom
Holds
Write default
pathAtom pathname only replace defaults to false (push).
qsAtom query object replace defaults to true.
hashAtom hash object replace defaults to true.
paramsAtom :param values Needs patternsAtom; rebuilds path on write.
patternsAtom matched pattern Set by Router / animatedRouter.

path + query (two destinations)

navigateTo syncs pathAtom and qsAtom. Tiny two-route demo.

import { html, useUtils, renderApp } from 'mates';

const App = () => {
const { pathAtom, qsAtom, navigateTo } = useUtils();

return () => html`
<div class="m-col m-gap">
<div class="m-grid m-grid-cols-3 m-gap">
<div class="card card-body center">
<span class="overline">path</span>
<span class="card-title">${pathAtom()}</span>
</div>
<div class="card card-body center">
<span class="overline">query</span>
<span class="card-title">
${Object.keys(qsAtom()).length ? JSON.stringify(qsAtom()) : '(none)'}
</span>
</div>
</div>
<div class="m-flex m-items-center m-justify-center m-gap-sm">
<button class="btn-ghost" @click=${() => navigateTo('/')}>Home</button>
<button class="btn-ghost" @click=${() => navigateTo('/about?tab=bio')}>
About?tab=bio
</button>
</div>
<p class="hint">Atom writes also navigate: pathAtom.set('/about')</p>
</div>
`;
};
renderApp(App, document.getElementById('app'));

paramsAtom with Router

Two routes: / and /users/:id. patternsAtom + paramsAtom update on match.

import { html, Router, useUtils, renderApp } from 'mates';

const Home = () => () => html`<p class="muted">Home</p>`;
const User = () => {
const { paramsAtom } = useUtils();
return () => html`<p class="muted">User #${paramsAtom().id ?? '?'}</p>`;
};

const App = () => {
const { navigateTo, paramsAtom, patternsAtom } = useUtils();
const outlet = Router([
{ path: '/', component: Home },
{ path: '/users/:id', component: User },
]);

return () => html`
<div class="m-col m-gap">
<div class="m-flex m-items-center m-justify-center m-gap-sm">
<button @click=${() => navigateTo('/')}>Home</button>
<button class="btn-primary" @click=${() => navigateTo('/users/7')}>/users/7</button>
</div>
<p class="hint">pattern=${patternsAtom() ?? '—'} params=${JSON.stringify(paramsAtom())}</p>
${outlet}
</div>
`;
};
renderApp(App, document.getElementById('app'));