studio-variable / kirby-soft-nav
Opt-in soft navigation for Kirby: links swap part of the page or run a script, and update the URL and history without a full reload
Package info
forge.studio-variable.com/studio-variable/kirby-soft-nav.git
Type:kirby-plugin
pkg:composer/studio-variable/kirby-soft-nav
Requires
- php: >=8.2
- getkirby/composer-installer: ^1.2
Requires (Dev)
- getkirby/cms: ^5.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Opt-in soft navigation for Kirby: a link swaps part of the page or runs a script, then updates the URL and the history without a full reload. Links without the attributes navigate normally.
Requirements • Installation • Usage • History • Events • Configuration • License
Requirements
- Kirby 4 or 5
- PHP 8.2+
Installation
Composer (recommended)
composer require studio-variable/kirby-soft-nav
Manual installation
Download the repository and copy it to site/plugins/soft-nav.
Usage
Add the snippet before </body>:
<?php snippet('soft-nav') ?>
Then opt in link by link. Every target URL must also exist as a real page, so a direct visit or a reload renders it on the server.
data-fetch: swap a region
<a href="/projects" data-fetch>Projects</a>
<a href="/projects" data-fetch=".grid">Projects</a>
The page at href is fetched and every element matching the selector (main by default) is replaced by its counterpart in the fetched page. The document title follows the fetched page. Pages are cached for the visit and prefetched on hover.
Scripts inside the swapped region do not run: listen to soft-nav:navigate to set up the new elements.
data-trigger: run a script
<a href="/creation" data-trigger="/assets/js/show-panel.js" data-title="Creation | My site">Creation</a>
The module is imported once and its default export is called with the new URL:
export default ({ url, link }) => {
document.querySelectorAll(".panel").forEach((panel) => {
panel.hidden = panel.dataset.path !== url.pathname;
});
};
link is the clicked link, or null when the history replays the navigation. data-title, when present, becomes the document title.
Current links
Every data-fetch and data-trigger link pointing to the current path gets aria-current="page", the others lose it.
History
Each entry stores its mode, so Back and Forward replay it with the URL of the entry:
- a
data-triggerentry calls the same module again, which must therefore handle every URL it can be called with, including the page where the visitor arrived; - a
data-fetchentry swaps the same region again, from the cache; - the page where the visitor arrived takes the mode of the first soft link clicked.
If a navigation fails, the page reloads.
Events
After each navigation, soft-nav:navigate is dispatched on document:
document.addEventListener("soft-nav:navigate", (event) => {
const { url, link, state } = event.detail;
});
Configuration
| Key | Type | Default | Description |
|---|---|---|---|
studio-variable.soft-nav.target | string | 'main' | Selector swapped by data-fetch links without a value |
License
MIT License © 2026 Studio Variable