@gluonjs/router


@gluonjs/router / packages/router/src / isRouteActive

Function: isRouteActive()

isRouteActive(current, target, exact?): boolean

Parameters

current

RouteLocationNormalized

target

RouteLocationNormalized

exact?

boolean = false

Returns

boolean

Example

Compare resolved current and target locations to determine whether navigation UI should display an active state:

ts
import { html } from '@gluonjs/core';
import {
  createMemoryHistory,
  createRouter,
  createRouterMatcher,
  createRouterPlugin,
  isRouteActive,
  lazyRoute,
  type LazyRouteComponent,
  type NamedRouteDefinition,
  type RouteComponentContext,
  type RouteNamedMap,
} from '@gluonjs/router';

const productRoute: NamedRouteDefinition<{ id: string }> = { params: { id: '42' } };
const routeContract = { product: productRoute } satisfies RouteNamedMap;
const ProductPage = ({ route }: RouteComponentContext) =>
  html`<h1>Product ${route.params.id}</h1>`;
const lazyProduct: LazyRouteComponent = lazyRoute(async () => ProductPage);

const router = createRouter({
  history: createMemoryHistory(['/products/42']),
  routes: [{ path: '/products/:id', name: 'product', component: lazyProduct }],
});
const matcher = createRouterMatcher([{ path: '/products/:id', name: 'product' }]);
const plugin = createRouterPlugin(router);
const current = router.resolve('/products/42');
const target = router.resolve('/products');
console.log(routeContract.product.params.id, matcher.resolvePath('/products/42'), isRouteActive(current, target), plugin);