2.0.0 β€’ Published 6 months ago

nice-use-modal v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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

ParameterDescriptionTypeDefaultVersion
visibleWhether to showbooleanfalse-
hideHide the modal() => void--
destroyDestroy the modal() => void--
dataData passed when opening the modalT \| Record<string,any> \| undefined--
propsProps passed when registering modalKundefined1.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

ParameterDescriptionTypeDefault
showShow(data?: T \| Record<string,any>) => void-
hideHide() => void-
destroyDestroy() => void-
1.1.4

7 months ago

2.0.0

6 months ago

1.1.3

9 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago

1.0.0-beta.2

9 months ago

1.0.0-beta.1

9 months ago

1.0.0-beta.0

9 months ago