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-promise-element

v1.0.0

Published

React hook 'usePromiseElement'

Downloads

5

Readme

🛠 React hook 'usePromiseElement'

npm npm bundle size license David Storybook

This hook was created to solve the problem with confirm modal window. But one you can use to solve other tasks if you wrap sub component into promise and waiting answer of him.

Install

# npm
npm install use-promise-element

# yarn
yarn add use-promise-element

Documentation

Methods

usePromiseElement (export)

[GetProps] [UsePromiseElement]

/**
 * @param component React component witch will be create when run method 'open'
 * @param getProps Function to getting props 'component' when run method 'open'
 */
export declare const usePromiseElement: <
  Result = any,
  Props extends object = {}
>(
  component:
    | React.ComponentClass<object | Props, any>
    | React.FC<object | Props>,
  getProps?: GetProps<Result, Props> | undefined,
) => UsePromiseElement<Result, Props>;

Types

GetProps (export)

/**
 * Type method getting props
 */
type GetProps<
  Result = any,
  Props extends object = {
    onSuccess(): void;
    onCancel(): void;
  }
> = (
  resolve: (value?: Result | PromiseLike<Result> | undefined) => void,
  reject: (reason?: any) => void,
) => Props;

UsePromiseElement

[Open] [Close]

/**
 * Type returns hook 'usePromiseElement'
 */
type UsePromiseElement<Result = any, Props extends object = {}> = [
  React.ReactElement | null,
  Open<Result, Props>,
  Close<Result>,
];

Open

[GetProps]

/**
 * Type method opening component
 */
type Open<Result = any, Props extends object = {}> = (
  getProps?: GetProps<Result, Props>,
) => Promise<Result>;

Close

[CloseCallback]

/**
 * Type method closing component
 */
type Close<Result = any> = (callback?: CloseCallback<Result>) => void;

CloseCallback

/**
 * Function callback for run getting result promise
 */
type CloseCallback<Result = any> = (
  resolve: (value?: Result | PromiseLike<Result> | undefined) => void,
  reject: (reason?: any) => void,
) => void;

Examples

It is simple example, for get more examples see storybook.

./src/modal.tsx

import * as React from "react";

export interface ModalProps {
  onSuccess?(): void;
  onCancel?(): void;
}

export const Modal: React.FC<ModalProps> = (props) => {
  return (
    <div className="modal">
      <div className="modal-title">Modal window</div>

      <button className="modal-success" onClick={props.onSuccess}>
        Success
      </button>

      <button className="modal-close" onClick={props.onCancel}>
        Cancel
      </button>
    </div>
  );
};

./src/app.tsx

import * as React from "react";
import { usePromiseElement } from "use-promise-element";
import { Modal } from "./modal";

export const App: React.FC = () => {
  /**
   * Use hook with set default props for component
   * (resolve, reject) => ({ onSuccess: resolve, onCancel: reject })
   */
  const [modal, open] = usePromiseElement(Modal);

  const clickOpne = React.useCallback(async () => {
    try {
      // Open Modal (create Modal)
      await open();

      // Code after click on button 'Success' in Modal
    } catch (e) {
      // Code after click on button 'Cancel' in Modal
    }
  }, [open]);

  return (
    <div>
      <button>Open</button>

      {/* insert modal */}
      {modal}
    </div>
  );
};

Tests

# npm
npm install
npm run test

# yarn
yarn install
yarn test