npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

modal-react-component

v0.1.4

Published

modal open and close with cutomization with button

Downloads

7

Readme

Modal

The Modal component is a reusable React component that displays a modal window with customisable content.

Installation

To install the npm package, use the following command:

npm install modal-react-component

Use

To use the Modal component in your project, import it from the npm package and use it in your code like any other React component:

import Modal from 'your-npm-package-name';

const App = () => {
  // ...

  return (
    // ...
    <Modal
      isOpen={modalOpen}
      onClose={handleCloseModal}
      modalStyle={{ backgroundColor: "white" }}
      modalClassName="my-custom-modal-class"
      modalTitle="My Custom Modal Title"
    >
      {/* Customisable modal content*/}
    </Modal>
    // ...
  );
};

Props

The Modal component accepts the following props:

  • isOpen (boolean): Indicates whether the modal should be opened or closed.
  • onClose` (function): Callback function called when the user closes the modal.
  • children` (node): The customisable content of the modal.
  • modalStyle (object): An object containing inline styles for the modal.
  • modalClassName (string) : A string containing CSS class names for the modal.
  • modalTitle (string) : A string containing the title of the modal.
  • firstName (string) : A string containing the first name to be displayed in the modal.
  • lastName (string) : A string containing the last name to be displayed in the modal.
  • onSave(function): A callback function called when the user clicks thesave` button.
  • onCancel` (function): Callback function called when the user clicks the "No thanks" button.

Example

Here's an example of using the Modal component to display data from a form and allow the user to save or cancel changes:

import React, { useState } from "react";
import Modal from 'your-npm-package-name';
import Form from "./Form";

const App = () => {
  const [modalOpen, setModalOpen] = useState(false);
  const [formData, setFormData] = useState({});

  const handleOpenModal = () => {
    setModalOpen(true);
  };

  const handleCloseModal = () => {
    setModalOpen(false);
  };

  const handleFormData = (data) => {
    setFormData(data);
  };

  const handleSave = () => {
    // code for saving form data
    console.log("Données du formulaire enregistrées :", formData);
    handleCloseModal();
  };

  const handleCancel = () => {
    // code to cancel changes to the form
    console.log("Modifications du formulaire annulées");
    handleCloseModal();
  };

  return (
    <div className="app">
      <Form handleFormData={handleFormData} />
      <button className="open-modal-button" onClick={handleOpenModal}>
        Open Modal
      </button>
      <Modal
        isOpen={modalOpen}
        onClose={handleCloseModal}
        modalStyle={{ backgroundColor: "white" }}
        modalClassName="my-custom-modal-class"
        modalTitle="My Custom Modal Title"
        firstName={formData.firstName}
        lastName={formData.lastName}
        onSave={handleSave}
        onCancel={handleCancel}
      >

      </Modal>
    </div>
  );
};

export default App;

In this example, we have a Form component which manages the form data and a handleFormData function which is passed as a prop to the Form component to receive updates to the form data. When the user enters data into the form, the Form component calls the handleFormData function to update the formData state in the App component.

We also have two functions, handleSave and handleCancel, which are passed as props to the Save and No thanks buttons in our Modal component. These functions are called when the user clicks on the buttons to save or cancel changes to the form.

Finally, we pass the form data as props to our Modal component to display the information in the modal.

Licence

This package is published under the ..... licence

Component Modal implementation :

import React, { useRef, useEffect } from "react";

const Modal = ({
  isOpen,
  onClose,
  children,
  modalStyle,
  modalClassName,
  modalTitle,
  firstName,
  lastName,
  onSave,
  onCancel
}) => {
  const modalRef = useRef(null);
  const triggerRef = useRef(null);

  useEffect(() => {
    if (isOpen) {
      triggerRef.current = document.activeElement;
      modalRef.current.focus();
    } else if (triggerRef.current) {
      triggerRef.current.focus();
      triggerRef.current = null;
    }
  }, [isOpen]);

  const handleBackdropClick = (event) => {
    if (event.target === event.currentTarget) {
      onClose();
    }
  };

  const modalContent = (
    <div className="modal-content">
      {modalTitle && <h2 className="modal-title">{modalTitle}</h2>}
      <button className="close-button" onClick={onClose}>
        <span>X</span>
      </button>
      {firstName && lastName && (
        <>
          <p>Enregistrez les informations ?</p>
          <p>Nom: {lastName}</p>
          <p>Prénom: {firstName}</p>
          <div className="modal-buttons">
            <button className="save-button" onClick={onSave}>Enregistrer</button>
            <button className="cancel-button" onClick={onCancel}>Non merci</button>
          </div>
        </>
      )}
      {children}
    </div>
  );
  
  return isOpen ? (
    <div
      className="modal-backdrop"
      onClick={handleBackdropClick}
    >
      <div
        className={`modal open ${modalClassName}`}
        style={modalStyle}
        aria-modal="true"
        tabIndex="-1"
        ref={modalRef}
      >
        {modalContent}
      </div>
    </div>
  ) : null;
};
export default Modal;