Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cicara/better-modal

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cicara/better-modal

a tool to help simplify antd modal state management

  • 0.0.1-3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
22
increased by144.44%
Maintainers
1
Weekly downloads
 
Created
Source

Better Modal

A tool to help simplify antd modal state management

Installation

pnpm add @cicara/better-modal

Usage

  1. Create an Antd Modal in the usual way, with the only difference that the state of the Modal is managed automatically using useModalContext.

    import { Input, Modal } from "antd";
    import { useModalContext } from "@cicara/better-modal";
    import { Form } from "antd";
    import { useCallback, useEffect } from "react";
    
    export type MyModalProps = {
      value?: string;
    };
    
    export function MyModal(props: MyModalProps) {
      const modal = useModalContext<string>(); // <- First use modal context
    
      const [form] = Form.useForm();
    
      const handleSubmit = useCallback(
        (values: { name: string }) => {
          modal.resolve(values.name); // <- Close the modal and return the data or message
        },
        [modal.resolve]
      );
    
      useEffect(() => {
        if (!modal.visible) {
          return;
        }
        form.setFieldsValue({ name: props.value });
      }, [props.value]);
    
      return (
        <Modal
          open={modal.visible}
          title="What's your name?"
          onOk={() => form.submit()}
          onCancel={() => modal.reject(new Error("user cancel"))} // <- Close modal with reject and reason
          afterClose={modal.destroy} // Optional, destroy the modal
        >
          <Form form={form} onFinish={handleSubmit} autoComplete="off">
            <Form.Item name="name" rules={[{ required: true }]}>
              <Input placeholder="your name..." />
            </Form.Item>
          </Form>
        </Modal>
      );
    }
    
  2. Use the useModal<string>(MyModal) react hook to create the modal and insert {myModal.placeholder} in the right place so that the modal can get the Context correctly.

    import { useModal } from "@cicara/better-modal";
    import { MyModal } from "./my-modal";
    
    export function App() {
      const myModal = useModal(MyModal);
    
      return <div>{myModal.placeholder}</div>;
    }
    
  3. Use the myModal.show function to display the modal and passing props, the show function returns a Promise, which corresponds to the resolve and reject function in the useModalContext.

    import { Button } from "antd";
    import { useModal } from "@cicara/better-modal";
    import { MyModal } from "./my-modal";
    import { useCallback } from "react";
    
    export function App() {
      const myModal = useModal(MyModal);
    
      const handleOpenModal = useCallback(async () => {
        try {
          const result = await myModal.show<string>({ value: "hungtcs" });
          alert(result);
        } catch (err) {
          alert(err);
        }
      }, []);
    
      return (
        <div>
          <Button onClick={handleOpenModal}>Open Modal</Button>
          {myModal.placeholder}
        </div>
      );
    }
    

Keywords

FAQs

Package last updated on 04 Apr 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc