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

use-react-dialog

v0.2.0

Published

Lightweight React library for dialog windows management.

Downloads

4

Readme

use-react-dialog · GitHub license npm version npm version npm version

use-react-dialog is a lightweight React library written on a TypeScript. The main goal is to simplify the way you manage dialog windows in your React application.

Installation

// using npm
npm install use-react-dialog

// using yarn
yarn add use-react-dialog

Documentation

<DialogProvider /> - React Context Provider. Used for passing context data to the application. Required to be used.

Props:

  • dialogs - an object that defines all the dialogs in a way: Record<string, DialogComponent>. This is used for identifying the correct dialog component by its name for different operations (e.g. opening, closing, etc.).

Returns: JSX.Element.


useDialog - React Hook. Used for fetching data from the DialogProvider.

Props:

  • name- a string that is used as an identifier for a dialog window. Optional.
  • data- anything that is passed as a data to a dialog window. Optional.

Returns:

  • name- a string that is used as an identifier for a dialog window.
  • dialogs - an array of all the dialogs that are open at the moment. Default value: [].
  • openDialog - a function that is used for opening a dialog window. Only one dialog can be opened within one function call.
  • closeDialog- a function that is used for closing a dialog window. Only one dialog can be closed within one function call.
  • updateDialog- a function that is used for updating a dialog window. Only one dialog can be updated within one function call.
  • closeAllDialogs- a function that is used for closing all the open dialogs.

Returns these properties only when name is provided to the hook:

  • isOpen - a boolean that indicates the state of the dialog.
  • data- anything that was passed as a data to a dialog window.
  • index - a number that indicates the index of the current dialog in a dialogs array. If the dialog is closed this equals -1.
  • openCurrentDialog - a function that is used for opening dialog window by name that was passed to the hook. You can pass additional data that will be merged with the data you passed to the useDialog hook before.
  • closeCurrentDialog- a function that is used for closing dialog window by name that was passed to the hook.
  • updateCurrentDialog- a function that is used for updating dialog window by name that was passed to the hook.

Usage (TypeScript)

// src/DialogOne.tsx

import { useDialog } from 'use-react-dialog';

// implement functionality of your dialog
function DialogOne() {
  const { closeCurrentDialog, isOpen, openCurrentDialog } = useDialog('DialogOne');

  if (!isOpen) {
    return (
      // opens DialogOne, alternatively can be -> openDialog('dialog name')
      <button onClick={() => openCurrentDialog()}>
        open dialog one
      </button>
    );
  }

  return (
    <div>
      <p>hello from the dialog one!</p>
      <button onClick={closeCurrentDialog}>you can close me now</button>
    </div>
  );
}


...

// src/dialogs.ts

import { DialogOne } from './DialogOne';

// define dialogs object that contains all the dialogs you gonna use
const dialogs = {
  DialogOne,
} as const;

...

// use-react-dialog.d.ts

// overwrite the types, so everything is well-typed in your project
import { DialogContextProps, DialogByNameContextProps } from 'use-react-dialog';
import { dialogs } from './src/dialogs';

declare module 'use-react-dialog' {
  type Name = keyof typeof dialogs;
  export function useDialog(): DialogContextProps<Name>;
  export function useDialog<T>(name: Name): DialogByNameContextProps<T, Name>;
  export function useDialog<T>(
    name: Name,
    data: T,
  ): DialogByNameContextProps<T, Name>;
}

...

// src/index.ts

import { createRoot } from 'react-dom/client';
import { DialogProvider } from 'use-react-dialog';
import App from './App';
import { dialogs } from './dialogs';

const container = document.getElementById('root')!;
const root = createRoot(container);

root.render(
  <DialogProvider dialogs={dialogs}>
    <App />
  </DialogProvider>,
);

Usage (JavaScript)

// src/DialogOne.jsx

import { useDialog } from 'use-react-dialog';

// implement functionality of your dialog
function DialogOne() {
  const { closeCurrentDialog, isOpen, openCurrentDialog } = useDialog('DialogOne');

  if (!isOpen) {
    return (
      // opens DialogOne, alternatively can be -> openDialog('dialog name')
      <button onClick={() => openCurrentDialog()}>
        open dialog one
      </button>
    );
  }

  return (
    <div>
      <p>hello from dialog one!</p>
      <button onClick={closeCurrentDialog}>you can close me now</button>
    </div>
  );
}

...

// src/dialogs.js

// define dialogs object that contains all the dialogs you gonna use
const dialogs = {
  DialogOne,
};

...

// src/index.js

import { createRoot } from 'react-dom/client';
import { DialogProvider } from 'use-react-dialog';
import App from './App';
import { dialogs } from './dialogs';

const container = document.getElementById('root')!;
const root = createRoot(container);

root.render(
  <DialogProvider dialogs={dialogs}>
    <App />
  </DialogProvider>,
);

Examples

Live demo (MaterialUI, TypeScript, CodeSandbox) example you can find here.

Source code of that example you can find in /example directory.

License

This project is licensed under the terms of the MIT license.