An open API service indexing awesome lists of open source software.

https://github.com/alekangelov/react-alert-async

A simple way of managing your alerts, prompts and confirms without defaulting to the browsers ugly implementations
https://github.com/alekangelov/react-alert-async

alerts confirms javascript modals react

Last synced: about 2 months ago
JSON representation

A simple way of managing your alerts, prompts and confirms without defaulting to the browsers ugly implementations

Awesome Lists containing this project

README

          

# react-alert-async

> A simple to use Alert/Confirmation system for React. On the server or the client.

[![NPM](https://img.shields.io/npm/v/react-alert-async.svg)](https://www.npmjs.com/package/react-alert-async) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

![The React Alert Async Logo](https://i.imgur.com/ywdToxR.jpg)

## Installation

```bash
yarn add react-alert-async
```

## Usage

Honestly, it's as simple as one two three.

```tsx
import AlertProvider, { alert, confirm, prompt } from 'react-alert-async'
import 'react-alert-async/dist/index.css'

function App() {
return (
<>


alert("Yay, it's an alert", { duration: 2000, title: 'Custom Title' })
}
/>
>
)
}
```

## Styling

It's pretty easy to style the components, they follow simple naming structures and it's encouraged to write your own styles

```scss
.alert {
/* this is the background of the alert */
&-inner {
/* body wrapper of the alert */
}
&-title {
/* title of the alert */
}
&-text {
/* text describer of the alert */
}
&-input {
/* the prompt input field */
}
&-buttons {
/* button wrapper of the alert */
&_ok {
/* ok button of the alert */
}
&_cancel {
/* cancel button of the alert */
}
}
}
```

## Sample Components

All taken from the demo, but modified just a bit.

### Alert

```jsx
function Alert() {
const [state, setState] = useState("Nothing's been alerted")
return (


{state}




{
try {
const x = await alert('Idk write something meaningful here')
setState('Yay, so you read all that')
} catch (e) {
setState("Oh, we're sorry about that :(")
}
}}
>
Make an alert



)
}
```

### Prompt

```jsx
function Prompt() {
const [state, setState] = useState("Nothing's been prompted")
return (


{state}




{
try {
const x = await prompt("What's your name?")
setState('Your name is: ' + x)
} catch (e) {
setState('Oh dang, we didnt get your name :(')
}
}}
>
Make a prompt



)
}
```

### Confirm with custom buttons

```jsx
function Confirm() {
const [state, setState] = useState("Nothing's been confirmed, nor denied")
return (


{state}




{
try {
await confirm("Read some of the things we've written", {
okLabel: "I'm custom 🤯",
cancelLabel: "I'm custom too 😎"
})
setState('Yay, so you read all that')
} catch (e) {
setState("Oh, we're sorry about that :(")
}
}}
>
Make a confirm



)
}
```

### Custom Body

```jsx
{
try {
const x = await alert(
() => (


I'm the title?



Lorem ipsum dolor sit amet consectetur, adipisicing elit.
Esse, id sunt nulla nam praesentium necessitatibus
voluptate consequatur, nesciunt ut cupiditate distinctio
natus dolorem debitis adipisci non deleniti dolores rem
vitae.



),
{
duration: 500
}
)

setState('Yay, so you read all that')
} catch (e) {
setState("Oh, we're sorry about that :(")
}
}}
>
Make a Funcitonal Alert

{
try {
const x = await alert(
class extends React.Component {
render() {
return (


I'm the title?



Lorem ipsum dolor sit amet consectetur, adipisicing
elit. Esse, id sunt nulla nam praesentium
necessitatibus voluptate consequatur, nesciunt ut
cupiditate distinctio natus dolorem debitis adipisci
non deleniti dolores rem vitae.



)
}
},
{
duration: 500
}
)

setState('Yay, so you read all that')
} catch (e) {
setState("Oh, we're sorry about that :(")
}
}}
>
Make a Class Alert

```

## TODO

You can follow the project's board here: https://github.com/alekangelov/react-alert-async/projects/1

## License

MIT © [alekangelov](https://github.com/alekangelov)