1.0.0 • Published 2 years ago

mobrix-engine-plugin-router v1.0.0

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

MoBrix-engine-plugin-router

NPM npm npm bundle size Maintenance


A routing system to control navigation with MoBrix-engine


Summary


Getting started

Installation

If you want to use this plugin with MoBrix-engine, install it:

npm i mobrix-engine-plugin-router

Check MoBrix-engine guide to init the system

Usage

Include this plugin inside your MoBrix-engine config file, and optionally set the router field, with the plugin settings. For example, to add a custom query parameter:

// Inside your MoBrix-engine config file

import { routerPlugin } from "mobrix-engine-plugin-router";

const config = {
  appName: "custom-app",
  plugins: [routerPlugin],
  router: {
    routes: {
      Home: "/",
      Custom: "/custom",
    },
    homePage: "Home",
    basename: "/custom-basename",
  },
};

export default config;

So the plugin will store 2 routes:

{
  "Home": "/custom-basename/",
  "Custom": "/custom-basename/custom"
}

and the home page route will be:

/custom-basename/

Additionally, this plugin save the used history into the router config, inside the config returned after MoBrix-engine is initialized. you can find it into the history field:

import { initEngine } from "mobrix-engine";
import { routerPlugin } from "mobrix-engine-plugin-router";

const config = {
  plugins: [routerPlugin],
  appName: "custom-app",
  router: {
    routes: {
      Home: "/",
      Custom: "/custom",
    },
    homePage: "Home",
    basename: "/custom-basename",
  },
};

const engineOutput = initEngine(config);

//You can use this history object in any part of your app
const history = engineOutput.config.router.history;

You can see a live preview by visiting mobrix-engine-playground

API

Config

This plugin adds a custom field inside the MoBrix-engine config, router:

SelectorsReturns
routesa dictionary with all custom routes (the key is the custom route name, and the value is the route associated)
homePagehome page route name (from routes field)
basenamecustom basename, a shared common routes that will be put at the start of every route
onLocationChangefunctions called everytime the location change

Check the usage section for a real example

Actions

Action creatorArgumentsEffect
goBack/Go back to previous route saved in history
goTo- path: new route to setGo to the given route, if it is one of the stored routes, and save it to the shared history

Import them from this lib:

import { goBack, goTo } from "mobrix-engine-plugin-router";

Then dispatch them from any part of your app:

import { goBack, goTo } from "mobrix-engine-plugin-router";

import { useDispatch } from "react-redux";

export const goToButton = () => {
  const dispatch = useDispatch();

  return (
    <button
      onClick={() => {
        dispatch(goTo('/custom-basename/custom'));
      }}
    >
      Go to '/custom-basename/custom' route
    </button>
  );

export const GoBackButton = () => {
  const dispatch = useDispatch();

  return (
    <button
      onClick={() => {
        dispatch(goBack());
      }}
    >
      Go back
    </button>
  );
};

Selectors

SelectorsReturns
getRouterViewrouter plugin state
getRouterPluginConfigrouter plugin configuration
getHomePagehome page route
getRoutesstored routes, with basename at the start of all routes
isHomePagetrue if actual route is home page, otherwise false

Import them from this lib:

import {
  getHomePage,
  getRouterPluginConfig,
  getRouterView,
  getRoutes,
  isHomePage,
} from "mobrix-engine-plugin-router";

Then you can use them, with selectors hooks, inside your components::

import { useSelector } from "react-redux";
import { getRouterConfig } from "mobrix-engine-plugin-router";

export const RouterDebugComponent = () => {
  const routerConfig = useSelector(getRouterConfig);

  return (
    <div>
      <p>Router plugin configuration</p>
      {routerConfig}
    </div>
  );
};

Integration with other plugins

  • This plugin expose some fields to work with any other plugin. If you want to interact with it, using your custom plugin, just check the add an interaction for mobrix-engine-router. With the given field and the actual engine config, you can add custom params to the plugin (look at the config section). For example, to add a custom function to be called when location change:
//Just a skeleton of a custom plugin that interacts with router plugin
const customPlugin = () => ({
  // Custom plugin stuffs

  interactions: [
    {
      plugin: "mobrix-engine-router",
      effect: (routerConfig) => {
        // Custom plugin stuffs

        //Add the custom callback
        routerConfig.onLocationChange.push(() => {
          alert("location changed");
        });
      },
    },
  ],
});

Included libraries


Authors


License

This project is licensed under the MIT License - see the LICENSE file for details