🚀 DAY 4 OF LAUNCH WEEK:Introducing Socket Scanning for OpenVSX Extensions.Learn more →
Socket
Book a DemoInstallSign in
Socket

@byteclaw/use-prompt

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@byteclaw/use-prompt

React hook and component for prompting the user to input anything using your custom components

latest
Source
npmnpm
Version
1.1.3
Version published
Maintainers
2
Created
Source

@Byteclaw/use-prompt

React hook and component for prompting the user to input anything using your custom components.

Installation

npm install @byteclaw/use-prompt
yarn add @byteclaw/use-prompt

Usage

Here is a quick example. answer accepts any values you want so you aren't limited just to boolean values.

By default application uses global event emitter context created by @byteclaw/use-event-emitter hook unless you create your own event emitter context.

Shared global event emitter context

import { Prompts, usePrompt } from '@byteclaw/use-prompt';
import React, { useCallback } from 'react';

function Dialog({ onAccept, onReject }) {
  return (
    <div>
      <p>Are you sure?</p>
      <button onClick={onAccept}>Yes</button>
      <button onClick={onReject}>No</button>
    </div>
  );
}

function ConfirmButton() {
  const [prompt, prompting] = usePrompt();
  const onClick = useCallback(async () => {
    const result = await prompt(({ answer }) => (
      <Dialog onAccept={() => answer(true)} onReject={() => answer(false)} />
    ));
  }, [ask]);

  return (
    <button disabled={prompting} onClick={onClick}>
      Do something
    </button>
  );
}

function App() {
  return (
    <>
      <ConfirmButton />
      <Prompts />
    </>
  );
}

Custom event emitter context

import {
  EventEmitterContext,
  useEventEmitterInstance,
} from '@byteclaw/use-event-emitter';
import { Prompts, usePrompt } from '@byteclaw/use-prompt';
import React, { useCallback } from 'react';

function Dialog({ onAccept, onReject }) {
  return (
    <div>
      <p>Are you sure?</p>
      <button onClick={onAccept}>Yes</button>
      <button onClick={onReject}>No</button>
    </div>
  );
}

function ConfirmButton() {
  const [prompt, prompting] = usePrompt();
  const onClick = useCallback(async () => {
    const result = await prompt(({ answer }) => (
      <Dialog onAccept={() => answer(true)} onReject={() => answer(false)} />
    ));
  }, [ask]);

  return (
    <button disabled={prompting} onClick={onClick}>
      Do something
    </button>
  );
}

function App() {
  const eventEmitter = useEventEmitterInstance();

  return (
    <EventEmitterContext.Provider value={eventEmitter}>
      <ConfirmButton />
      <Prompts />
    </EventEmitterContext.Provider>
  );
}

Keywords

react

FAQs

Package last updated on 24 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts