Skip to content
chris edited this page Dec 10, 2023 · 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.

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