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-prompt

v1.1.0

Published

Display a react component to a user asynchronously

Downloads

124

Readme

use-prompt

CI Coverage Status Codacy Badge

NPM Package Typescript Package size MIT License

use-prompt is a React Hook that lets you conveniently display a component to a user asynchronously.

This allows you to ask a user for input, prompt for an answer, display a message, or do whatever you want in an asynchronous manner.

Features:

  • Use your own custom components
  • Promise-based (async/await and try/catch capable)
  • N-number of concurrent prompt support
  • Render anywhere you like for each prompt
  • Typescript support
  • Minimalistic, easy to use API
  • Very small bundle size

  1. API Details
  2. Typescript
  3. Examples

Installation

npm i use-prompt
yarn add use-prompt

API

const [prompt, showPrompt, visible, clearPrompt] = usePrompt(
  options?: {
    persist?: boolean;
  }
): [RenderedPrompt, ShowPrompt, IsVisible, ClearPrompt]

See the API Details page for more information.

Basic example

The following demonstrates a very basic example use of usePrompt.

import usePrompt from 'use-prompt';

function App() {
  const [prompt, showPrompt, visible] = usePrompt();

  function showMyPrompt() {
    showPrompt(({ resolve }) => (
      <>
        <div>She sells seashells by the seashore.</div>
        <button onClick={resolve}>Ok thanks</button>
      </>
    ));
  }

  return (
    <div>
      <button onClick={showMyPrompt} disabled={visible}>
        Show prompt
      </button>
      {prompt}
    </div>
  );
}

Async/await and try/catch

Use async/await and try/catch in order to retrieve what the users response within the prompt was:

import { useState } from 'react';
import usePrompt from 'use-prompt';

function App() {
  const [prompt, showPrompt, visible] = usePrompt();
  const [cancelReason, setCancelReason] = useState(null);
  const [confirmResponse, setConfirmResponse] = useState(null);

  async function showMyPrompt() {
    try {
      const response = await showPrompt(({ resolve, reject }) => (
        <>
          <div>Are you sure?</div>
          <button onClick={() => reject('clicked cancel')}>Cancel</button>
          <button onClick={() => resolve('clicked yes')}>Yes</button>
        </>
      ));
      setConfirmResponse(response);
    } catch (reason) {
      setCancelReason(reason);
    }
  }

  return (
    <>
      <button onClick={showMyPrompt} disabled={visible}>
        Show prompt
      </button>
      {prompt}
      {confirmResponse && `Prompt was confirmed: ${confirmResponse}`}
      {cancelReason && `Prompt was cancelled: ${cancelReason}`}
    </>
  );
}

Sequential prompts

Display prompts one after another in a sequential manner:

import usePrompt from 'use-prompt';

function Prompt({ resolve, message }) {
  return (
    <>
      <div>{message}</div>
      <button onClick={resolve}>Ok thanks</button>
    </>
  );
}

function App() {
  const [prompt, showPrompt, visible] = usePrompt();

  async function showPrompts() {
    await showPrompt((props) => <Prompt {...props} message="Prompt 1" />);
    await showPrompt((props) => <Prompt {...props} message="Prompt 2" />);
    await showPrompt((props) => <Prompt {...props} message="Prompt 3" />);
  }

  return (
    <div>
      <button onClick={showPrompts} disabled={visible1 || visible2 || visible3}>
        Show prompts
      </button>
      {prompt}
    </div>
  );
}

Multiple concurrent prompts

Display as many prompts concurrently as you want:

import usePrompt from 'use-prompt';

function Prompt({ resolve, message }) {
  return (
    <>
      <div>{message}</div>
      <button onClick={resolve}>Ok thanks</button>
    </>
  );
}

function App() {
  const [prompt1, showPrompt1, visible1] = usePrompt();
  const [prompt2, showPrompt2, visible2] = usePrompt();
  const [prompt3, showPrompt3, visible3] = usePrompt();

  function triggerPrompt1() {
    showPrompt1((props) => <Prompt {...props} message="Prompt1" />);
  }
  function triggerPrompt2() {
    showPrompt2((props) => <Prompt {...props} message="Prompt2" />);
  }
  function triggerPrompt3() {
    showPrompt3((props) => <Prompt {...props} message="Prompt3" />);
  }

  return (
    <div>
      <button onClick={triggerPrompt1} disabled={visible1}>
        Show prompt 1
      </button>
      {prompt1}

      <button onClick={triggerPrompt2} disabled={visible2}>
        Show prompt 2
      </button>
      {prompt2}

      <button onClick={triggerPrompt3} disabled={visible3}>
        Show prompt 3
      </button>
      {prompt3}
    </div>
  );
}

See the full docs