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

@kamo88/react-dialog-hooks

v0.9.6

Published

react use dialog hooks & dialog component

Downloads

70

Readme

license npm (scoped) codecov pages-build-deployment

npm i @kamo88/react-dialog-hooks

or

Please choose your @version

<script src="https://unpkg.com/@kamo88/[email protected]/dist/umd/index.js" crossorigin></script>
<!doctype html>
<html>
  <head>
    <script
      src="https://unpkg.com/react@18/umd/react.development.js"
      crossorigin
    ></script>
    <script
      src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"
      crossorigin
    ></script>
    <script
      src="https://unpkg.com/@kamo88/[email protected]/dist/umd/index.js"
      crossorigin
    ></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>

    <script type="text/babel">
      function Hello() {
        const { ref, isOpen, showDialog, closeDialog } =
          Kamo88Dialog.useDialog();
        return (
          <div>
            <button type="button" onClick={showDialog}>
              showDialog
            </button>
            <Kamo88Dialog.Dialog
              ref={ref}
              isOpen={isOpen}
              onClickAway={closeDialog}
            >
              <div>
                <div>header</div>
                <div>Hello World!</div>
                <div>
                  <button type="button" onClick={closeDialog}>
                    closeDialog main
                  </button>
                  <button type="button" onClick={closeDialog}>
                    closeDialog sub
                  </button>
                </div>
              </div>
            </Kamo88Dialog.Dialog>
          </div>
        );
      }

      const container = document.getElementById('root');
      const root = ReactDOM.createRoot(container);
      root.render(<Hello />);
    </script>
  </body>
</html>

@kamo88/react-dialog-hooks

This React component & hooks are to be displayed using the <dialog> tag.

storybook

Description

  1. Hooks for using the dialog tag.
  2. Using body-scroll-lock to stop the body scrolling.
  3. Dialog Component loops focus to a focusable element in the content using focus-trap-react.
  4. Dialog Component can be styled using className. ex) tailwindcss , css modules (@emotion/react is also available)

Usage

useDialog

Normal usage.

Of course, you can also create markup using regular dialog tags.

import { useDialog } from '@kamo88/react-dialog-hooks';

const UseDialogExample = () => {
  const { ref, isOpen, showDialog, closeDialog } = useDialog();

  return (
    <div>
      <button type="button" onClick={showDialog}>
        showDialog
      </button>
      <dialog role="presentation" ref={ref} onClick={closeDialog}>
        <div role="presentation" onClick={(e) => e.stopPropagation()}>
          <div>header</div>
          <div>main</div>
          <div>
            footer
            <button type="button" onClick={closeDialog}>
              closeDialog
            </button>
          </div>
        </div>
      </dialog>
    </div>
  );
};

useDialogPromise

The use of showDialog's return is Promise. Wait for user operation in a Promise and handle it with its return ('main', 'sub', 'abort' type string). ※ The same behavior can be implemented in useDialog, even though Promise cannot be used.

import { useCallback } from 'react';
import { useDialogPromise, DialogResponse } from '@kamo88/react-dialog-hooks';

const UseDialogPromiseExample = () => {
  const {
    ref,
    isOpen,
    showDialog,
    closeDialogMain,
    closeDialogSub,
    closeDialogAbort,
  } = useDialogPromise();

  const handleShowDialog = useCallback(async () => {
    const dialogRes = await showDialog();
    if (dialogRes === DialogResponse.main) {
      // main processing ex) primary button`s action
      return;
    }

    if (dialogRes === DialogResponse.sub) {
      // sub processing ex) secondary button`s action
      return;
    }

    if (dialogRes === DialogResponse.abort) {
      // abort processing ex) click away`s action & This Component`s unmount
    }
  }, [showDialog]);

  return (
    <div>
      <button type="button" onClick={handleShowDialog}>
        showDialog
      </button>
      <dialog role="presentation" ref={ref} onClick={closeDialogAbort}>
        <div role="presentation" onClick={(e) => e.stopPropagation()}>
          <div>header</div>
          <div>main</div>
          <div>
            footer
            <button type="button" onClick={closeDialogMain}>
              closeDialogMain
            </button>
            <button type="button" onClick={closeDialogSub}>
              closeDialogSub
            </button>
          </div>
        </div>
      </dialog>
    </div>
  );
};

Dialog

import { Dialog, useDialog } from '@kamo88/react-dialog-hooks';

const DialogComponentExample = () => {
  const { ref, isOpen, showDialog, closeDialog } = useDialog();

  return (
    <div>
      <button type="button" onClick={showDialog}>
        showDialog
      </button>
      <Dialog
        className="backdrop:bg-gray-900 backdrop:opacity-80"
        ref={ref}
        isOpen={isOpen}
        className="dialogClass"
        shouldFocusTrap
        initialFocus={false}
        onClickAway={closeDialog}
      >
        <div>
          <div>header</div>
          <div>main</div>
          <div>
            footer
            <button type="button" onClick={closeDialog}>
              closeDialog main
            </button>
          </div>
        </div>
      </Dialog>
    </div>
  );
};

Dialog Component Props

| key | type | required | default | description | | --------------- | ------------------------------------ | -------- | --------- | --------------------------------------------------------------------------------------------------------- | | ref | React.RefObject<HTMLDialogElement> | ⭕ | | dialog ref | | children | ReactNode | ⭕ | | dialog contents.Basically, include elements that can focus. | | isOpen | boolean | ⭕ | | dialog open state | | shouldFocusTrap | boolean | | true | | | initialFocus | undefined or false | | undefined | This is based on the focus-trap-react property. | | onClickAway | () => void | | | Event when backdrop in Dialog is clicked.This is an alternative to the click event in the dialog tag. |

& <dialog> element`s attributes