Navigationcomponent
Navigation({ children, ...meta }: NavigationProps): ReactElement| Param | Type | |
|---|---|---|
children | NavigationProps | The app subtree to provide navigation to. required |
| Return | |
|---|---|
ReactElement | The navigation provider wrapping children. |
Top-level navigation provider.
- Owns a single
NavigationStoreinitialised from the surrounding<Meta>url/base. - Intercepts same-origin anchor clicks (excluding
downloadanchors) and turns them intoforward()calls. - Listens for
popstateto sync the store with browser back/forward. - Publishes the live URL via
<Meta url={…} params={…}>so descendant<Router>s re-render on navigation.
Exactly one <Navigation> per app — nested routers share this single store.
TODO: switch click/popstate handling to the browser Navigation API when broadly supported.
The top-level navigation provider for a client-side app. It owns a single NavigationStore, publishes the live URL into the <Meta> context so descendant <Router>s re-render on navigation, and wires up browser history. Use exactly one <Navigation> per app — nested routers all share its single store.
Things to know:
- Same-origin anchor clicks are intercepted automatically and turned into
forward()calls. Add adownloadattribute to an anchor to opt out. - It listens for
popstateso the store stays in sync with browser back/forward. - It initialises the store from the surrounding
<Meta>url/base, so set those on an ancestor<HTML>/<Page>. <Router>works with no<Navigation>at all (SSR, static rendering, tests) —<Navigation>is only what makes the URL live on the client.
Usage
import { HTML, Navigation, Router } from "shelving/ui";
<HTML url={initialUrl} root="https://example.com/">
<Navigation>
<Router routes={ROUTES}/>
</Navigation>
</HTML>Imperative navigation
Read the navigation store from anywhere in the tree with requireNavigation() for imperative URL changes:
import { requireNavigation } from "shelving/ui";
const nav = requireNavigation();
nav.forward("/users/123"); // push a new history entry
nav.redirect("/login"); // replace the current history entryExamples
<Navigation><App /></Navigation>