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

overlay-manager-rc

v0.5.0

Published

React overlay component manager

Downloads

52

Readme

Overlay-manager-rc

Inspired by angular cdk overlay

React overlay component manager

Feature

  • (alert-dialog, dialog, sheet...) open, close state no more management.
  • You don't need to worry about declaring overlay component.
  • It's okay to have multiple overlay.
  • Delivering data to overlay component Props.
  • Detect when an overlay component is closed.
    • The resulting data is received on close.

Install

npm

npm install overlay-manager-rc

yarn

yarn add overlay-manager-rc

pnpm

pnpm add overlay-manager-rc

Setting

ex) nextjs(app router) + shadcn-ui(radix-ui)

already install

  • alert-dialog
  • dialog
  • sheet

Step1

make file OverlayManagerProvider.tsx;

'use client';

import { useOverlayRegister } from 'overlay-manager-rc';
import type { ReactNode } from 'react';

export function OverlayManagerProvider({ children }: { children: ReactNode }) {
  const { OverlayProvider, overlays } = useOverlayRegister();

    return (
        <OverlayProvider>
            {children}
             {/* ---step2 ---*/}
        </OverlayProvider>
    );
}

Step2

   {overlays.map((contentRender) => {
                const { close, content: ContentComponent, data } = contentRender;

                if (typeof ContentComponent !== 'function') {
                    return null;
                }
               /*----case 1----*/
  
                return <ContentComponent
                        close={contentRender.close}
                        data={contentRender.data}
                        id={contentRender.id}
                        key={contentRender.id}
                        open={contentRender.state}
                />
                
               /*----case 2----*/

/*
                return (
                    <AlertDialog
                        key={contentRender.id}
                        onOpenChange={(v) => {
                            !v && close();
                        }}
                        open={contentRender.state}
                    >
                        <AlertDialogContent>
                            <AlertDialogHeader>
                                <AlertDialogTitle>{contentRender.title}</AlertDialogTitle>
                                <AlertDialogDescription>
                                    <ContentComponent
                                      close={contentRender.close}
                                      data={contentRender.data}
                                      id={contentRender.id}
                                      open={contentRender.state}
                                    />
                                </AlertDialogDescription>
                            </AlertDialogHeader>
                            <AlertDialogFooter>
                                <AlertDialogCancel>Cancel</AlertDialogCancel>
                                <AlertDialogAction>Continue</AlertDialogAction>
                            </AlertDialogFooter>
                        </AlertDialogContent>
                    </AlertDialog>
                );
*/

               
            })}

Step3

set provider in Rootlayoutcomponent

  export default function RootLayout({ children }: { children: ReactNode }) {
  return (
          <html lang="en" suppressHydrationWarning>
            <body className={'min-h-screen font-sans antialiased dark'}>
                <OverlayManagerProvider>{children}</OverlayManagerProvider>
            </body>
          </html>
  );
}

Usage

Create overlay component

import type { OverlayContentProps } from 'overlay-manager-rc';

export function OverlayContentComponent({ data, close, open }: OverlayContentProps<string>) {
  /*----*case1-----*/
    return (<AlertDialog
                    open={open}
                    onOpenChange={(v) => {
                      !v && close();
                    }}
            >
              <AlertDialogContent>
                <AlertDialogHeader>
                  <AlertDialogTitle>Alert title</AlertDialogTitle>
                  <AlertDialogDescription>Get Data: {data}</AlertDialogDescription>
                </AlertDialogHeader>
                <AlertDialogFooter>
                  <AlertDialogCancel>Cancel</AlertDialogCancel>
                  <AlertDialogAction>Continue</AlertDialogAction>
                </AlertDialogFooter>
              </AlertDialogContent>
            </AlertDialog>
    );
    
    /*----*case2-----*/
   /*
    return (<div>Get Data: {data}</div>)
    */
}

you can arrow function component

import type { OverlayContentComponent } from 'overlay-manager-rc';

export const OverlayContentComponent:OverlayContentComponent<string> = ({ data, close })  => {
  /* ----------------------- */
} 

Open overlay

'use client';

import { useOverlayManager } from 'overlay-manager-rc';

export function OverlaySection() {
  const { overlayOpen } = useOverlayManager();


  const handleOpenAlert = () => {
    overlayOpen({
      content: OverlayContentComponent,
      data: 'Input Data',
      close: () => {
          // close logic
      }
    });
  };
  return (
          <section className="md:h-screen">
            <div className="flex flex-col gap-10">
              <Button onClick={handleOpenAlert}>
                show alert
              </Button>
            </div>
          </section>
  );
}

Receive resulting data when closing

export function OverlayContentComponent({data, close}: OverlayContentProps<string, boolean>) {

  return (<div>Get Data: {data}
    <button onClick={() => {close(false);}}>close</button>
  </div>)
}

/* open handler*/
const handleOpenAlert = () => {
  overlayOpen({
    content: OverlayContentComponent,
    data: 'Input Data',
    close: (result) => {
      console.log(result);
    }
  });
}

API

useOverlayManager

returns

| name | description | parameter | |-----------------|-------------------------------|-------------------| | overlayOpen | open overlay component | OverlayOpenOption | | closeAllOverlay | close all overlay component | - |

OverlayOpenOption

| Prop | Type | Default | Required | |----------------|----------------------------------------------|---------|------------| | content | OverlayContentComponent<T, R> | - | Yes | | data | T | - | | | close | OverlayCloseType | - | | | title | ReactNode | - | | | width | CSSProperties['width'] | - | | | height | CSSProperties['height'] | - | | | style | CSSProperties | - | | | className | string | - | | | position | OverlayPositionType | - | | | overlayHidden | boolean | false | | | kind | 'overlay' | 'sheet' | 'modal' | 'confirm' | - | |

OverlayContentProps<T, R>

| Prop | Type | Default | Required | | ----- | -------------------- | ------- |----------| | data | T | - | Yes | | close | OverlayCloseType | - | Yes | | id | OverlayId | - | - | | open | boolean | - | Yes |