npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@thanhnn/use-modal

v0.1.1

Published

Small typescript package that help you to easily control modals in your React application.

Downloads

1

Readme

@thanhnn/use-modal

Small typescript package that help you to easily control modals in your React application.

Features

| 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 |

Installation

Install with npm or yarn

npm install --save @thanhnn/use-modal
yarn add @thanhnn/use-modal

Usage/Examples

ModalProvider

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>;
}

useModal

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>
    </>
  );
}

withModal

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;

Conclusion

@thanhnn/use-modal is a powerful yet lightweight typescript package for managing modals in your React application.

License

This package is licensed under the MIT License.