All Projects → hiradary → Reoverlay

hiradary / Reoverlay

Licence: mit
The missing solution for managing modals in React.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Reoverlay

React Overlays
Utilities for creating robust overlay components
Stars: ✭ 809 (+789.01%)
Mutual labels:  modal, overlay
React Layer Stack
Layering system for React. Useful for popover/modals/tooltip/dnd application
Stars: ✭ 152 (+67.03%)
Mutual labels:  modal, overlay
React Native Loading Spinner Overlay
💈 React Native loading spinner overlay
Stars: ✭ 1,369 (+1404.4%)
Mutual labels:  modal, overlay
react-layer-stack
Layering system for React. Useful for popover/modals/tooltip/dnd application
Stars: ✭ 158 (+73.63%)
Mutual labels:  overlay, modal
Popupdialog
A simple, customizable popup dialog for iOS written in Swift. Replaces UIAlertController alert style.
Stars: ✭ 3,709 (+3975.82%)
Mutual labels:  modal, overlay
react-spring-bottom-sheet
Accessible ♿️, Delightful ✨, & Fast 🚀
Stars: ✭ 604 (+563.74%)
Mutual labels:  overlay, modal
toppy
Overlay library for Angular 7+
Stars: ✭ 81 (-10.99%)
Mutual labels:  overlay, modal
Jquery Popup Overlay
jQuery plugin for responsive and accessible modal windows and tooltips
Stars: ✭ 511 (+461.54%)
Mutual labels:  modal, overlay
Webannoyances
Fix and remove annoying web elements such as sticky headers, floating boxes, floating videos, dickbars, social share bars and other distracting elements.
Stars: ✭ 831 (+813.19%)
Mutual labels:  modal, overlay
Chromium os Raspberry pi
Build your Chromium OS for Raspberry Pi 3B/3B+/4B and Pi400
Stars: ✭ 1,156 (+1170.33%)
Mutual labels:  overlay
Ng Popups
🎉 Alert, confirm and prompt dialogs for Angular. Simple as that.
Stars: ✭ 80 (-12.09%)
Mutual labels:  modal
Swiftyoverlay
Show overlay and info on app components
Stars: ✭ 63 (-30.77%)
Mutual labels:  overlay
React Native Alert Pro
The Pro Version of React Native Alert (Android & iOS)
Stars: ✭ 69 (-24.18%)
Mutual labels:  modal
Kde
[MIRROR] KDE team's testing overlay
Stars: ✭ 80 (-12.09%)
Mutual labels:  overlay
Bubbles
⚡️A library for adding messenger style floating bubbles to any android application 📲
Stars: ✭ 66 (-27.47%)
Mutual labels:  overlay
Autocomplete
🔮 Fast and full-featured autocomplete library
Stars: ✭ 1,268 (+1293.41%)
Mutual labels:  modal
React Native Hole View
✂️ React-Native component to cut a touch-through holes anywhere you want. Perfect solution for tutorial overlay
Stars: ✭ 61 (-32.97%)
Mutual labels:  overlay
Jl S Unity Blend Modes
👾 Collection of Unity shaders that support blend modes like photoshop layer style (Darken, Multiply, Linear Burn, etc)
Stars: ✭ 62 (-31.87%)
Mutual labels:  overlay
Tingle
⚡ 2kB vanilla modal plugin, no dependencies and easy-to-use
Stars: ✭ 1,287 (+1314.29%)
Mutual labels:  modal
Jbox
jBox is a jQuery plugin that makes it easy to create customizable tooltips, modal windows, image galleries and more.
Stars: ✭ 1,251 (+1274.73%)
Mutual labels:  modal

Reoverlay

Size Downloads Version NPM License Twitter

The missing solution for managing modals in React.

Reoverlay

Installation 🔥

npm install reoverlay --save

# or if you prefer Yarn:
yarn add reoverlay

Demo ⭐️

You can see a couple of examples on the website.

Philosophy 🔖

There are many ways you can manage your modals in React. You can (See a relevant article):

  • Use a modal component as a wrapper (like a button component) and include it wherever you trigger the hide/show of that modal.
  • The ‘portal’ approach that takes a modal and attaches it to document.body.
  • A top level modal component that shows different contents based on some property in the store.

Each one of these has its own cons & pros. Take a look at the following example:

const HomePage = () => {
  const [isDeleteModalOpen, setDeleteModal] = useState(false)
  const [isConfirmModalOpen, setConfirmModal] = useState(false)
  
  return (
    <div>
      <Modal isOpen={isDeleteModalOpen}>
        ...
      </Modal>
      <Modal isOpen={isConfirmModalOpen}>
        ...
      </Modal>
      ...
      <button onClick={() => setDeleteModal(true)}>Show delete modal</button>
      <button onClick={() => setConfirmModal(true)}>Show confirm modal</button>
    </div>
  )
}

This is the most commonly adopted approach. However, I believe it has a few drawbacks:

  • You might find it difficult to show modals on top of each other. (aka "Stacked Modals")
  • More boilerplate code. If you were to have 3 modals in a page, you had to use Modal component three times, declare more and more variables to handle visibility, etc.
  • Unlike reoverlay, you can't manage your modals outside React scope (e.g Store). Though it's not generally a good practice to manage modals/overlays outside React scope, It comes in handy in some cases. (e.g Using axios interceptors to show modals according to network status, access control, etc.)

Reoverlay, on the other hand, offers a rather more readable and easier approach. You'll be given a top-level modal component (ModalContainer), and a few APIs to handle triggering hide/show. Check usage to see how it works.

Usage 🎯

There are two ways you can use Reoverlay.

#1 - Pass on your modals directly

App.js:

import React from 'react';
import { ModalContainer } from 'reoverlay';

const App = () => {
  return (
    <>
      ...
      <Routes />
      ...
      <ModalContainer />
    </>
  )
}

Later where you want to show your modal:

import React from 'react';
import { Reoverlay } from 'reoverlay';

import { ConfirmModal } from '../modals';

const PostPage = () => {
  
  const deletePost = () => {
    Reoverlay.showModal(ConfirmModal, {
      text: "Are you sure you want to delete this post",
      onConfirm: () => {
        axios.delete(...)
      }
    })
  }

  return (
    <div>
      <p>This is your post page</p>
      <button onClick={deletePost}>Delete this post</button>
    </div>
  )
}

Your modal file (ConfirmModal in this case):

import React from 'react';
import { ModalWrapper, Reoverlay } from 'reoverlay';

import 'reoverlay/lib/ModalWrapper.css';

const ConfirmModal = ({ confirmText, onConfirm }) => {

  const closeModal = () => {
    Reoverlay.hideModal();
  }

  return (
    <ModalWrapper>
      {confirmText}
      <button onClick={onConfirm}>Yes</button>
      <button onClick={closeModal}>No</button>
    </ModalWrapper>
  )
}

This is the simplest usage. If you don't want your modals to be passed directly to Reoverlay.showModal(myModal), you could go on with the second approach.

#2 - Pass on your modal's name

App.js:

import React from 'react';
import { Reoverlay, ModalContainer } from 'reoverlay';

import { AuthModal, DeleteModal, ConfirmModal } from '../modals';

// Here you pass your modals to Reoverlay
Reoverlay.config([
  {
    name: "AuthModal",
    component: AuthModal
  },
  {
    name: "DeleteModal",
    component: DeleteModal
  },
  {
    name: "ConfirmModal",
    component: ConfirmModal
  }
])

const App = () => {
  return (
    <>
      ...
      <Routes />
      ...
      <ModalContainer />
    </>
  )
}

Later where you want to show your modal:

import React from 'react';
import { Reoverlay } from 'reoverlay';

const PostPage = () => {
  
  const deletePost = () => {
    Reoverlay.showModal("ConfirmModal", {
      confirmText: "Are you sure you want to delete this post",
      onConfirm: () => {
        axios.delete(...)
      }
    })
  }

  return (
    <div>
      <p>This is your post page</p>
      <button onClick={deletePost}>Delete this post</button>
    </div>
  )
}

Your modal file: (ConfirmModal in this case):

import React from 'react';
import { ModalWrapper, Reoverlay } from 'reoverlay';

import 'reoverlay/lib/ModalWrapper.css';

const ConfirmModal = ({ confirmText, onConfirm }) => {

  const closeModal = () => {
    Reoverlay.hideModal();
  }

  return (
    <ModalWrapper>
      {confirmText}
      <button onClick={onConfirm}>Yes</button>
      <button onClick={closeModal}>No</button>
    </ModalWrapper>
  )
}

NOTE: Using ModalWrapper is optional. It's just a half-transparent full-screen div, with a few preset animation options. You can create and use your own ModalWrapper. In that case, you can fully customize animation, responsiveness, etc. Check the code for ModalWrapper.

Props ⚒

Reoverlay methods

config(configData)
Name Type Default Descripiton
configData Array<{ name: string, component: React.FC }> [] An array of modals along with their name.

This method must be called in the entry part of your application (e.g App.js), or basically before you attempt to show any modal. It takes an array of objects, containing data about your modals.

import { AuthModal, DeleteModal, PostModal } from '../modals';

Reoverlay.config([
  {
    name: 'AuthModal',
    component: AuthModal
  },
  {
    name: 'DeleteModal',
    component: DeleteModal
  },
  {
    name: 'PostModal',
    component: PostModal
  }
])

NOTE: If you're code-splitting your app and you don't want to import all modals in the entry part, you don't need to use this. Please refer to usage for more info.

showModal(modal, props)
Name Type Default Descripiton
modal string | React.FC null Either your modal's name (in case you've already passed that to Reoverlay.config()) or your modal component.
props object {} Optional
import { Reoverlay } from 'reoverlay';
import { MyModal } from '../modals';

const MyPage = () => {
  const showModal = () => {
    Reoverlay.showModal(MyModal);
    // or Reoverlay.showModal("MyModal")
  }

  return (
    <div>
      <button onClick={showModal}>Show modal</button>
    </div>
  )
}
hideModal(modalName)
Name Type Default Descripiton
modalName string null Optional. Specifies which modal gets hidden. By default, the last visible modal gets hidden.
import { Reoverlay, ModalWrapper } from 'reoverlay';

const MyModal = () => {
  const closeModal = () => {
    Reoverlay.hideModal();
  }

  return (
    <ModalWrapper>
      <h1>My modal content...</h1>
      <button onClick={closeModal}>Close modal</button>
    </ModalWrapper>
  )
}

const MyPage = () => {
  const showModal = () => {
    Reoverlay.showModal(MyModal);
  }

  return (
    <div>
      <button onClick={showModal}>Show modal</button>
    </div>
  )
}
hideAll()

This comes in handy when dealing with multiple modals on top of each other (aka "Stacked Modals"). With this, you can hide all modals at once.

ModalWrapper props

Name Type Default Descripiton
wrapperClassName string '' Additional CSS class for modal wrapper element.
contentContainerClassName string '' Additional CSS class for modal content container element.
animation 'fade' | 'zoom' | 'flip' | 'door' | 'rotate' | 'slideUp' | 'slideDown' | 'slideLeft' | 'slideRight' 'fade' A preset of various animations for your modal.
onClose function () => Reoverlay.hideModal() Gets called when the user clicks outside modal content.

Support ❤️

PRs are welcome! You can also buy me a coffee if you wish.

LICENSE

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].