react-modal_by_cl
v1.1.7
Published
React Modal Component
Downloads
12
Maintainers
Readme
React Modal Component
A reusable, accessible modal dialog implementation for React applications. This component supports features like focus trapping, keyboard accessibility, and overlay click handling.
Table of Contents
Features
- Close the modal using the Escape key.
- Focus trapping within the modal when open.
- Close the modal by clicking on the overlay (background).
- Fully customizable through props for styling and behavior.
Props
|Prop Name|Type|Default Value|Description| |---------|----|-------------|-----------| |isOpen |boolean|undefined|Controls whether the modal is open. Pass true to open and false to close.| |onClose |function|undefined|Callback function executed when the modal is closed. Required for handling close actions| |children |node|undefined|Content to display inside the modal.| |overlayClassName |string|'overlay'|Class name for the overlay. Use for custom styling.| |modalClassName |string|'modal'|Class name for the modal container. Use for custom styling.| |buttonClassName |string|'closeButton'|Class name for the close button. Use for custom styling.|
Usage
Basic Example
import React, { useState } from 'react';
import Modal from './Modal';
import './style.css';
const App = () => {
const [isModalOpen, setIsModalOpen] = useState(false);
return (
<div>
<button onClick={() => setIsModalOpen(true)}>Open Modal</button>
<Modal isOpen={isModalOpen} onClose={() => setIsModalOpen(false)}>
<h1>Modal Content</h1>
<p>This is an example of modal content.</p>
</Modal>
</div>
);
};
export default App;Custom Styling Example
<Modal
isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
overlayClassName="customOverlay"
modalClassName="customModal"
buttonClassName="customCloseButton"
>
<h1>Custom Styled Modal</h1>
</Modal>Accessibility
- Keyboard Navigation:
- Press
Escapeto close the modal - Use
TabandShift + Tabto navigate within the modal content.
- Press
- ARIA Attributes:
role="dialog"andaria-modal="true"are used to ensure compatibility with screen readers.
Key Functions
handleKeyDown
Handles keyboard interactions, including:
- Closing the modal with the
Escapekey. - Managing focus trapping with the
Tabkey.
trapFocus
Ensures focus remains within the modal by cycling through focusable elements.
handleOverlayClick
Closes the modal if a click occurs on the overlay (background).
Styling
The component is fully customizable through CSS class names provided via props.
Default Styles
overlay: {
position: 'fixed',
top: 0,
left: 0,
width: '100%',
height: '100%',
backgroundColor: 'rgba(0, 0, 0, 0.5)',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
zIndex: 1000,
},
modal: {
position: 'relative',
},
closeButton: {
position: 'absolute',
top: '-10px',
right: '-10px',
borderRadius: '100%',
border: 'none',
cursor: 'pointer',
},Default CSS
.modal {
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
text-align: center;
padding: 20px;
}
.closeButton {
background: black;
color: white;
font-size: 20px;
}Custom CSS
Use the overlayClassName, modalClassName, and buttonClassName props to apply your custom styles.
Best Practices
- Provide Required Props:
- Ensure
isOpenandonCloseare passed to control modal behavior properly.
- Ensure
- Test Focus Management:
- Verify focus trapping with
Tabnavigation in various scenarios.
- Verify focus trapping with
- Customize Styles:
- Match the modal's appearance to your application's theme using the class name props.
- Ensure Accessibility:
- Validate ARIA attributes and focus trapping for assistive technologies.
Troubleshooting
- Modal Not Closing on Escape Key
- Verify
onCloseis passed and correctly implemented
- Verify
- Focus Not Trapped:
- Check that all focusable elements inside the modal are correctly defined.
- Overlay Click Not Working:
- Ensure
handleOverlayClickis triggered by checking event propagation.
- Ensure
Notes
- Manage the
isOpenstate in the parent component to avoid unintended behavior.
License
This component is provided under the MIT license. Feel free to use and modify it as needed.
