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

nice-use-modal

v2.0.0

Published

A React modal hook

Downloads

41

Readme

nice-use-modal

license MIT npm downloads

Docs

Introduction

Using modals and drawers in React can be a bit of a pain. nice-use-modal can help alleviate this annoyance.

Features

  • 🚀 No UI Dependency: It only manages state and rendering internally, so you can use any UI library or components you like.
  • 🚀 No Side Effects: It uses createContext internally to maintain context, avoiding the loss of global configurations in rendering.
  • 🚀 More Flexible: Use it as hooks, call it imperatively, no need to import ReactNode into components.
  • 🚀 Simpler: The internal state is automatically reset when the modal is closed, so you don't need to manage it manually.
  • 🚀 TypeScript Support: Written in TypeScript, it provides TypeScript hints.

Installation

# pnpm
pnpm add nice-use-modal

# yarn
yarn add nice-use-modal

# npm
npm i nice-use-modal -S

Examples

main.tsx

import { ModalProvider } from "nice-use-modal";

ReactDOM.createRoot(document.getElementById("root")!).render(
    <ModalProvider>
      <App />
    </ModalProvider>
);

MyModal.tsx

The usage of Drawer and Modal is the same.

import React, { useEffect } from "react";
import { Modal, message } from "antd";
import { ModalProps } from "nice-use-modal";

interface IData {
  title?: string;
  desc?: string;
}

interface IProps {
  onOk: () => void;
  onCancel?: () => void;
}

export default (p: ModalProps<{
  data: IData;
  props: IProps;
}>) => {
  const { visible, hide, destroy, data = {}, props } = p;

  const { title = "New", desc = "Hello World!" } = data;
  const { onOk, onCancel } = props;

  useEffect(() => {
    message.info("The component is registered only when the show method is executed.");
  }, []);

  return (
    <Modal
      title={title}
      onOk={() => {
        onOk?.();
        hide();
      }}
      open={visible}
      onCancel={() => {
        onCancel?.();
        hide();
      }}
      afterClose={() => destroy()} // For components with closing animations, it's best to destroy the component after the animation ends to preserve the animation effect.
    >
      {desc}
    </Modal>
  );
};

home.tsx

import MyModal from "./MyModal";
import { Button, Space, message } from "antd";
import { useModal } from "nice-use-modal";

export default () => {
  const { show, hide, destroy } = useModal(MyModal, {
    onOk: () => {
      message.success("ok");
    },
    onCancel: () => {
      message.error("cancel");
    },
  });

  return (
    <>
      <Space>
        <Button
          onClick={() => {
            show();
          }}
        >
          New
        </Button>
        <Button
          onClick={() =>
            show({
              title: "Edit",
              desc: "You can pass data in real-time for internal use in the component.",
            })
          }
        >
          Edit
        </Button>
        <Button onClick={() => destroy()}>Destroy</Button>
      </Space>
    </>
  );
};

You can also use the "useModal" approach directly (I recommend this approach more).

// MyModal.tsx
import { useModal } from "nice-use-modal";

export default (props: IProps;)=>{
  return useModal<{
    data: IData;
  }>(({
    visible,
    hide,
    destroy,
    data,
  })=>{
    return <div>hello world</div>
  })
}

This way, you can use Modal anywhere.

import useMyModal from './MyModal'

const {show,hide,destroy} = useMyModal({
  onOk:()=>{},
  onCancel:()=>{}
})

API

import { useModal } from 'nice-use-modal';
import type { ModalProps , ModalResult } from 'nice-use-modal';

const Result:ModalResult = useModal<{data:T;props:K}>((Props:ModalProps<{data:T;props:K}>)=>{},props)

Props

| Parameter | Description | Type | Default | Version | | --------- | ----------------------------------- | -------------------------------------- | ----------- | ------- | | visible | Whether to show | boolean | false | - | | hide | Hide the modal | () => void | - | - | | destroy | Destroy the modal | () => void | - | - | | data | Data passed when opening the modal | T \| Record<string,any> \| undefined | - | - | | props | Props passed when registering modal | K | undefined | 1.1.0 |

Note: The difference between hide and destroy is that hide preserves the modal's state, while destroy destroys the modal's state. For modals with closing animations, it's best to use hide first, and then destroy after the animation ends. Using destroy directly may cause the animation to not end properly.

Note: The difference between data and props is that data is passed each time the modal is opened, while props are passed when the modal is registered and do not change.

Result

| Parameter | Description | Type | Default | | --------- | ----------- | ------------------------------------------ | ------- | | show | Show | (data?: T \| Record<string,any>) => void | - | | hide | Hide | () => void | - | | destroy | Destroy | () => void | - |