Skip to content
tech_e edited this page Dec 23, 2025 · 3 revisions

Client-Side Router

Overview

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.

Router Data

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.path

Navigation Methods

navigate()

Programmatically 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);

getPath()

Get the current route path:

const currentPath = router.getPath();
// Returns: '/clients/123'

getEndPoint()

Get the current endpoint:

const endpoint = router.getEndPoint();

Route Management

add()

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()

Remove a route by URI:

router.remove('/clients');

getRoute()

Get a route object by URI:

const route = router.getRoute('/clients');

getRouteById()

Get a route object by ID:

const route = router.getRouteById('clients-route');

Links

Anchor (<a>) tag navigation is managed by the router to align with SPA routing mechanisms.

Disable a Link from Routing

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

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
});

Navigation Item Atom

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

Utilizing the Navigation Item Atom

The NavItem atom is utilized within a navigation component as follows:

Nav([
    NavItem('Clients', '/clients'),
    NavItem('Products', '/products')
]);

Clone this wiki locally