Skip to content

Navigation from data

Experimental

Everything on this page is experimental: the KARNI_NAVIGATION and KARNI_NAVIGATION_PAGES settings, karni.navigation (with karni.navigation.api, karni.navigation.checks and karni.navigation.admin), the NavItem model, GET /api/navigation/, the navigation key of karni.yaml, and useNavigation, navigationEntries, defaultNavIcons, LayoutNavGroup, the NavGroup slot and Layout's navSource, pathname and icons in @karni/ui. They may change in a minor release, and each change is in the CHANGELOG. The error codes keep their meaning.

A large app shows each customer a different set of modules and pages, and its admins change the menu without a deploy. Karni builds that menu from data and opens each entry with the same grants that open the API: a user sees an entry when their roles or their per-user grants hold the permission it names.

Two sources, one shape

The menu lives in the settings, or in the database.

In the settings, KARNI_NAVIGATION is a list of items and groups:

# config/settings.py
KARNI_NAVIGATION = [
    {"label": "Home", "route": "/", "icon": "home", "requires": "public"},
    {
        "label": "Catalog",
        "icon": "box",
        "children": [
            {"label": "Products", "route": "/products", "requires": "catalog.product:view"},
            {"label": "Categories", "route": "/categories", "requires": "catalog.category:view"},
        ],
    },
    {"label": "Regions", "route": "/regions", "icon": "map", "requires": "catalog.region:*"},
    {"label": "Account", "route": "/account"},
]

In the database, KARNI_NAVIGATION = "db" reads the menu from the NavItem table, a tree model the admin edits. The table comes with the karni.navigation app, which only a project that uses this source installs, so every other project gains no table:

# config/settings.py
KARNI_NAVIGATION = "db"
INSTALLED_APPS += ["karni.navigation"]

Then run karni migrate and add the entries in the admin under Navigation items. Startup refuses "db" without the app (E531).

An entry has:

Key Takes
label the text of the entry, required
route an app path that starts with /, such as /products. An entry without a route is a group
icon an icon name the frontend knows (home, bar-chart), optional
order the place among its siblings, default 0, then the list order. A NavItem has position instead
requires public, authenticated (the default) or a permission pattern
children a group's entries

requires takes one of three:

  • public: everyone, signed in or not.
  • authenticated: every signed-in user.
  • a permission pattern as KARNI_ROLES writes it: catalog.product:view, catalog.*:view, catalog.region:* or *. The entry opens for a user who holds a grant for a permission the pattern names, from a role or a per-user grant. An expired user grant opens nothing. A superuser holds every permission. This is grants_for, what Granted() and user.can read, so the menu and the API always agree.

A group is an entry without a route. It shows when its own requirement opens and at least one entry inside it shows, so a group whose entries are all hidden is left out. Groups nest. A group's requirement defaults to authenticated like any entry, so a group that holds a public entry says "requires": "public" too. An entry with a route has no children: startup refuses that in the settings (E530), and the admin form refuses it in the table (E536).

The endpoint: GET /api/navigation/

While KARNI_NAVIGATION is set, karni.api.urls mounts navigation/ first: the menu of the requesting user, signed in or not. A catalog editor whose role holds catalog.product:* and catalog.category:view gets:

{
  "items": [
    {"label": "Home", "icon": "home", "route": "/", "children": []},
    {
      "label": "Catalog",
      "icon": "box",
      "route": null,
      "children": [
        {"label": "Products", "icon": null, "route": "/products", "children": []},
        {"label": "Categories", "icon": null, "route": "/categories", "children": []}
      ]
    },
    {"label": "Account", "icon": null, "route": "/account", "children": []}
  ]
}

A visitor who is not signed in gets the public entries. Both sources give the same response for the same menu. The requirement is not in the response.

Named menus (experimental)

A project with a public side has more than one menu: the staff sidebar, the site's header and the signed-in person's own menu. KARNI_NAVIGATION may name them:

# config/settings.py
KARNI_NAVIGATION = {
    "app": [{"label": "Orders", "route": "/orders", "requires": "karni_commerce.order:view"}],
    "public": [{"label": "Products", "route": "/shop/catalogue", "requires": "public"}],
    "account": [{"label": "Your orders", "route": "/orders"}],
}

GET /api/navigation/?menu=public answers one of them, filtered for the caller as above. Without ?menu= it answers app. A list (or "db") is the app menu alone. A name is lower case letters, digits, - and _, starting with a letter (E530 otherwise). A menu the project does not name answers no entries, so a frontend may ask for account in any project. karni check compares the routes and requirements of every menu, and names a menu other than app in its message ('public menu > Products').

It is one response per user and cheap: no query for public and authenticated entries, one query for the role grants once an entry names a permission, one more for the user grants while KARNI_USER_GRANTS is on, and one for the NavItem rows of the database source. At most three queries, whatever the size of the menu, besides the ones that sign the user in.

The OpenAPI schema describes the response as the Navigation and NavigationItem components (operationId navigation), so karni types writes their types. Unset, the route does not exist and the schema leaves it out. A URLconf built while it was set answers 404 E535.

The menu in the React app

@karni/ui's Layout renders the server's menu with one prop:

const pathname = useRouterState({ select: (state) => state.location.pathname });

<Layout navSource="server" pathname={pathname} navItems={fallbackItems}>
  <Outlet />
</Layout>
  • useNavigation() reads GET /navigation/ through the KarniProvider client, cached per signed-in user (karniKeys.navigation(user)), so a sign-in or a sign-out reads the menu again.
  • Groups collapse behind a button (aria-expanded, aria-controls) that Enter and Space open and close. A group opens by itself while it holds the current page.
  • The current page is the item whose route is pathname, else the item with the longest route pathname is under (/products/42 is under /products). It gets aria-current="page". Without pathname, Layout reads window.location.pathname.
  • Icons come by name: defaultNavIcons knows home, list, grid, dashboard, folder, file, chart, bar-chart, settings, sliders, user, users, box, calendar, map, tag and search. The icons prop adds names or replaces these. An unknown name shows no icon.
  • While the menu loads, the nav shows placeholders. When the request fails, it shows navItems. A server without a menu answers 404, which is never retried, so the same layout works with and without KARNI_NAVIGATION.

Static menus take groups too: navItems accepts { label, icon, children } next to links. navigationEntries(items, { pathname, icons }) turns the server's items into those entries on their own, and the NavGroup slot replaces how a group renders. The scaffold's layout.tsx keeps its menu from the routes until you pass navSource="server".

Pages a visitor sees without signing in

The scaffold's layout puts every page behind <AuthGuard explain>, so a visitor gets the sign-in prompt in place of a page. A route with staticData: { public: true } in frontend/src/routes.tsx is left out of it, and out of the terms gate: a catalogue, a product page, a cart. Its data must be public in the API too (access: {read: public}), or the page shows its error state. A recipe marks its pages public = true in recipe.toml (recipes).

createRoute({ getParentRoute: () => root, path: "/shop", component: ShopPage,
  staticData: { public: true } }),

The admin

NavItem has the tree changelist of the admin: entries in tree order, each label indented by its depth, with the route, the requirement and the icon. The form takes the label, route, icon, requirement, parent and position, and checks each one (E530, E532, E536). Every change is in the change history.

Menu preview, a button on the changelist (preview/), shows the menu one user gets: pick them by email or id, or a visitor who is not signed in. It is exactly what GET /api/navigation/ answers that user, with each entry's requirement next to it. Previewing someone else's menu shows what they may do, so it needs what Effective permissions needs: the permission matrix's view.

karni check

karni check compares the menu with the project while KARNI_NAVIGATION is set. The settings source is always compared, and the database source when its database answers.

Code Problem
E533 an item's route is served by no page of the frontend. The message names the closest route
E534 an entry's requirement names no permission of the project, so only a superuser would see it
W630 the database menu could not be read, so it was not compared
W631 no KARNI_NAVIGATION_PAGES file exists, so the routes were not compared

The frontend's pages are the path: entries of the files in KARNI_NAVIGATION_PAGES (default frontend/src/routes.tsx, where karni make page and karni build register each page). A $param segment takes any value. KARNI_NAVIGATION_PAGES = [] turns the comparison off, for a menu whose client Karni cannot read (a mobile app). A code in SILENCED_SYSTEM_CHECKS is left out.

navigation: karni.E533: Navigation item 'Catalog > Products' links to /prodcts, which no page of the frontend serves. The closest route is /products.

In karni.yaml

The spec's navigation key writes KARNI_NAVIGATION into the settings block (details):

pages:
  - home: dashboard
  - Product: [list, detail, form]
  - Category: [list]
  - reports: code
navigation:
  - home
  - Catalog: {icon: box, items: [Product, Category]}
  - reports: {icon: bar-chart}

navigation: true is every page with a route in pages order. Each page's requirement comes from its access. A mapping of named menus writes the named menus: navigation: {app: [...], public: [...], account: [...]}, each list written as above.

Escape hatches

  • menu_for(user, nodes=...) filters any tree of NavNode (a project's own source), and opens(requirement, user) is the requirement check alone.
  • NavigationView is a plain view: subclass it, override get_menu, and declare path("api/navigation/", YourView.as_view()) before the API include.
  • NavItemAdmin is an ordinary admin: unregister NavItem and register your own.
  • Layout's sidebar replaces the whole nav, and navItems without navSource keeps a menu of your own.

Codes

Code Problem
E530 KARNI_NAVIGATION or a NavItem field is not valid
E531 KARNI_NAVIGATION = "db" without the karni.navigation app
E532 an entry's requirement is not valid
E533 an item's route is served by no page
E534 a requirement names no permission
E535 navigation is turned off
E536 an entry has both a route and entries
W630 the database menu could not be read
W631 no frontend pages file to compare with
C640 navigation names a page the spec does not have
C641 a navigation entry is not valid
C642 a navigation requirement is not valid