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');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')
]);