1.0.1 • Published 9 months ago

@loumorganrene/react-mini-modal v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

React Mini Modal

React Mini Modal is a minimalist and easy-to-style modal component for React.

Prerequisites

My Table of Content

Installation

You can install the react-mini-modal package using npm:

npm install @loumorganrene/react-mini-modal

Minimal Basic Usage

Import Modal in your React component:

import Modal from '@loumorganrene/react-mini-modal';

Implement Modal in your React component:

function MyComponent() {
  const [open, setOpen] = useState(false)

  return (
    <div>
        <button onClick={() => setOpen(!open)}>Open Modal</button>

        <Modal
          content="Here is your modal's content"
          open={open}
          onClose={() => setOpen(false)}
        />
      </div>
  )
}

In the above example, the Modal component is rendered with only the required props such as content, open, and onClose.

Modal Props

The Modal component offers some more props to facilitate is styling:

PropTypeDescriptionRequiredDefault Value
titlestringThe title of the modal.No-
contentstringThe content of the modal.Yes-
heightstringThe height of the modal.No-
widthstringThe width of the modal.No-
colorstringThe color of the modal text.No"#6b7280"
footerButtonJSX.ElementA custom button component for the modal footer.Yes-
backgroundColorstringThe background color of the modal.No"#fff"
openbooleanBoolean indicating whether the modal is open or closed.Yesfalse
onClose() => voidCallback function to be called when the modal is closed.Yes-

Example with Button Component

The Modal component is distributed with a minimalist Button component to facilitate the addition of a footerButton if needed. Here's a example including it :

Import Button in your React component:

import Button from '@loumorganrene/react-mini-modal';

Implement Button as the footerButton prop value of your Modal component:

function MyComponent() {
  const [open, setOpen] = useState(false)

  return (
    <div>
        <button onClick={() => setOpen(!open)}>Open Modal</button>

        <Modal
          content="Here is your modal's content"
          footerButton={<Button onClick={() => setOpen(false)} content="This button close the modal" />}
          open={open}
          onClose={() => setOpen(false)}
        />
      </div>
  )
}

Button Props

The Button component require only two props:

PropTypeDescriptionRequired
contentstringThe text content of the button.Yes
onClick() => voidCallback function for click event.Yes

Advanced Styling

Aside from using the provided styling props such as color, width or backgroundColor, the appearance of the Modal component can be customized using CSS classes. Here's the available BEM's CSS classes :

CSS Class Description

ClassDescription
.modal__backgroundStyles the background overlay of the modal window. By default, it sets the coverage on the entire viewport and applies a blurry effect.
.modal__wrapperStyles the modal wrapper element. By default, it positions the modal at the center of the screen and applies a box shadow.
.modalStyles the modal content container. By default, it sets the gap between elements and the font styling.
.modal__headerStyles the header of the modal. By default, it provided an empty space to displays the title.
.modal__titleStyles the title of the modal. By default, it's applied on a h2.
.modal__bodyStyles the body of the modal. By default, it provided an empty space to displays the content.
.modal__contentStyles the content of the modal. By default, it's applied on a p.
.modal__footerStyles the footer of the modal. By default, it provided an empty space to displays buttons or elements added through the footerButton prop.
.modal__btnStyles this library Button component.
.modal__btn--closeStyles the close button icon. By default, it positions the icon at the top right corner of the modal.

DOM Structure

<!-- Background Page Covering/Blurrying -->
<div className='modal__background'>
            <div className='modal__wrapper'>
            <div className='modal'>
                <!-- Close Icon -->
                <button className='modal__btn modal__btn--close' />

                <!-- Header with Title -->
                <div className="modal__header">
                    <h2 className="modal__title" />
                </div>

                <!-- Body with Content -->
                <div className="modal__body">
                    <p className="modal__content" />
                </div>

                <!-- Footer with footerButton -->
                <div className="modal__footer">
                    <button className='modal__btn' />
                </div>
            </div>
        </div>
    </div>
1.0.1

9 months ago

1.0.0

10 months ago

0.0.1

10 months ago