1.0.1 • Published 2 years ago

mobrix-engine-plugin-modal v1.0.1

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

MoBrix-engine-plugin-modal

NPM npm npm bundle size Maintenance


Manage your web app modal with mobrix-engine system


Summary


Getting started

Installation

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

npm i mobrix-engine-plugin-modal

Usage

Check mobrix-engine guide to init the system

Include this plugin inside your mobrix-engine config, optionally with modal field set, to customize onModalOpen and onModalClose callbacks:

const { modalPlugin } = require("mobrix-engine-plugin-modal");

const config = {
  appName: "custom-app",
  plugins: [modalPlugin],
  modal: {
    onModalClose: [() => alert("modal closed")],
    onModalOpen: [() => alert("modal opened")],
  },
};

module.exports = { config };

Then you can drive your modal component (in this example it is used MoBrix-ui modal), with selectors:

import { isModalVisible, getModalType } from "mobrix-engine-plugin-modal";
import { Modal } from "mobrix-ui";

const forms = {
  formOne: () => <div>Form one</div>,
  formTwo: () => <div>Form two</div>,
};

export const CustomComponent = () => {
  const isVisible = useSelectors(isModalVisible);
  const type = useSelectors(getModalType);
  const Content = forms[type] || () => <div/>;

  return (
    <Modal hide={!isVisible}>
      <Content />
    </Modal>
  );
};

API

Config

This plugin adds adds a custom field inside mobrix-engine config, modal. Inside this field, there are the plugin settings:

SettingDescription
onModalClosearray of functions, that are called everytime the modal is closed
onModalOpenarray of functions, that are called everytime the modal is opened

Actions

Action creatorArgumentsEffect
openModal- type: modal form type to open - context : (optional) custom data associated with the given modal formOpen the modal, and optionally set the actual context
closeModal/Close the modal, and reset the context
setModalContext- context : custom modal context to set(similar to openModa and closeModal, but doesn't affect the visibility or the form type)
setModalForm- type : modal form type to setSet modal form type (similar to openModa and closeModal, but doesn't affect the context or the visibiity)
setModalVisiblity- visibility : modal visibility to setSet modal visibility (similar to openModa and closeModal, but doesn't affect the context or the form type)

Import them from this lib:

import {
  closeModal,
  openModal,
  setModalContext,
  setModalForm,
  setModalVisiblity,
} from "mobrix-engine-plugin-modal";

Then dispatch them from any part of your app:

import { isModalVisible, getModalType } from "mobrix-engine-plugin-modal";
import { useDispatch, useSelector } from "react-redux";

import { Button } from "mobrix-ui";

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

  return (
    <Button
      onClick={() => {
        dispatch(openModal("formOne"));
      }}
    >
      Open form one
    </Button>
  );
};

Selectors

SelectorsReturns
getModalViewModal state, or default state if not enabled
getModalTypeModal type, that can be used as a key to find the right component to show
getModalContextModal context, a custom object associated to the actual modal type (can be null)
isModalVisibleModal visibility, to determine when show or hide the Modal component

Import them from this lib:

import {
  getModalContext,
  getModalType,
  getModalView,
  isModalVisible,
} from "mobrix-engine-plugin-modal";

Then use them from any part of your app:

import { isModalVisible, getModalType } from "mobrix-engine-plugin-modal";
import { useSelector } from "react-redux";

import { Button } from "mobrix-ui";

export const ModalDebugComponent = () => {
  const type = useSelector(getModalType);
  const isVisible = useSelector(isModalVisible);

  return (
    <div>
      <p>{`Actual form type is set to ${type}`}</p>
      <p>{{`Modal is ${isVisible?"opened":"closed"}`}}</p>
  </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, add an interaction with mobrix-engine-modal plugin into you custom plugin. There you can add your callbacks to modal fields (check config section for details). For example, to add a custom function to be called when modal is opened, inside the format function of your custom plugin:
//Just a skeleton of a custom plugin that interacts with modal plugin
const customPlugin = () => ({
  // Custom plugin stuffs

  interactions: [
    {
      plugin: "mobrix-engine-modal",
      effect: (modalConfig) => {
        //Add the custom callback
        modalConfig.onModalOpened.push(() => {
          alert("Modal opened");
        });
      },
    },
  ],
});

Included libraries


Authors


License

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

1.0.1

2 years ago

1.0.0

2 years ago