@magma73/modal-react-typescript
v1.0.6
Published
A simple and customizable modal component for React or Typescript applications.
Downloads
8
Maintainers
Readme
@magma73/modal-react-typescript
Table of Contents
Description
The Modal React component provides a flexible and customizable modal window for use in React applications. With support for various configurations and styling options, this component allows developers to easily implement modal dialogs for displaying additional content, notifications, or interactive elements within their applications.
Technologies
- React V18.2.0
- React-Dom V18.2.0
- TypeScript V5.2.2
- ESLint V8.57.0
- Vite V5.2.0
- NodeJS V20.10.0
- VS Code V1.87.2
Install
To install, you can use npm or yarn:
$ npm install --save @magma73/modal-react-typescript
$ yarn add @magma73/modal-react-typescript
Usage
Example
import React, { useState } from 'react';
import { createPortal } from "react-dom";
import { Modal } from '@magma73/modal-react-typescript';
import '@magma73/modal-react-typescript/dist/style.css';
const App = () => {
const [showModal, setShowModal] = useState(false);
const openModal = () => {
setShowModal(true);
};
const closeModal = () => {
setShowModal(false);
};
return (
<>
<button onClick={openModal}>Ouvrir la modal</button>
{showModal &&
createPortal(
<Modal
onClose={closeModal}
title="Le titre de la modale"
titleClose="Fermer la modale"
/>,
document.body
)}
</>
)
}
export default App
Step by step
- Import component and style file
import { Modal } from '@magma73/modal-react-typescript';
import '@magma73/modal-react-typescript/dist/style.css';
- Create open/close state and save the currently active element with useState hook
import React, { useState } from 'react';
import { createPortal } from "react-dom";
const App = () => {
const [showModal, setShowModal] = useState(false);
...
}
- Create functions open/close
const openModal = () => {
setShowModal(true);
};
const closeModal = () => {
setShowModal(false);
};
- Add component in render with props
{showModal &&
createPortal(
<Modal
onClose={closeModal}
title="Le titre de la modale"
titleClose="Fermer la modale"
/>,
document.body
)}
- Add children to the component
{showModal &&
createPortal(
<Modal
onClose={closeModal}
title="Le titre de la modale"
titleClose="Fermer la modale"
>
<p>Hello world !</p>
</Modal>,
document.body
)}
- Control modal by changing state
<button onClick={openModal}>Open modal</button>
- Choose to customize styles and modal
You can choose to :
- Display the icon with :
showCloseIcon={false}
- Delete the style importation ~~
import '@magma73/modal-react-typescript/dist/style.css';
~~ and add your own style using :{styles.yourStyle}
{showModal &&
createPortal(
<Modal
onClose={closeModal}
customModal={styles.modalContent}
customContainerInformations={styles.containerInformations}
customTitle={styles.title}
customBtnClose={styles.btnClose}
customIconClose={styles.picture}
title="Your Message Has Been Sent"
titleClose="Close the message"
showCloseIcon={false}
>
<p>Hello world !</p>
</Modal>,
document.body
)}
Authors
Marine Magnin
License
ISC © Marine Magnin