npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-hooks-use-modal

v3.3.1

Published

A react hook which can open the modal with react-portal

Downloads

5,843

Readme

useModal

This is a react hook which can open the modal easily.

Usage

import React, { useState, useCallback } from 'react';
import { createRoot } from 'react-dom/client';
import { useModal } from 'react-hooks-use-modal';

const App = () => {
  const [Modal, open, close, isOpen] = useModal('root', {
    preventScroll: true,
    focusTrapOptions: {
      clickOutsideDeactivates: false,
    },
  });
  return (
    <div>
      <p>Modal is Open? {isOpen ? 'Yes' : 'No'}</p>
      <button onClick={open}>OPEN</button>
      <Modal>
        <div>
          <h1>Title</h1>
          <p>This is a customizable modal.</p>
          <button onClick={close}>CLOSE</button>
        </div>
      </Modal>
    </div>
  );
};
const root = createRoot(document.getElementById('root'));
root.render(<App />);

Syntax

[ModalComponent, openFunc, closeFunc, isOpenBool] = useModal(domNode?, { initialValue?, preventScroll?, focusTrapOptions?, components? })

ModalComponent Type: React.FC<{ title?: React.ReactNode; description?: React.ReactNode, children?: React.ReactNode, additionalProps?: Record<string, unknown> }> Modal component that displays children in the screen center. If you specify title and description, additionalProps you must implement custom components with the components option's Modal property and render in them. See EXAMPLE below for details. https://github.com/microcmsio/react-hooks-use-modal/blob/master/examples/src/js/components-option/index.tsx

openFunc A function to open modal.

closeFunc A function to close modal.

isOpenBool A boolean to know the state whether modal is open or not.

domNode Optional. Default value is 'root'. Modal component uses React-Portal. You can specify the output destination domNode with this argument

initialValue Optional. Default value is false. This is useful when you want to mount the modal in an open position.

preventScroll Optional to prevent scrolling while modal is open. Default value is false.

focusTrapOptions Override the focus-trap options used internally. For example, to prevent a modal from closing when a non-modal element is clicked, do the following

useModal('root', {
  focusTrapOptions: {
    clickOutsideDeactivates: false,
  },
});

components Optional. This is an option to customize the ModalWrapper returned by useModal. Use as follows

useModal('root', {
  components: {
    Modal: ({ title, description, children }) => {
      return (
        <div
          style={{
            padding: '60px 100px',
            backgroundColor: '#fff',
            borderRadius: '10px',
          }}
        >
          {title && <h1>{title}</h1>}
          {description && <p>{description}</p>}
          {children}
        </div>
      );
    },
    Overlay: () => {
      return (
        <div
          style={{
            position: 'fixed',
            top: 0,
            left: 0,
            bottom: 0,
            right: 0,
            backgroundColor: 'rgba(0, 0, 0, 0.5)',
          }}
        />
      );
    },
    Wrapper: ({ children }) => {
      return (
        <div
          style={{
            position: 'fixed',
            top: 0,
            left: 0,
            bottom: 0,
            right: 0,
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center',
            zIndex: 1000,
          }}
        >
          {children}
        </div>
      );
    },
  },
});

Combined with ModalProvider (described below), you can specify the default style for all useModal in the project.

Global Settings

The ModalProvider component allows you to apply a common default configuration to all useModal hooks.

<ModalProvider {...options}>
  <Component />
</ModalProvider>

The following example sets all useModal hooks to not scroll outside the modal by default.

const Component1 = () => {
  const [Modal] = useModal('root');
  return (
    <Modal>
      <h2>Common</h2>
    </Modal>
  );
};
const Component2 = () => {
  const [Modal] = useModal('root', { preventScroll: false }); // override
  return (
    <Modal>
      <h2>Override options</h2>
    </Modal>
  );
};

const App = () => {
  return (
    <ModalProvider preventScroll>
      <Component1 />
      <Component2 />
    </ModalProvider>
  );
};

Demo

https://microcmsio.github.io/react-hooks-use-modal/

How To Develop

Setup

$ yarn

Build src

$ yarn build

Watch src

$ yarn watch

Start demo

$ yarn start:demo

Then access it on http://localhost:3001/react-hooks-use-modal

License

MIT