| ⚠ Notice | This library is still work in progress. |
|---|
Are you bored with the default alert or prompt messages in your website? Then this library is for you.
With Promises and the async and await directives, you are able to achieve nearly the exact same behavior as the
original notification mechanisms, but with styling!
Pick your preferred version from the build directory and include it in your website,
preferably at the end of the page, just above the closing </body> tag.
These snippets are also only working properly as modules, here is an example on how to include them:
// ...
<script type="module" src="./js/main.mjs"></script>
// ...
</body>
</html>To be able to comfortably achieve the pseudo process blocking mechanism, there are a few things you should take note of.
As the await directive is used, you need to surround these operations with another
function body which got the async directive. This will work quite well in class or functions
but in the context of a main process, you need to work with the old-fashioned .then() and .catch() methods.
const result = await alert('Are you sure?');| Name | Type | Description |
|---|---|---|
| confirmButtonText | string |
Determines the text on the confirm button |
const answer = await prompt('What is your name?');
console.log(answer) // 'answer' will contain the user input| Name | Type | Description |
|---|---|---|
| confirmButtonText | string |
Determines the text on the confirm button |
| cancelButtonText | string |
Determines the text on the cancel button |
| showInput | boolean |
Displays a input field for the user |
| showCancelButton | boolean |
Displays a cancel button to cancel the current operation |
| validators | array |
Accepts a list of validators |
While the validators are only available for the prompt input and also only when the input field is visible, it is worth mentioning the functionality.
To define a single or multiple validators, add them to the validators property:
import NotEmpty from './Validators/NotEmpty';
// ...
await prompt('How is it going?', {
showInput: true,
confirmButtonText: 'Next',
validators: [
new NotEmpty()
]
});
// ...| Name | Arguments | Description |
|---|---|---|
| Enum | array |
Allows only specific values to be valid |
| Range | min, max |
Field may have a value within a specific range |
| NotEmpty | none | Field may have a length of more than zero |
| IsString | none | Whether the field contains a string or not |
| IsNumber | none | Whether the field contains a number or not |
Create your class and extend from Validator. You need to define the property errorMessage,
which is used to display the message. These messages are being translated.
You also need to define the method validate, which accepts the argument value and can be used
to determine its validity. Just return true when the validator should succeed or false otherwise.
import Validator from "../Validator.mjs";
class IsEmail extends Validator {
errorMessage = 'Value seems to be an invalid email address.';
validate(value) {
return typeof value === 'string' && (/\@/).test(value);
}
}
export default IsString;import alert from './Modules/Alert';
class ExampleClass {
async exampleMethod() {
// ...
const answer = await alert('Are you sure?');
// This section is only being reached after the user processed the alert box
// ..
}
}async function askForPermission() {
// ...
const answer = await prompt('Please type "bundle/package" to confirm deletion:');
// This section is only being reached after the user processed the prompt
if (answer === 'bundle/package') {
// Here you can put the code into which gets executed after the user confirmed the input
}
// ..
}
More details coming soon...