Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@thanhnn/use-modal
Advanced tools
Small typescript package that help you to easily control modals in your React application.
Small typescript package that help you to easily control modals in your React application.
Feature | Description | Type |
---|---|---|
Open modal | Open a specific modal by passing its unique name. | (name: string) => void |
Close modal | Close a specific modal by passing its unique name. | (name: string, shouldCallback: boolean = true) => void |
Close all modals | Close all open modals. | (shouldCallback: boolean = true) => void |
Check if modal is opened | Check if a specific modal is open by passing its unique name. | (name: string) => boolean |
Determine close callback | Props closeCallback which is a method that will be called when the modal is closed. | (name: string, callback: () => void) => void |
Install with npm or yarn
npm install --save @thanhnn/use-modal
yarn add @thanhnn/use-modal
Wrap your app with the ModalProvider
component to enable modal handling throughout your application:
import { ModalProvider } from '@thanhnn/use-modal';
function App() {
return <ModalProvider>{/* Your app code */}</ModalProvider>;
}
Use the useModal
hook to access modal handling functions and properties:
You can provide type TModalName
or not.
import { useModal } from '@thanhnn/use-modal';
type TModalName = ['modal-a', 'modal-b'];
function MyComponent() {
const { open, close, closeAll, check } = useModalH<TModalName>();
return (
<>
<button onClick={() => open('modal-a')}>Open</button>
<button onClick={() => closeModal('modal-b')}>Close</button>
<button onClick={closeAllModal}>Close All</button>
<p>Modal is open: {check('modal-a') ? 'Yup' : 'Nope'}</p>
</>
);
}
Use the withModal
higher-order component to add modal handling capabilities to your own modal components:
You can provide type TModalName
or not.
import { withModal, TWithModalProps} from '@thanhnn/use-modal';
type TModalProps = TWithModalProps & {
content: string
}
type TModalName = ['modal-a', 'modal-b'];
function Modal({ content= "Content", open, onClose }: TModalProps) {
return (
<div>
<p>{content}</p>
<p>{open ? 'Open': 'Close'}</p>
<button onClick={onClose}>Close Modal</button>
</div>
);
}
export default withModal<TModalProps, TModalName>(Modal, 'modal-a');
Now you can use the Modal
component like this:
import Modal from './Modal';
import { useModal } from '@thanhnn/use-modal';
function Component() {
const { open } = useModal();
return (
<div>
<button onClick={() => open('modal-a')}>Open Modal</button>
<Modal closeCallback={() => { alert('Modal is closed)'}}/>
</div>
);
}
export default Component;
@thanhnn/use-modal
is a powerful yet lightweight typescript package for managing modals in your React application.
This package is licensed under the MIT License.
FAQs
Small typescript package that help you to easily control modals in your React application.
The npm package @thanhnn/use-modal receives a total of 0 weekly downloads. As such, @thanhnn/use-modal popularity was classified as not popular.
We found that @thanhnn/use-modal demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.