Repository navigation
Router
Our framework incorporates a client-side router that interfaces with the History API, allowing for the interception and management of native link actions, as well as page navigation events, to support single-page application (SPA) behaviors.
The router configuration must precede application instantiation to ensure proper handling of routes, links, and navigation switches. Configuration necessitates a base URL and a document title.
import { router } from '@base-framework/base';
const baseUrl = '/todo/';
const title = 'Example App';
router.setup(baseUrl, title);Post-setup, the router manages all navigational actions in accordance with application parameters.
The router provides a reactive data object that contains the current path:
// Access current path
const currentPath = router.data.path;
// Watch for path changes
router.data.on('path', (newPath) => {
console.log('Route changed to:', newPath);
});
// Use in components with watchers
Div({ class: 'current-route-[[path]]' }); // binds to router.data.pathProgrammatically navigate to a new route:
// Basic navigation
router.navigate('/clients');
// Navigation with data
router.navigate('/client/123', { id: 123 });
// Navigation with replace (replaces current history entry)
router.navigate('/clients', null, true);Get the current route path:
const currentPath = router.getPath();
// Returns: '/clients/123'Get the current endpoint:
const endpoint = router.getEndPoint();Add a new route dynamically:
// Object syntax
router.add({
uri: '/clients',
component: ClientList,
title: 'Clients',
id: 'clients-route'
});
// Arguments syntax
router.add('/clients', ClientList, callBack, 'Clients', 'clients-route', container);Remove a route by URI:
router.remove('/clients');Get a route object by URI:
const route = router.getRoute('/clients');Get a route object by ID:
const route = router.getRouteById('clients-route');Routes can now include activation conditions directly in the URI definition.
Use #{token} to require an exact hash match.
router.add({
uri: '/docs#{api}',
component: DocsPage
});This route activates only when the URL hash is exactly #api.
Use ?key=value to require query parameters.
router.add({
uri: '/users?mode=edit',
component: UsersPage
});This route activates only when the query string contains mode=edit.
Multiple query conditions can be required with &:
router.add({
uri: '/users?mode=edit&tab=permissions',
component: UsersPage
});Routes can be defined by conditions without a pathname.
router.add({
uri: '#{modal}',
component: ModalRoute
});
router.add({
uri: '?drawer=true',
component: DrawerRoute
});Condition-only routes activate by condition alone, regardless of the current pathname.
Route matching now works in this order:
- Match pathname using the compiled route path regex.
- Enforce required hash and query conditions.
- Deactivate the route automatically if conditions no longer match.
This means routes that depend on hash/query state do not remain active after those conditions are removed.
- Router preserves the raw route URI when creating routes so condition-only routes are detected correctly.
- Route setup parses conditions once (precomputed), compiles the normal path regex from the path portion, and stores required hash/query checks.
- Matching still uses normal path regex first, then condition enforcement.
Anchor (<a>) tag navigation is managed by the router to align with SPA routing mechanisms.
To exclude a link from the router's management, the target="_blank" attribute can be assigned, or a data-cancel-route data attribute can be added to the anchor tag.
{
tag: 'a',
href: '/clients',
'data-cancel-route': true
}
// or
{
tag: 'a',
href: '/clients',
target: '_blank'
}NavLink components synchronize with the router's path, toggling an 'active' class in response to URL path correspondence.
@import { NavLink } from '@base-framework/base';
const link = new NavLink({
href: '/clients', // URL string or watcher array
text: 'Clients', // Text string or watcher array
exact: false, // Defaults to true unless explicitly set to false
activeClass: 'selected', // Defaults to "active" unless specified
});A recommended practice is to encapsulate NavLink creation within an atom function for generating navigational items.
/**
* Factory function for NavLink components.
*
* @param {string} label - The text label of the navigation link.
* @param {string} href - The href for the navigation link.
* @return {object} - Returns a NavLink component.
*/
const NavItem = (label, href) => (new NavLink({
href: href,
text: label,
exact: false,
activeClass: 'selected'
}));The NavItem atom is utilized within a navigation component as follows:
Nav([
NavItem('Clients', '/clients'),
NavItem('Products', '/products')
]);