sg-modal-lib
TypeScript icon, indicating that this package has built-in type declarations

1.1.8 • Public • Published

SG Modal Library

Static Badge Static Badge

🖥 Tester la librairie sur CodeSandBox

🔎 Voir la librairie sur npm

sg-modal-lib is a simple and flexible React library for creating and managing modals in your web applications. It allows you to easily create custom modals with themes and configurations.

Features

  • Easy to use : Simple library for creating modals.
  • Customizable : Apply different themes (light/dark) and custom button text.
  • Lightweight : Built with minimal dependencies to keep your bundle size small.
  • Tested : Thoroughly tested with unit tests to ensure reliability and performance.
  • Accessible : Designed with accessibility in mind, following best practices to ensure usability for all users.
  • Responsive : Adapts seamlessly to various screen sizes and devices for a consistent user experience.
  • Flexible : Supports various modal types (confirmation, alert, etc.) to meet diverse use cases.
  • Easy integration : Works well with popular frameworks and libraries, making it simple to implement in existing projects.

Installation

Prerequisites

  • NodeJS (^20.11.1)
  • npm (^10.2.4)
  • IDE (VSCode recommended)

To install the sg-modal-lib library, you can use npm, pnpm or yarn :

npm install sg-modal-lib

or

pnpm install sg-modal-lib

or

yarn add sg-modal-lib

Usage

Here's a basic example of how to use the Modal component in your React application :

import { useState } from 'react';
import { Modal } from 'sg-modal-lib';
import 'sg-modal-lib/style.css'; // Import default styles function

const App = () => {
  const [isModalOpen, setIsModalOpen] = useState(false);

  const toggleModal = () => {
    setIsModalOpen((prevIsOpen) => !prevIsOpen);
  };

  return (
    <div>
      <button onClick={toggleModal}>Open Modal</button>
      <Modal
        isOpen={isModalOpen}
        toggleModal={toggleModal}
        title='Title' /* Optionnal, default to null */
        btnText='close' /* Optionnal, default to null */
        escapeClose={false} /* Optionnal, default to true */
        overlayClickClose={false} /* Optionnal, default to true */
        showClose={false} /* Optionnal, default to true */
        fadeDuration={300} /* Optionnal, default to 0 */
        styleTheme='dark' /* Optionnal, default to light */
      >
        <p>This is the content inside the modal!</p>
      </Modal>
    </div>
  );
};

export default App;

Props

Modal

The Modal component accepts the following props :

Name Type Required Default value Description
isOpen boolean yes / Indicates whether the modal is open or closed
toggleModal function yes / Function to toggle the modal's visibility
children React.ReactNode yes / The content to be displayed inside the modal
escapeClose boolean no true Allows the user to close the modal by pressing ESC
overlayClickClose boolean no true Allows the user to close the modal by clicking the overlay
showClose boolean no true Shows a (X) icon/link in the top-right corner
title string no null The title of the modal, displayed on top if provided. If no provided, it's not displayed.
btnText string no null The text for the additionnal bottom to close the modal, if provided. If no btnText provided, no button is displayed.
styleTheme string no light Optional theme for the modal (e.g., 'light', 'dark').
fadeDuration number no 0 Number of milliseconds the fade transition takes

Styling

The library includes default styles that can be imported using :

import 'sg-modal-lib/style.css';

You can also override these styles in your project by applying additional styles.

Css className :

  • sg-modal-lib
  • sg-modal-lib--open
  • sg-modal-lib--close
  • sg-modal-lib__dialog
  • sg-modal-lib__dialog--open
  • sg-modal-lib__dialog--close
  • sg-modal-lib__close
  • sg-modal-lib__title
  • sg-modal-lib__children
  • sg-modal-lib__btn

Remember to import the CSS file before your own so that you can override it.

Light theme :
Light theme screenshot
Dark theme :
Dark theme screenshot

Dependencies

Production Dependencies

Development Dependencies

Authors

  • Ségolène Ganzin - Initial work and main development (GitHub Profile)

Readme

Keywords

Package Sidebar

Install

npm i sg-modal-lib

Weekly Downloads

1

Version

1.1.8

License

ISC

Unpacked Size

367 kB

Total Files

8

Last publish

Collaborators

  • segolenegz