1.0.10 • Published 9 months ago

modal-kf-react v1.0.10

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

Modal Component

The Modal component is a reusable React component written in TypeScript that renders a modal dialog box. It provides a flexible and customizable way to display information, warnings, or errors.

To use this modal, you will need to import the provider in the parent component and wrap the children in it. You will then need to import to context hook from react, and the context from the module in the children, and use the function "displayModal" to display and modify it.

Here is the installation step by step.

Installation

To use the Modal component in your React project, follow these steps:

Install the required dependencies by running the following command:

npm install modal-kf-react

First, import the Provider in your root file, such as index.jsx

import ModalProvider from "modal-kf-react/ModalProvider";

Then, you will need the useContext from react and the context from the module in the desired children file.

import { useContext } from "react";

import { ModalContext } from "modal-kf-react/ModalProvider";

You're ready to use the Modal component in your application!

Usage

You need to wrap your app in the Provider

index.jsx

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <ModalProvider>
      <App />
    </ModalProvider>
  </React.StrictMode>
);

app.jsx (or any children with the imports)

const { DisplayModal } = useContext(ModalContext);
 const showModal = () => {
    DisplayModal({
      mode: "info",
      title: "Test modal",
      children: "This modal should be diplayed in blue (because of the mode: info)",
    });
  };

  ...

   return (
    <>
      <button onClick={showModal}>test modal</button>
    </>
  );

With this example, clicking on the button should display a blue modal with a title and a children. Clicking on the close button, or anywhere outside the modal should close it.

Typescript usage

You can use the same setup above if your application is in Typescript, you'll need to also import the props in the children element

import { DisplayModalProps } from "modal-kf-react/ModalProvider";

and then add the props in the children element

App.tsx

 const showModal = () => {
    DisplayModal({
      mode: "info",
      title: "Test modal",
      children: "This modal should be diplayed in blue (because of the mode: info)",
    } as DisplayModalProps);
  };

Props

The Modal component accepts several props to customize its behavior and appearance:

NameTypeRequiredDescriptionDefault
modestringrequiredThe mode of the modal. Possible values are "default," "info", "warning", "error" or "custom". Default use the pattern shown in the base project, info warning and error are premade modals. "Custom" is a blank modale with no default, this is mainly used to display full components. You can overwrite any props using those modes.-------
titlestringoptionalThe title of the modal.-------
childrenReactNodeoptionalThe children components to be rendered inside the modal.-------
classNamestringoptionalAdditional class name for styling the modal.-------
onClosedfunctionoptionalCallback function invoked when the modal is closed.return
onClosefunctionoptionalCallback function invoked to determine whether the modal should be closed. Should return a boolean value.return true
enableFadeInbooleanoptionalEnables fade-in animation for the modal.true
enableFadeOutbooleanoptionalEnables fade-out animation for the modal.true
delaynumberoptionalSet a timer for the display of the modal.-1
heightstring or numberoptionalSet the height of the modalauto
widthstring or numberoptionalSet the width of the modalauto
modalPositionstringoptionalSet the modal position. Possible values are "center", "top-left", "top-right", "bottom-left", "bottom-right""center"
backgroundColorstringoptionalSet the background color of the modalDepends on the mode
backgroundColorTitlestringoptionalSet the background color of the title, if it existsDepends on the mode
textColorstringoptionalSet the color of the text inside the modalmode "default" : black ; other modes "white"
closePositionstringoptionalSet the position of the close button on the modal. Possible values are "right", "left", "outside-right", "outside-left""outside-right"
borderstringoptionalSet the border of the modal"solid black 2px"
borderRadiusstringoptionalSet the border radius of the modal"20px"
borderInsidestringoptionalSet the color of the border on top or on the left of the children"solid black 1px"
boxShadowstringoptionalSet the box shadow of the modal"none"
orientationstringoptionalSet the orientation of the display inside the modal. The display is in flex. Accepts only "row"."column"
1.0.10

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago