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

@catconsult/simplify-modal-control

v1.1.1

Published

Simplify React modal control flow

Downloads

139

Readme

npm version

simplify-modal-control

Like the name suggested, this library is created to simplify modal control flow for React. Note, this library is for controlling modals, you still have to pick your favorite modal component as the wrapper.

Features

  • Handle open and close modal
  • Pass props into modal content
  • Declare reusable modals and access it anywhere
  • Prevent data flickering during modal transition
  • Prevent re-rendering when modal is in close state
  • Super customizable, not tied to any modal components
  • ZERO dependencies beside React v16.8.0 or above
  • Extremely lightweight, just ~10 KB in package size
  • Written in TypeScript, every pieces are well Typed!

Problems Addressed

In React world, normally when you want to use a modal, you'll have to go through the process of declaring a openModal state, creating open and close handlers, and finally returning the modal and content component in the component block. This process is tedious and cause several issues:

  • Lots of boilerplate codes
  • Hard to reuse and pass around props into the modal
  • Introduce extra re-rendering caused by the parent component
  • Modal component continue to trigger re-rendering in close state
  • Modal content flickering during close transition caused by change modal state

This library addressed all the above issues by introduce a simpler modal control flow.

Quick Installation

yarn add @catconsult/simplify-modal-control

or

npm i @catconsult/simplify-modal-control

Quick Usage

// use MUI Dialog as Example, choose you favorite component wrapper
import { Dialog, DialogContent } from '@mui/material';
import makeModalControlSimpler from '@catconsult/simplify-modal-control';

// define your modal
const MyModal = ({
  onClose,
  customMessage,
}: DefaultModalProps & {
  customMessage: string;
}) => {
  return (
    <>
      <DialogContent>
        <span>Modal Content: {customMessage}</span>
        <button onClick={onClose}>Close</button>
      </DialogContent>
    </>
  );
};

// construct modal controls
const {
  useModal,
  useModalContextSelector,
  useModalContext,
  ModalContextProvider,
  openModal,
  closeModal,
} = makeModalControlSimpler({
  ModalWrapper: Dialog,
  // pass props to modal wrapper
  mapModalProps: ({ open, handleClose }, modalProps) => ({
    ...modalProps,
    open,
    onClose: handleClose,
  }),
  // define your global modals
  useContextModals: useModal => ({
    myModal0: useModal(MyModal),
    myModal1: useModal(MyModal),
    myModal2: useModal(MyModal, {
      modalProps: {
        // pass props to modal wrapper
        maxWidth: 'xs',
      },
    }),
    myModal3: useModal(MyModal, {
      // pass default props to modal content
      defaultProps: { customMessage: 'something' },
    }),
  }),
});

// three ways to consume modal
const MyConsumer = () => {
  // select single from global context
  const [handleOpenMyModal1, handleCloseMyModal1] =
    useModalContextSelector('myModal1');

  // select multiple from global context
  const {
    myModal2: [handleOpenMyModal2],
    myModal3: [handleOpenMyModal3],
  } = useModalContext();

  // use modal within component scope
  const [MyModal4Node, handleOpenMyModal4] = useModal(MyModal);

  return (
    <div>
      {MyModal4Node}
      <button
        onClick={() => {
          openModal('modal0', { customMessage: 'modal0' });
          handleOpenMyModal1({ customMessage: 'modal1' }, { maxWidth: 'lg' });
          handleOpenMyModal2({ customMessage: 'modal2' });
          handleOpenMyModal3({ customMessage: 'modal3' });
          handleOpenMyModal4({ customMessage: 'modal4' });
        }}>
        Open
      </button>
    </div>
  );
};

export const App = () => {
  return (
    <ModalContextProvider>
      <MyConsumer />
    </ModalContextProvider>
  );
};

Demo

Documentations

makeModalControlSimpler

/**
 * Make modal control simpler!
 * @param options.ModalWrapper the modal wrapper component function
 * @param options.mapModalProps function that return modal wrapper props
 * @param options.onOpen function trigger when open modal
 * @param options.onClose function trigger when close modal
 * @param options.useHookValue custom hooks to return any hook value to be consumed by mapModalProps. Similar purpose as customValues
 * @param options.useContextModals return modals to be use in modal context provider
 *
 * @returns { { useModal, useModalContext, useModalContextSelector, ModalContextProvider } }
 */
makeModalControlSimpler(options);

openModal

/**
 * Open modal without using hooks
 * @param key modal key
 * @param newProps modal content props
 * @param newModalProps modal props
 * @example
 * ```ts
 * openModal("fooModal", {fooProp:"pass in props to the modal"}, { maxWidth:"lg" });
 * ```
 */
openModal(key, newProps, newModalProps);

closeModal

/**
 * Close modal without using hooks
 * @param key modal key
 * @example
 * ```ts
 * closeModal("fooModal");
 * ```
 */
closeModal(key);

useModal

/**
 * @param ModalContent modal content component function
 * @param options
 * @param options.defaultProps props to feed modal content
 * @param options.modalProps modal props
 * @param options.customValues custom values to be consumed by mapModalProps. Similar purpose as useHookValue
 * @returns { [ModalNode, handleOpen, handleClose] }
 */
useModal(MyModal, UseModalOptions);

useModalContext

/**
 *  Select all modal handlers from global modal context
 *  @returns { { [modalKey]: [handleOpen, handleClose] } }
 */
useModalContext();

useModalContextSelector

/**
 *  Select a single modal open and close handler from global modal context
 *  @param key modal key
 *  @returns { [handleOpen, handleClose] }
 */
useModalContextSelector(key);

Local Development

Prerequisites

  • NodeJS and NPM
  • Yarn
  • Editor/IDE that support TypeScript

Quick Start

install dependencies using yarn

yarn

run test watch

yarn test

test build

yarn build