Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mattjennings/react-modal-stack

A simple, flexible, zero-dependency modal stack manager for React.
https://github.com/mattjennings/react-modal-stack

Last synced: about 8 hours ago
JSON representation

A simple, flexible, zero-dependency modal stack manager for React.

Awesome Lists containing this project

README

        

# react-modal-stack

A simple, flexible, zero-dependency modal stack manager for React.

[Live Demo](https://mattjennings.github.io/react-modal-stack/)

![GIF Preview](https://media.giphy.com/media/cnVra4LzHe1LWN5qpq/giphy.gif)

## Install

```
npm install @mattjennings/react-modal-stack
```

## Usage

Add `` to the root of your App

```jsx
import React from 'react'
import { ModalStack } from '@mattjennings/react-modal-stack'
import App from './App'

React.render(


,
document.querySelector('#root')
)
```

Create a Modal component. It doesn't matter how you style it, it just needs to receive an `open` prop.

```jsx
import React from 'react'
import { useModals } from '@mattjennings/react-modal-stack'

function Modal({ open, title, message }) {
const { closeModal } = useModals()

if (!open) {
return null
}

return (



{title}


{message}


{
closeModal()
}}
>
Close



)
}
```

Open the modal

```jsx
import React from 'react'
import { useModals } from '@mattjennings/react-modal-stack'
import Modal from './Modal'

function OpenModal() {
const { openModal } = useModals()

return (
{
openModal(Modal, {
title: 'Hello',
message: 'This is your modal',
})
}}
>
Open Modal

)
}
```

## Animating

`ModalStack` provides a `renderModals` prop to take control of rendering the modals. This lets you use a library like [framer-motion](https://github.com/framer/motion) to animate your transitions between modals.

See the [framer-motion example](/stories/Animated.stories.tsx) to see how it works.

## API

#### ``

| Props | Type | Description |
| ------------ | ------------------------------ | ------------------------------------------------------------------------------------------------------- |
| renderModals | ({ stack }) => React.ReactNode | If you want to take control on how the modals are rendered (eg. adding animations), you can do so here. |

#### `useModals()`

| Method | Description |
| -------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| openModal(component, props, options?: { replace }) | Opens the component provided as the modal.

If `replace: true` is provided for `options`, it will dismiss & replace the currently displayed modal in the stack. |
| closeModal | Dismisses the current modal |
| closeModals(amount) | Dismisses the amount of modals off the stack |
| closeAllModals() | Dismisses all modals |
| stack | The current stack of modals |