use-open
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

use-open

hooks for react component open state, and call with method

support antd, mui, shadcn/ui and any other react components

Install

yarn add use-open

Usage

// page/index.tsx
const Page = () => {
    const opener = useOpen();

    return (
        <>
            <button onClick={() => opener.open()}>
                click
            </button>
            <Modal opener={opener} />
        </>
    );
};

// components/Modal.tsx
import { createPortal } from 'react-dom';
import { OpenInstance, useOpen, useOpenProps } from './index';

// component original props
interface IProps {

}

interface OpenProps {
    onConfirm: () => void;
}

const Modal = (props: IProps & {
    opener: OpenInstance<OpenProps>
}) => {
    const openProps = useOpenProps(props.opener);

    return createPortal((
        <AntModal
            open={openProps.opened}
        >
            modal
            <button
                onClick={() => openProps.onConfirm() && openProps.close()}
            >
                click
            </button>
        </AntModal>
    ), document.body);
};

/use-open/

    Package Sidebar

    Install

    npm i use-open

    Weekly Downloads

    5

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    7.64 kB

    Total Files

    6

    Last publish

    Collaborators

    • yeliex