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

v1.0.1

Published

This library provides an easy and convenient programmatic approach to render anything as a dialog

Downloads

4

Readme

This library allows programmatic creation of dialogs instead of using states which is often time a little inconvenient and results in quite a bit of boilerplate, its minimal, yet powerful API lets you render anything as a dialog, in additional to that, the CSS required for each created dialog is simple with no deeply nested selectors allowing you to easily style your dialogs anyway you want to fit your needs.

Why do you want to programmatically create dialogs? The short answer is simplicity, the long answer is because dialogs are on-demand components, you don't want them to appear inside your DOM tree until you need them which results in less bloated HTML, in additional to that, you don't have to create and maintain a new state to manage the dialog's life cycle (showing and closing the dialog)

Installation

npm i -S react-dialog-builder

Setting up

import { DialogBuilder } from 'react-dialog-builder';

// This is necessary because this library relies on a few CSS rules
import 'react-dialog-builder/light.css';
// or import 'react-dialog-builder/dark.css';

Available APIs

class DialogBuilder {
    /**
     * The entry point to the `DialogBuilder` APIs
     *
     * @param content The component's JSX to render
     * @returns A new dialog builder instance
     */
    static newDialog(content: ReactNode): DialogBuilder;

    /**
     * Adds another class name for this dialog, newly added class names are
     * concatenated instead of replacing previous ones.
     *
     * @param value The new class name to add
     * @returns This dialog builder instance
     */
    addClassName(value: string): DialogBuilder;

    /**
     * Sets whether this dialog can be closed by clicking on the backdrop, `false` by default
     *
     * @returns This dialog builder instance
     */
    dismissible(): DialogBuilder;

    /**
     * Sets whether the backdrop should be transparent, `false` by default
     *
     * @returns This dialog builder instance
     */
    transparentBackdrop(): DialogBuilder;

    /**
     * Opens the currently configured dialog, returns a dialog reference object that includes methods
     * to close this dialog or to set a callback to be invoked after the dialog is closed
     *
     * @returns A {@link DialogRef} object
     */
    open(): DialogRef;
}
/**
 * Represents an object that contains a reference to the opened dialog which allows
 * closing the dialog or setting a callback to be invoked after the dialog is closed
 */
interface DialogRef {
    /**
     * Manually close this dialog
     */
    close(): void;

    /**
     * Sets the callback to be invoked after the dialog is closed.
     */
    runAfterClosed(fn: () => void): void;
}

Using DialogBuilder

DialogBuilder contains a single entry point via the static method called newDialog which returns a new DialogBuilder. It uses the fluent interface pattern to make the APIs more friendly to develop with.

Code example

import { DialogBuilder } from 'react-dialog-builder';

import 'react-dialog-builder/light.css';

function App() {
  const openDialog = () => {
    const dialogRef: DialogRef = DialogBuilder.newDialog(
      <div style={{
        display: 'flex',
        flexFlow: 'column nowrap',
        alignItems: 'flex-end',
        backgroundColor: '#fff',
        padding: '5px 20px 15px',
        borderRadius: 4,
        fontFamily: 'Helvetica Neue'
      }}>
        <p>Creating dialogs can never be easier</p>
        <button
          type='button'
          style={{
            marginTop: 10
          }}
          onClick={() => {
              // Use the returned `DialogRef` object to close the dialog manually
              dialogRef.close();
          }}>
          Close
        </button>
      </div>
    )
        // Calling this method will make clicking on the backdrop close this dialog
      .dismissible()

      // The final step in configuring this dialog which returns a `DialogRef`
      // object allowing you to close this dialog instance or to set a callback to
      // be invoked after the dialog is closed
      .open();

    // The returned `DialogRef` object contains a method to set a callback to be
    // invoked right after the dialog is closed
    dialogRef.runAfterClosed(() => console.info('Dialog was closed'));
  };

  return (
    <button onClick={openDialog}>Demo for react-dialog-builder library</button>
  );
}

Result

Example 1