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-use-dialog

v1.0.0

Published

Headless React dialog component

Downloads

3

Readme

React Use Dialog

A set of headless React components that ease the implementation of dialogs. Completely unstyled components that allow you to pick your own styles and only implement the basic open/close behaviour.

Example

import { DialogStack, Dialog, useDialogs } from "react-use-dialog";

enum DialogKeys {
  myDialog,
}

function App() {
  const dialogs = useDialogs<DialogKeys>();

  const openDialog = () => dialogs.open(DialogKeys.myDialog);

  return (
    <>
      <button onClick={openDialog}>Open dialog</button>
      <Dialog id={DialogKeys.myDialog}>
        <div>Dialog content</div>
        <button onClick={dialogs.closeCurrent}>Close</button>
      </Dialog>
    </>
  );
}

ReactDOM.render(
  <DialogStack>
    <App />
  </DialogStack>,
  document.getElementById("root")
);

Installation

  1. npm i react-use-dialog

Usage

  1. Wrap your component (or App root) in a <DialogStack>. This provides the context for all other dialog children and the useDialogs hook.
  2. Add a <Dialog>

API

You can open dialogs through the useDialogs hook or by passing isOpen prop directly to Dialogs.

Hooks

  • useDialogs<DialogKeys>
    • open(id: string | number): Open a dialog in the current stack.
    • close(id: string | number): Close a dialog in the current stack.
    • closeAll(): Close all dialogs in the current stack.
    • closeCurrent(): Close the topmost dialog.
    • closeAtIndex(index: number): Close all dialogs that have same or larger index.

Components

  • <DialogStack /> - Context provider. You might want to have only one at the root of your app or multiple depending on your use-case.
    • portalTarget?: Element = document.body - An element to which the dialog will be rendered into. Defaults to body
  • <Dialog />
    • id: string | number: Unique identifier in the current dialog stack.
    • portalTarget?: Element = document.body - An element to which the dialog will be rendered into. Overrides the stack target.
    • isOpen?: boolean: Allows to toggle the dialog state on outside state change, etc. You can also use useDialogs().open(id: string)
    • closeOnEsc?: boolean = true: Sets if dialog can be closed by pressing the ESC key.
    • closeOnOverlayClick?: boolean = true: Similarly to closeOnEsc you might want to disable a default behaviour of closing when user clicks the overlay.
    • showOverlay?: boolean = true: Set if the dialog will be rendered in an overlay. You will have to supply the overlay yourself if set to false.
    • overlayProps?: OverlayProps: Allows to pass custom properties to default overlay element. This is mainly for styling, but you can also pass dataTestId prop for test targetting purposes.
    • onOpen?: Function: Called when dialog state changes to open.
    • onClose?: Function: Called when dialog state changes to closed.
    • onEscPress?: Function: Called before onClose when user presses ESC on an escapable dialog.
    • onOverlayClick?: Function: Can be used in addition to isOpen to handle dialog state outside of the DialogStack context.
  • Overlay
    • dataTestId?: string: Shorthand property for easier test targetting.
    • …HTMLProps<HTMLDivElement>