Message
Use the bsMessageModal
function to alert users to a simple message or to capture information via a pop-up modal dialog.
Note, the bsMessageModal
function requires the Bootstrap 4+ JavaScript library. This library can be downloaded or referenced via CDN from https://www.bootstrapcdn.com.
The example below displays a simple message in a modal dialog by setting the message
property on the configuration parameter to the constructor.
The message
property can also be set to HTML.
Add a title by using the title
property.
Give the modal a DOM id
using the id property. This is convenient of its required to reference the modal from code or inside style sheets.
Buttons
A simple modal like the one above will contain an Ok button that can be pressed when the user has finished reading the message. To add a Cancel button, set the cancel
property's visible
attribute to true
.
Labels
To change the labels for either the Ok or Cancel button, use the label
attributes of each.
Colors
To change the colors for the modal and/or the buttons, set the css
properties. The property on the root configuration is set on the modal's title.
Callback
Add a custom callback function to the configuration via the callback
property. This function will be called when either the Ok or Cancel buttons have been clicked.
The first parameter tells the function whether the Ok button was clicked. The second parameter is a method that when called returns control to the modal. This method takes an optional parameter that tells the modal whether it should stay up (true
) or is ok to drop (false
or undefined
).
Await
A call to bsMessageModal
runs synchronously which means after the modal is displayed the code below the line that called the modal continues to run. In some cases, it is required that the response from the modal be considered before continuing with the code logic. Callbacks are typically the constructs used in JavaScript for this purpose. However, callbacks can get clumsy if responses from a number of them need to be chained together.
The await
keyword can be used to asynchronously pause the code execution via a promise at the line in which the modal was called. Here, the code waits for the modal response before continuing.
In the example below, bsMessageModal
returns a promise and waits for the response from the modal. It returns the same ok
parameter from the callback in the earlier example.
Data Inputs
Because the message property supports HTML, advanced templates can be used with the message modal that include forms and other data input HTML blocks.
Validation
Sometimes it is required that the modal stay up based on the user input. For example, if data input fails validation, the user may be asked to change the input.
Returning true
in the callback in the example below, will prevent the modal from dropping based on the your-name
input failing validation.
Last updated