Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-modal-component-hw

Package Overview
Dependencies
Maintainers
0
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-modal-component-hw

A simple, modular and customizable modal window component for React applications, compatible with Tailwind CSS, allowing easy management of modal windows with styling and accessibility options

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

⚠️ Package Deprecation

This package is deprecated and will no longer be maintained.

Please use react-ease-modal instead.


status Static Badge Static Badge Static Badge

A simple, modular and customizable modal window component for React applications, compatible with Tailwind CSS, allowing easy management of modal windows with styling and accessibility options

Table of Contents

Installation

To install the Modal component in your React project, use npm or yarn:

npm install react-modal-component-hw clsx lucide-react

or

yarn add react-modal-component-hw clsx lucide-react

Compatibility and Dependencies

The Modal component relies on certain external dependencies to function properly. Here are the minimum required versions of these libraries:

Dependencies

  • clsx : Used to conditionally manage CSS classes. You can combine CSS or Tailwind classes via clsx.

    • Required version: ^2.1.1
    • Installation:
      npm install clsx@^2.1.1
      
      Or
      yarn add clsx@^2.1.1
      
  • lucide-react : Used for SVG icons, including the close icon in the modal.

    • Required version: ^0.446.0
    • Installation:
      npm install lucide-react@^0.446.0
      
      Or
      yarn add lucide-react@^0.446.0
      

Compatibility

  • React : This component is compatible with React 17+ and React 18+.
  • Tailwind CSS (optional) : Although Tailwind is not mandatory, the clsx package allows you to use Tailwind for class and style management if you wish.

Make sure you have installed these dependencies for the component to function properly in your project.


This should cover the minimum requirements for clsx, lucide-react and other aspects related to the component's compatibility.

Usage

To use the Modal component and its sub-components, import it into your file and incorporate it into your JSX code:

import React, { useState } from 'react';
import { Modal, ModalContent, ModalHeader, ModalTitle, ModalDescription, ModalFooter } from 'react-modal-component-hw';

const App = () => {
    const [isOpen, setIsOpen] = useState(false);

    const toggleModal = () => {
        setIsOpen(!isOpen);
    };

    return (
        <div>
            <button onClick={toggleModal}>Open Modal</button>
            <Modal open={isOpen}>
                <ModalContent onClose={toggleModal}>
                    <ModalHeader>
                        <ModalTitle>Title of Modal</ModalTitle>
                    </ModalHeader>
                    <ModalDescription>This is the content of the modal.</ModalDescription>
                    <ModalFooter>
                        <button onClick={toggleModal}>Close Modal</button>
                    </ModalFooter>
                </ModalContent>
            </Modal>
        </div>
    );
};

export default App;

Properties

The Modal component and its sub-components accept the following properties:

Modal
PropertyTypeRequiredDescription
classNamestringOptionalCustom CSS class(es) for the modal.
openbooleanYesIndicates if the modal is open or closed.
childrenReactNodeYesContent to display inside the modal.
ariaLabelledBystringOptionalID of the element defining the modal title for the aria-labelledby attribute.
ariaDescribedBystringOptionalID of the element defining the modal description for the aria-describedby attribute.
ModalContent
PropertyTypeRequiredDescription
classNamestringOptionalCustom CSS class(es) for the modal content.
onClosefunctionYesCallback function called to close the modal (used for the close button).
sizenumberOptionalSize of the close icon. Default: 25.
strokestringOptionalColor of the close icon. Default: '#000000'.
ariaLabelstringOptionalLabel for the aria-label attribute of the close icon.
ModalHeader
PropertyTypeRequiredDescription
classNamestringOptionalCustom CSS class(es) for the modal header.
childrenReactNodeYesContent to display inside the modal header.
ModalTitle
PropertyTypeRequiredDescription
classNamestringOptionalCustom CSS class(es) for the modal title.
childrenReactNodeYesTitle to display inside the modal.
idstringOptionalID used for the aria-labelledby attribute to link the title to the modal.
ModalDescription
PropertyTypeRequiredDescription
classNamestringOptionalCustom CSS class(es) for the modal description.
childrenReactNodeYesDescription to display inside the modal.
idstringOptionalID used for the aria-describedby attribute to link the description to the modal.
ModalFooter
PropertyTypeRequiredDescription
classNamestringOptionalCustom CSS class(es) for the modal footer.
childrenReactNodeYesContent to display inside the modal footer.

Accessibility

The Modal component includes several features to improve accessibility:

  • Use of the role="dialog" attribute to identify the modal window as a dialogue, enhancing the experience for screen reader users.
  • The aria-modal="true" attribute signals to assistive technologies that the background content is not accessible while the modal is open.
  • The aria-labelledby and aria-describedby attributes are used to provide contextual descriptions of the modal:
    • aria-labelledby associates an element, such as a modal title, with the dialogue so that the title is announced.
    • aria-describedby associates a description with the dialogue, offering additional context to users.
  • The addition of the tabIndex="-1" attribute to ensure the modal is focused when the user navigates via the keyboard.
  • The close button includes an aria-label attribute to describe its function to screen reader users.

These best practices enable better interaction with the modal for users utilizing assistive technologies.

Custom Styles

The Modal component allows you to add custom styles using the clsx package, which allows you to easily combine Tailwind classes or your own CSS classes. You don't need to use the component's base classes, you can directly apply your own styles via the className properties of the different sub-components.

Customization

Here is an example of using the component with custom styles:

import { useState } from 'react';
import { Modal, ModalContent, ModalHeader, ModalTitle, ModalDescription, ModalFooter } from 'react-modal-component-hw';
import './styles/main.css';

const App = () => {
 const [isOpen, setIsOpen] = useState(false);

    const toggleModal = () => {
        setIsOpen(!isOpen);
    };

    return (
        <div>
           <button className="btn" onClick={toggleModal}>
                Open modal
            </button>
            <Modal className="custom-modal" open={isOpen}>
                <ModalContent className="custom-modal-container" onClose={toggleModal} stroke="#dfdfdf" size={32}>
                    <ModalHeader className="custom-modal-header">
                        <img className="custom-modal-img" src="./hero.svg" alt="hero" />
                        <ModalTitle className="custom-modal-title">Title of Modal</ModalTitle>
                        <ModalDescription className="custom-modal-description">This is the content of the modal.</ModalDescription>
                    </ModalHeader>
                    <ModalFooter className="custom-modal-footer">
                        <button
                            className="custom-modal-button"
                            onClick={toggleModal}
                        >
                            Close Modal
                        </button>
                    </ModalFooter>
                </ModalContent>
            </Modal>
    );
};

export default App;

Demonstration

You can see the demo of this modal component at the following address: https://aeonshad.github.io/react-modal-component-hw/

The source code is available at this address: https://github.com/aeonshad/react-modal-component-hw

License

This project is licensed under the MIT.

Keywords

FAQs

Package last updated on 01 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc