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-attached-modal-portal

v1.0.2

Published

React modal which attaches to selected parent

Downloads

11

Readme

react-attached-modal-portal

React modal which attaches to selected parent. It will automatically calculate is it enough space under the button and place the modal below or above the button.

NPM version npm-typescript License

Installation:

npm install react-attached-modal-portal --save

or

yarn add react-attached-modal-portal

React Modal With Scroll And Mobile View

Usage:

For correct position work of AttachedModal you need to pass your button and ref:

import React, { useRef, useState } from 'react';

import { AttachedModal } from 'react-attached-modal-portal';

const App = () => {
  const [selected, setSelected] = useState(5);
  const [isOpen, setIsOpen] = useState(false);
  const ref = useRef<HTMLButtonElement>(null);

  return (
    <AttachedModal
      isOpen={isOpen}
      buttonRect={ref.current?.getBoundingClientRect()}
      setIsOpen={setIsOpen}
      withScroll
      OpenModalBtn={
        <button ref={ref} onClick={() => setIsOpen(true)}>
          Open
        </button>
      }
      Header={
        <div style={{ display: 'flex', justifyContent: 'space-between', padding: 10 }}>
          <span style={{ fontWeight: 'bold' }}>Header</span>
          <button onClick={() => setIsOpen(false)}>X</button>
        </div>
      }
      Footer={
        <div style={{ padding: 10 }}>
          <button autoFocus onClick={() => setSelected(0)}>
            Clear
          </button>
        </div>
      }
    >
      <div style={{ padding: 10, overflow: 'hidden' }}>
        {[...new Array(selected)].map((_value, index) => (
          <div key={index}>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry
            standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to
            make a type specimen book. It has survived not only five centuries, but also the leap into electronic
            typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset
            sheets containing Lorem Ipsum passages, and more recently with desktop publishi
          </div>
        ))}
      </div>
    </AttachedModal>
  );
};

Other options such as Header, withScroll etc are optional. If you pass withScroll it will add scroll to the body of the modal and will save the position of scroll after closing modal.

Attached modal Customized

Usage:

If you want to customize styles based on AttachedModal properties you can use useAttachedModalContext: For correct using you need to add AttachedModalContextProvider above the AttachedModal component.

import React, { useRef, useState } from 'react';

import { AttachedModal, useAttachedModalContext, AttachedModalContextProvider } from 'react-attached-modal-portal';

const App = () => {
  const [selected, setSelected] = useState(5);
  const [isOpen, setIsOpen] = useState(false);

  const ref = useRef<HTMLButtonElement>(null);

  const { isMobile, canModalFitBelowButton } = useAttachedModalContext();

  const desktopBackground = canModalFitBelowButton ? 'green' : 'orange';

  return (
    <AttachedModal
      isOpen={isOpen}
      buttonRect={ref.current?.getBoundingClientRect()}
      setIsOpen={setIsOpen}
      withScroll
      OpenModalBtn={
        <button ref={ref} onClick={() => setIsOpen(true)}>
          Open
        </button>
      }
      Header={
        <div style={{ display: 'flex', justifyContent: 'space-between', padding: 10 }}>
          <span style={{ fontWeight: 'bold' }}>Header</span>
          <button onClick={() => setIsOpen(false)}>X</button>
        </div>
      }
      Footer={
        <div style={{ padding: 10 }}>
          <button autoFocus onClick={() => setSelected(0)}>
            Clear
          </button>
        </div>
      }
      wrapperStyles={{
        background: isMobile ? 'purple' : desktopBackground,
        color: isMobile ? 'white' : 'black',
      }}
    >
      <div style={{ padding: 10, overflow: 'hidden' }}>
        {[...new Array(selected)].map((_value, index) => (
          <div key={index}>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry
            standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to
            make a type specimen book. It has survived not only five centuries, but also the leap into electronic
            typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset
            sheets containing Lorem Ipsum passages, and more recently with desktop publishi
          </div>
        ))}
      </div>
    </AttachedModal>
  );
};


root.render(
  <React.StrictMode>
    <AttachedModalContextProvider>
      <App />
    </AttachedModalContextProvider>
  </React.StrictMode>,
);

Other options such as Header, withScroll etc are optional. If you pass withScroll it will add scroll to the body of the modal and will save the position of scroll after closing modal.

Props AttachedModal:

| Name | Type | Description | |:------------------------|--------------------------------------------------------------------------------|:----------------------------------------------------------------------------| | Header | React.ReactNode | Header of Modal | | Footer | React.ReactNode | Footer of Modal | | isOpen | boolean | State of Modal | | setIsOpen | React.Dispatch<React.SetStateAction<boolean>> | Handler to change state of Modal | | withScroll | boolean | Prop to add scroll to the body of Modal with remembering position of scroll | | wrapperStyles | React.CSSProperties | Styles for Wrapper | | bodyStyles | React.CSSProperties | Styles for body | | buttonRect | DOMRect | Button parameters | | OpenModalBtn | React.ReactElement | Button with which you will open modal |

If you want to support

Give a ⭐️ to project if you like it!