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-hook-modal-pure

v1.0.9

Published

React Modal Provider is a simple and powerful package that allows you to add modal functionality to your React applications with ease. It uses the React context API to manage the modal state and provides a set of hooks for triggering and controlling modal

Downloads

18

Readme

React Modal Provider

React Modal Provider is a simple and powerful package that allows you to add modal functionality to your React applications with ease. It uses the React context API to manage the modal state and provides a set of hooks for triggering and controlling modals.

Installation

Install the package using npm or yarn:

npm install react-hook-modal-pure

or

yarn add react-hook-modal-pure

Usage

  1. To use the package, you need to wrap your application in the ModalProvider component. This component provides the modal context to all child components.
import { ModalProvider } from 'react-modal-provider';
import App from './App';

ReactDOM.render(
  <React.StrictMode>
    <ModalProvider>
      <App />
    </ModalProvider>
  </React.StrictMode>,
  document.getElementById('root'),
);
  1. Use the useModalTrigger hook in a component to create a trigger button: This hook provides the necessary props for a button that will trigger the modal.
import { useModalTrigger } from 'react-modal-provider';

function MyComponent() {
  const { isModalVisible, useModalParams, buttonProps } = useModalTrigger();

  return (
    <>
      <button {...buttonProps}>Open Modal</button>
      {isModalVisible && <MyModal {...useModalParams} />}
    </>
  );
}
  1. Use the useModal hook in a modal component to manage the modal's visibility and position:

This hook provides a ref for the modal and a style object that positions the modal next to the trigger button. It also automatically closes the modal when the user clicks outside of it or presses the Escape key.

import { useModal } from 'react-modal-provider';

function MyModal({ closeModal }: { closeModal: () => void }) {
  const { style, ref } = useModal({ closeModal });

  return (
    <div ref={ref} style={style}>
      <h1>Hello, I'm a modal!</h1>
    </div>
  );
}

API

ModalProvider

  • The ModalProvider component wraps your application and provides the modal context to all child components.

Props

  • children: The child elements of the provider.

useModalTrigger

The useModalTrigger hook provides the necessary props for a button that will trigger the modal.

Returns

  • isModalVisible: A boolean indicating whether the modal is visible or not.
  • useModalParams: An object containing the buttonRef and closeModal function to be passed to useModal.
  • buttonProps: An object containing the necessary onClick and ref props for the trigger button.

useModal

The useModal hook manages the modal's visibility and position based on the provided buttonRef and closeModal function.

Arguments

  • buttonRef: A RefObject<HTMLElement> that points to the button that triggers the modal.
  • closeModal: A function that closes the modal.

Returns

  • style: An object with the CSS styles to position the modal correctly.
  • ref: A RefObject<HTMLDivElement> that should be attached to the modal's outermost div.

Example

Here's a simple example demonstrating how to use react-modal-provider:

import { useModalTrigger } from 'react-modal-provider';

function ComponentUsingModal() {
  const { isModalVisible, useModalParams, buttonProps } = useModalTrigger();

  return (
    <div>
      <button {...buttonProps}>Open Modal</button>
      {isModalVisible && <Modal useModalParams={useModalParams} />}
    </div>
  );
}

export default MyComponent;
import { useModal } from 'react-modal-provider';

function Modal(props) {
  const modalProps = useModal(props);

  return (
    <StyledModal {...modalProps}>
      <h1>Modal Content</h1>
    </StyledModal>
  );
}

export default Modal;

In this example, a button is created with the buttonProps from useModalTrigger, and when clicked, it will open a modal. The modal is managed by the useModal hook, which controls its visibility and positioning.