All Projects → mpontus → React Modal Hook

mpontus / React Modal Hook

Syntactic sugar for handling modals using React Hooks

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Modal Hook

Popmodal
jquery plugin for showing tooltips, titles, modal dialogs and etc
Stars: ✭ 149 (-14.37%)
Mutual labels:  dialog, modal
Dialog
👻 A simple to use, highly customizable, and powerful modal for Angular Applications
Stars: ✭ 158 (-9.2%)
Mutual labels:  dialog, modal
Launchy
Launchy: An Accessible Modal Window
Stars: ✭ 89 (-48.85%)
Mutual labels:  dialog, modal
React Poppop
A mobile support and multi-directional modal for ReactJS
Stars: ✭ 78 (-55.17%)
Mutual labels:  dialog, modal
Djsemimodalviewcontroller
Simple semi modal presentation dialog with stacked content
Stars: ✭ 137 (-21.26%)
Mutual labels:  dialog, modal
Ng Popups
🎉 Alert, confirm and prompt dialogs for Angular. Simple as that.
Stars: ✭ 80 (-54.02%)
Mutual labels:  dialog, modal
React Native Push Notification Popup
A <NotificationPopup/> component for presenting your own push notification in react-native app
Stars: ✭ 111 (-36.21%)
Mutual labels:  dialog, modal
Rodal
A React modal with animations.
Stars: ✭ 754 (+333.33%)
Mutual labels:  dialog, modal
Svelte Simple Modal
A simple, small, and content-agnostic modal for Svelte v3
Stars: ✭ 130 (-25.29%)
Mutual labels:  dialog, modal
Vue Final Modal
🍕Vue Final Modal is a tiny, renderless, mobile-friendly, feature-rich modal component for Vue.js.
Stars: ✭ 128 (-26.44%)
Mutual labels:  dialog, modal
React Native Alert Pro
The Pro Version of React Native Alert (Android & iOS)
Stars: ✭ 69 (-60.34%)
Mutual labels:  dialog, modal
Ax5ui Kernel
Javascript UI Framework - AX5UI - Kernel Module
Stars: ✭ 164 (-5.75%)
Mutual labels:  dialog, modal
Ng2 Bootstrap Modal
Library to simplify the work with bootstrap modal dialogs
Stars: ✭ 53 (-69.54%)
Mutual labels:  dialog, modal
Jbox
jBox is a jQuery plugin that makes it easy to create customizable tooltips, modal windows, image galleries and more.
Stars: ✭ 1,251 (+618.97%)
Mutual labels:  dialog, modal
Bootstrap Show Modal
A Bootstrap 4 / jQuery plugin wrapper, to create modals dynamically in JavaScript
Stars: ✭ 38 (-78.16%)
Mutual labels:  dialog, modal
Customalertviewdialogue
Custom AlertView Dialogue is the world's most advanced alert view library. Custom AlertView Dialogue includes simple message popups, confirmation alerts, selector popups, action sheet bottom menus, and input/feedback contact forms.
Stars: ✭ 100 (-42.53%)
Mutual labels:  dialog, modal
React Useportal
🌀 React hook for Portals
Stars: ✭ 698 (+301.15%)
Mutual labels:  dialog, modal
Simplelightbox
Touch-friendly image lightbox for mobile and desktop
Stars: ✭ 744 (+327.59%)
Mutual labels:  dialog, modal
A11y Dialog
A very lightweight and flexible accessible modal dialog script.
Stars: ✭ 1,768 (+916.09%)
Mutual labels:  dialog, modal
Bdialog
Extend the Bootstrap Modal features, making dialog more functions and easier to use, dialog type including modal, alert, mask and toast types
Stars: ✭ 174 (+0%)
Mutual labels:  dialog, modal

react-modal-hook

Syntactic sugar for handling modal windows using React Hooks.

This library does not provide any UI, but instead offers a convenient way to render modal components defined elsewhere.

For a simple modal component check out react-modal, which works well with this library.

Demo (Material-UI)

Table of Contents

Install

npm install --save react-modal-hook

Usage

Use ModalProvider to provide modal context for your application:

import React from "react";
import ReactDOM from "react-dom";
import { ModalProvider } from "react-modal-hook";
import App from "./App";

ReactDOM.render(
  <ModalProvider>
    <App />
  </ModalProvider>,
  document.getElementById("root")
);

Call useModal with the dialog component of your choice. Example using react-modal:

import React from "react";
import ReactModal from "react-modal";
import { useModal } from "react-modal-hook";

const App = () => {
  const [showModal, hideModal] = useModal(() => (
    <ReactModal isOpen>
      <p>Modal content</p>
      <button onClick={hideModal}>Hide modal</button>
    </ReactModal>
  ));

  return <button onClick={showModal}>Show modal</button>;
};

Updating Modals

Second argument to useModals should contain an array of values referenced inside the modal:

const App = () => {
  const [count, setCount] = useState(0);
  const [showModal] = useModal(
    () => (
      <ReactModal isOpen>
        <span>The count is {count}</span>
        <button onClick={() => setCount(count + 1)}>Increment</button>
      </ReactModal>
    ),
    [count]
  );

  return <button onClick={showModal}>Show modal</button>;
};

Modals are also functional components and can use react hooks themselves:

const App = () => {
  const [showModal] = useModal(() => {
    const [count, setCount] = useState(0);

    return (
      <ReactModal isOpen>
        <span>The count is {count}</span>
        <button onClick={() => setCount(count + 1)}>Increment</button>
      </ReactModal>
    );
  });

  return <button onClick={showModal}>Show modal</button>;
};

Animated Modals

Use TransitionGroup as the container for the modals:

import React from "react";
import ReactDOM from "react-dom";
import { ModalProvider } from "react-modal-hook";
import { TransitionGroup } from "react-transition-group";
import App from "./App";

ReactDOM.render(
  <ModalProvider rootComponent={TransitionGroup}>
    <App />
  </ModalProvider>,
  document.getElementById("root")
);

When TransitionGroup detects of one of its children removed, it will set its in prop to false and wait for onExited callback to be called before removing it from the DOM.

Those props are automatically added to all components passed to useModal. You can can pass them down to CSSTransition or modal component with transition support.

Here's an example using Material-UI's Dialog:

import React from "react";
import { useModal } from "react-modal-hook";
import { Button, Dialog, DialogActions, DialogTitle } from "@material-ui/core";

const App = () => {
  const [showModal, hideModal] = useModal(({ in: open, onExited }) => (
    <Dialog open={open} onExited={onExited} onClose={hideModal}>
      <DialogTitle>Dialog Content</DialogTitle>
      <DialogActions>
        <Button onClick={hideModal}>Close</Button>
      </DialogActions>
    </Dialog>
  ));

  return <Button onClick={showModal}>Show modal</Button>;
};

License

MIT © mpontus

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].