Skip to content

async alternatives to browser alert() and prompt() and confirm()

License

Notifications You must be signed in to change notification settings

simonw/prompts-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Prompts

npm version License: Apache2

A lightweight, dependency-free JavaScript library for creating accessible modal alert, confirm, and prompt dialogs. This library implements an async promise-based approach as an alternative to the browser built-in alert(), confirm(), and prompt() functions.

Features

  • Keyboard accessible: Built with keyboard navigation in mind, including focus trapping and keyboard navigation (Tab, Shift+Tab, Escape, Enter).
  • Promise-based: Uses Promises to allow for easy async/await usage.
  • No Dependencies: Written in pure JavaScript, requiring no external libraries or frameworks.
  • Lightweight: The library is small and designed to add minimal overhead to your project.

Installation

npm install prompts-js

Or use it from a CDN:

<script src="https://cdn.jsdelivr.net/npm/prompts-js"></script>

Usage

// Alert
await Prompts.alert("This is an alert message!");
console.log("Alert closed");

// Confirm
const confirmed = await Prompts.confirm("Are you sure you want to proceed?");
if (confirmed) {
  console.log("User confirmed");
} else {
  console.log("User canceled");
}

// Prompt for a string
const userInput = await Prompts.prompt("Please enter your name:");
if (userInput) {
  console.log("User entered:", userInput);
} else {
  console.log("User canceled or entered nothing");
}

API

await Prompts.alert(message)

Displays an alert dialog with an "OK" button.

  • message (string): The message to display in the alert.

await Prompts.confirm(message)

Displays a confirmation dialog with "OK" and "Cancel" buttons.

  • message (string): The message to display in the confirmation dialog.
  • Resolves: true if the user clicks "OK", false if they click "Cancel" or press escape.

await Prompts.prompt(message)

Displays a prompt dialog with a text input field and "OK" and "Cancel" buttons.

  • message (string): The message to display above the input field.
  • Resolves: the user's input (string) if they click "OK", null if they click "Cancel" or press escape.

About

async alternatives to browser alert() and prompt() and confirm()

Resources

License

Stars

Watchers

Forks

Sponsor this project