Skip to content
tech_e edited this page Apr 6, 2026 · 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');

Route Activation Conditions

Routes can now include activation conditions directly in the URI definition.

Hash Token Condition

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.

Query Param Condition

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

Condition-Only Routes

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.

Matching Behavior

Route matching now works in this order:

  1. Match pathname using the compiled route path regex.
  2. Enforce required hash and query conditions.
  3. 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.

Notes on the Update

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

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