# @frankhu00/react-modal

> React modal

Latest version **1.2.0** (published 2021-01-01) · ISC license · 0 weekly downloads

## Install

```sh
npm install @frankhu00/react-modal
pnpm add @frankhu00/react-modal
yarn add @frankhu00/react-modal
bun add @frankhu00/react-modal
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2021-01-01 |
| First published | 2020-12-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 62.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Frank Hu |
| Maintainers | frankhu00 |
| Keywords | React, modal, lightbox |

## Links

- npm: https://www.npmjs.com/package/@frankhu00/react-modal
- Repository: https://github.com/frankhu00/react-modal
- Homepage: https://github.com/frankhu00/react-modal#readme
- Issues: https://github.com/frankhu00/react-modal/issues
- npm.io page: https://npm.io/package/@frankhu00/react-modal

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2021-01-01
- 1.1.0 — 2020-12-20
- 1.0.0 — 2020-12-20

## README

# React Modal

A simple `react` + `styled-components` modal

## Package Interface

Named exports: `Modal`, `useModalContext`, `ModalContainer`, and `ModalContent`  
Default export: `Modal`

## Usages

### 1) Pass in a function as children to access modal methods

```javascript
<Modal content={() => <div>Modal Content</div>}>
    {({ showModal }) => <button onClick={showModal}>Button to trigger modal</button>}
</Modal>
```

### 2) Modal methods are passed to the direct descendant

```javascript
const SampleButton = ({ showModal }) => {
    return <button onClick={showModal}>Sample Button</button>;
};

<Modal content={() => <div>Modal Content</div>}>
    <SampleButton />
</Modal>;
```

### 3) Using ContextAPI to access modal methods

```javascript
const GrandchildButton = () => {
    const { showModal } = useModalContext();

    return <button onClick={showModal}>Grandchild Button</button>;
};

<Modal content={() => <div>Modal Content</div>}>
    <div className="component-container">
        <GrandchildButton />
    </div>
</Modal>;
```

## Modal Props

| Prop                  | Type                         | Default                 | Description                                                                                                                                                                                       |
| --------------------- | ---------------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| content               | Component \| () => Component | undefined               | A component or function that returns a component to serve as the modal content                                                                                                                    |
| onModalShow           | func                         | undefined               | Function to trigger when the modal is shown                                                                                                                                                       |
| onModalClose          | func                         | undefined               | Function to trigger when the modal is closed                                                                                                                                                      |
| animationDuration     | number                       | 500                     | The fade in / out duration in ms                                                                                                                                                                  |
| showOnRender          | boolean                      | false                   | If true, modal will display when mounted                                                                                                                                                          |
| showXBtn              | boolean                      | true                    | If true, a "x" button will rendered at the top right of the modal                                                                                                                                 |
| persist               | boolean                      | false                   | If true, the modal will be mounted in dom and hidden / shown via CSS                                                                                                                              |
| closeOnBackdrop       | boolean                      | true                    | If true, clicking on the backdrop will close the modal                                                                                                                                            |
| addControls           | boolean                      | false                   | If true, modal will automatically append two action buttons: "Close" and "Confirm". The "Close" button will close the modal, and the action of the "Confirm" is dependent on "proceedAction" prop |
| proceedAction         | func                         | () => console.warn(...) | The onClick handler for the "Confirm" button created from "addControls" prop                                                                                                                      |
| proceedBtnText        | string                       | "Confirm"               | The text displayed for "Confirm" button                                                                                                                                                           |
| closeBtnText          | string                       | "Close"                 | The text displayed for "Close" button                                                                                                                                                             |
| CustomModalContainer  | Component                    | ModalContainer          | Defines the component that warps around the entire modal                                                                                                                                          |
| CustomModalContent    | Component                    | ModalContent            | Defines the component used to wrap around the modal content                                                                                                                                       |
| CustomPrimaryButton   | Component                    | button element          | The component that is used to generate the "Confirmed" button                                                                                                                                     |
| CustomSecondaryButton | Component                    | button element          | The component that is used to generate the "Closed" button                                                                                                                                        |

## Modal Methods

Below are the methods available to control the modal

| Method                 | Inputs                                           | Description                       |
| ---------------------- | ------------------------------------------------ | --------------------------------- |
| showModal              | none                                             | Displays the modal                |
| closeModal             | none                                             | Closes the modal                  |
| toggleModal            | none                                             | Toggles the state of the modal    |
| setDynamicContent      | Component \| Func Component                      | Updates the contents of the modal |
| updateOptions          | { showXBtn, addControls, ...etc }                | Updates the specified option      |
| updateCustomComponents | { Container, Content, PrimaryBtn, SecondaryBtn } | Updates the custom components     |

## Additional Notes

### ModalContainer styled component

**Extend ModalContainer with styled-components to make furthur css changes** since this component is looking for `show`, `zIndex`, `closeOnBackdrop`, `stage`, `animationDuration` prop for animations and default styling

-   `show` is used to toggle the css display rule
-   `zIndex` sets z-index rule
-   `closeOnBackdrop` will set the cursor rule
-   `stage` is used to determine the component's rendering stage (entering vs exiting) to use either fade in or fade out animation
-   `animationDuration` specifies the duration of the animation

---
_Source: https://npm.io/package/@frankhu00/react-modal · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
