|
| 1 | +--- |
| 2 | +title: Alerts |
| 3 | +order: 410 |
| 4 | +--- |
| 5 | + |
| 6 | +## Native Alerts |
| 7 | + |
| 8 | +NativePHP allows you to show native alerts to the user. They can be used to display messages, ask for confirmation, or |
| 9 | +report an error. |
| 10 | + |
| 11 | +Alerts are created using the `Alert` facade. |
| 12 | + |
| 13 | +```php |
| 14 | +use Native\Laravel\Facades\Alert; |
| 15 | +``` |
| 16 | + |
| 17 | +### Showing Alerts |
| 18 | + |
| 19 | +To show an alert, you may use the `Alert` class and its `show()` method. |
| 20 | + |
| 21 | +```php |
| 22 | +Alert::new() |
| 23 | + ->show('This is a simple alert'); |
| 24 | +``` |
| 25 | + |
| 26 | +## Configuring Alerts |
| 27 | + |
| 28 | +### Alert Title |
| 29 | + |
| 30 | +You may set the title of the alert using the `title()` method. |
| 31 | + |
| 32 | +```php |
| 33 | +Alert::new() |
| 34 | + ->title('Pizza Order') |
| 35 | + ->show('Your pizza has been ordered'); |
| 36 | +``` |
| 37 | + |
| 38 | +### Alert Buttons |
| 39 | + |
| 40 | +You may configure the buttons of the alert using the `buttons()` method. |
| 41 | +This method takes an array of button labels. |
| 42 | + |
| 43 | +The return value of the `show()` method is the index of the button that the user clicked. |
| 44 | +Example: If the user clicks the "Yes" button, the `show()` method will return `0`. If the user clicks the "Maybe" |
| 45 | +button, the `show()` method will return `2`. |
| 46 | + |
| 47 | +If no buttons are defined, the alert will only have an "OK" button. |
| 48 | + |
| 49 | +```php |
| 50 | +Alert::new() |
| 51 | + ->buttons(['Yes', 'No', 'Maybe']) |
| 52 | + ->show('Do you like pizza?'); |
| 53 | +``` |
| 54 | + |
| 55 | +### Alert Detail |
| 56 | + |
| 57 | +You may set the detail of the alert using the `detail()` method. |
| 58 | +The detail is displayed below the message and provides additional information about the alert. |
| 59 | + |
| 60 | +```php |
| 61 | +Alert::new() |
| 62 | + ->detail('Fun facts: Pizza was first made in Naples in 1889') |
| 63 | + ->show('Do you like pizza?'); |
| 64 | +``` |
| 65 | + |
| 66 | +### Alert Type |
| 67 | + |
| 68 | +You may set the type of the alert using the `type()` method. |
| 69 | +The type can be one of the following values: `none`, `info`, `warning`, `error`, `question`. On Windows, `question` |
| 70 | +displays the same icon as `info`. On macOS, both `warning` and `error` display the same warning icon. |
| 71 | + |
| 72 | +```php |
| 73 | +Alert::new() |
| 74 | + ->type('error') |
| 75 | + ->show('An error occurred'); |
| 76 | +``` |
| 77 | + |
| 78 | +### Alert Default Button |
| 79 | + |
| 80 | +You may set the default button of the alert using the `defaultId()` method. |
| 81 | +The default button is preselected when the alert appears. |
| 82 | + |
| 83 | +The default button can be set to the index of the button in the `buttons()` array. |
| 84 | + |
| 85 | +```php |
| 86 | +Alert::new() |
| 87 | + ->defaultId(0) |
| 88 | + ->buttons(['Yes', 'No', 'Maybe']) |
| 89 | + ->show('Do you like pizza?'); |
| 90 | +``` |
| 91 | + |
| 92 | +### Alert Cancel Button |
| 93 | + |
| 94 | +You may set the cancel button of the alert using the `cancelId()` method. |
| 95 | +The cancel button is the button that is selected when the user presses the "Escape" key. |
| 96 | + |
| 97 | +The cancel button can be set to the index of the button in the `buttons()` array. |
| 98 | + |
| 99 | +By default, this is assigned to the first button labeled 'Cancel' or 'No'. If no such buttons exist and this option is |
| 100 | +not set, the return value will be `0`. |
| 101 | + |
| 102 | +```php |
| 103 | +Alert::new() |
| 104 | + ->cancelId(1) |
| 105 | + ->buttons(['Yes', 'No', 'Maybe']) |
| 106 | + ->show('Do you like pizza?'); |
| 107 | +``` |
| 108 | + |
| 109 | +### Error Alerts |
| 110 | + |
| 111 | +You may use the `error()` method to display an error alert. |
| 112 | + |
| 113 | +The `error()` method takes two required parameters: the title of the error alert and the message of the error alert. |
| 114 | + |
| 115 | +```php |
| 116 | +Alert::new() |
| 117 | + ->error('An error occurred', 'The pizza oven is broken'); |
| 118 | +``` |
0 commit comments