0.4.1 • Published 3 years ago

@daucus/router v0.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@daucus/router

Router for Daucus Web Apps

:book: Documentation

@daucus/router provides 3 differents solutions to help you use the routes generated by the Daucus CLI (routes.js):

  1. a simple daucus-router Web Component
  2. a programmatic router
  3. route matching functions, for custom routing33

Usage Examples

Programmatic API

The DaucusRouter class provides a default implementation of the @modern-helpers/router AbstractRouter for Daucus.

import { DaucusRouter } from "@daucus/router";
// routes generated by the Daucus CLI
import daucusRoutes from "./_daucus_/routes.js";

const appRouter = new DaucusRouter();
appRouter.run(daucusRoutes, "/the-default-path/", "/base/directory");

For more advanced use cases, use the routeFinder and i18nRouteFinder functions to match a path with a Daucus route.

import { routeFinder } from "@daucus/router";
// routes generated by the Daucus CLI
import daucusRoutes from "./_daucus_/routes.js";

const daucusRouteFinder = routeFinder(daucusRoutes);

// custom routing
function myCustomRouter() {
  //...
  if (path.startsWith("/docs/")) {
    const daucusRoute = daucusRouteFinder(path);
    //...
  }
}

For now, building a custom router is mandatory when using the i18n option with the Daucus CLI.

import { i18nRouteFinder } from "@daucus/router";
// routes generated by the Daucus CLI (with "i18n": true)
import daucusRoutes from "./_daucus_/routes.js";

const daucusRouteFinder = i18nRouteFinder(daucusRoutes);

// custom routing
function myCustomRouter() {
  //...
  if (path.startsWith("/docs/")) {
    const daucusRoute = daucusRouteFinder(path, lang);
    //...
  }
}

Web Components

daucus-router

Routing wrapper element for Daucus Routes

Wrap the programmatic DaucusRouter for declarative usage.

Properties

PropertyAttributeTypeDefaultDescription
baseDirbase-dirstring"templates/"Directory where templates are published, relative to the baseHRef (should end with a '/')
defaultPathdefault-pathstring"/docs/"Path of the default route
routesSimpleRoutesConfig | undefinedDaucus Routes

Events

EventDescription
navigation-endA navigation ended without error
navigation-startNew navigation begins
project-changeA navigation led to a route from another Daucus Project
route-matchA corresponding route was found for a navigation
route-redirectionA navigation led to a redirection to another route