# useModals (https://react-easy-modals.alexandrejoly.me/docs/use-modals)

The main entry point for the modals API.

## modals.open

Opens a modal. Returns a promise that resolves when the modal is closed. Value can be determined by the modal using its `close(value)` prop, otherwise it will resolve to `undefined`.

```tsx
import { useModals } from "react-easy-modals";
import MyModal from "./MyModal";

const modals = useModals();
const value = await modals.open(MyModal, props, options);
```

| Param | Type | Required | Description |
| --- | --- | --- | --- |
| `component` | `ReactComponent` | Yes | Your React modal component |
| `props` | `any` | No | Props for the modal |
| `options` | `object` | No |  |
| `options.id` | `string` | No | Gives the modal an id, which can be used for `closeById(id)` |
| `options.replace` | `boolean` | No | This modal will replace the last modal in the stack |

## modals.close

Closes the current modal or last amount of modals in the stack. Returns `true` if the modal was closed, `false` if the modal prevented the close via `useBeforeClose`.

```tsx
import { useModals } from "react-easy-modals";

const modals = useModals();

// Close the current modal
modals.close();

// Close the last 2 modals
modals.close(2);

// Force close, ignoring useBeforeClose callbacks
modals.close(1, { force: true });
```

| Param | Type | Required | Description |
| --- | --- | --- | --- |
| `amount` | `number` | No | The number of modals to close. Defaults to 1 |
| `options` | [`CloseOptions`](/docs/props#closeoptions) | No | Options for closing |

## modals.closeById

Closes a modal by its id. Returns `true` if the modal was closed, `false` if the modal prevented the close via `useBeforeClose`.

```tsx
import { useModals } from "react-easy-modals";

const modals = useModals();

modals.open(MyModal, {}, { id: "my-modal" });
modals.closeById("my-modal");

// Force close, ignoring useBeforeClose callback
modals.closeById("my-modal", { force: true });
```

| Param | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | Yes | The id of the modal to close |
| `options` | [`CloseOptions`](/docs/props#closeoptions) | No | Options for closing |

## modals.closeAll

Closes all modals. Returns `true` if any modal was closed. Modals using `useBeforeClose` that return `false` will remain open unless `force` is used.

```tsx
import { useModals } from "react-easy-modals";

const modals = useModals();

// Close all modals (respects useBeforeClose)
modals.closeAll();

// Force close all, ignoring useBeforeClose callbacks
modals.closeAll({ force: true });
```

| Param | Type | Required | Description |
| --- | --- | --- | --- |
| `options` | [`CloseOptions`](/docs/props#closeoptions) | No | Options for closing |

## modals.stack

An array of the current stack of modals.

```tsx
import { useModals } from "react-easy-modals";

const modals = useModals();

console.log(modals.stack); // Array of current modals
```

## modals.action

The last action that was performed on the modals stack (`push`, `pop` or `replace`). This can be useful for animations.

```tsx
import { useModals } from "react-easy-modals";

const modals = useModals();

console.log(modals.action); // 'push' | 'pop' | 'replace' | undefined
```

<Callout type="info">
  The `modals.open` method returns a promise that resolves when the modal is
  closed. Modals can return a value by using their [close()
  prop](/docs/how-it-works#the-close-prop).
</Callout>

<Callout type="info">
  When closing modals programmatically via `closeById`, `close(n)` or
  `closeAll`, their `modals.open` promises resolve with `undefined`. If you need
  to resolve with a custom value, use the [close()
  prop](/docs/how-it-works#the-close-prop) from inside the modal, or
  [`useModalAt`](/docs/use-modal-at) to call `close(value)` on a parent from a
  nested child.
</Callout>