The Alert API allows you to display native system alert dialogs with custom messages, buttons, and types.
Creating an Alert
use Native\Desktop\Facades\Alert;
Alert::new()
->title('Confirm Action')
->detail('Are you sure you want to continue?')
->buttons(['Cancel', 'Continue'])
->defaultId(1)
->cancelId(0)
->show('This action cannot be undone');
Methods
new()
Create a new alert instance.
Returns a new Alert instance
type()
Set the alert type. Controls the icon displayed in the alert.
The alert type: none, info, error, question, or warning
Alert::new()
->type('warning')
->show('This is a warning message');
title()
Set the alert dialog title.
The title text for the alert dialog
Alert::new()
->title('Important Notice')
->show('Please read this carefully');
detail()
Set additional detail text for the alert.
Additional detail text displayed below the message
Alert::new()
->title('Update Available')
->detail('Version 2.0 includes new features and bug fixes')
->show('A new version is available');
Set custom button labels for the alert.
Array of button label strings
Alert::new()
->buttons(['Cancel', 'Save', 'Don\'t Save'])
->show('Do you want to save your changes?');
defaultId()
Set which button is focused by default (0-indexed).
Index of the default button
Alert::new()
->buttons(['Cancel', 'Delete'])
->defaultId(0) // Focus on "Cancel"
->show('Delete this item?');
cancelId()
Set which button is triggered when the user presses Escape.
Index of the cancel button
Alert::new()
->buttons(['Cancel', 'Delete'])
->cancelId(0) // Escape triggers "Cancel"
->show('Delete this item?');
show()
Display the alert dialog with the specified message.
The main message to display
Returns the index of the button that was clicked
$response = Alert::new()
->buttons(['Cancel', 'Continue'])
->show('Do you want to continue?');
if ($response === 1) {
// User clicked "Continue"
}
error()
Show a simple error dialog.
Returns true when the dialog is dismissed
Alert::error(
'Operation Failed',
'Could not save the file. Please check permissions.'
);
Complete Example
use Native\Desktop\Facades\Alert;
// Confirmation dialog
$result = Alert::new()
->type('question')
->title('Confirm Deletion')
->detail('This item will be permanently deleted.')
->buttons(['Cancel', 'Delete'])
->defaultId(0)
->cancelId(0)
->show('Are you sure you want to delete this item?');
if ($result === 1) {
// User confirmed - proceed with deletion
deleteItem();
}
// Simple error alert
Alert::error(
'Network Error',
'Could not connect to the server. Please check your internet connection.'
);
// Warning with multiple options
$response = Alert::new()
->type('warning')
->title('Unsaved Changes')
->buttons(['Cancel', 'Don\'t Save', 'Save'])
->defaultId(2)
->show('Do you want to save your changes before closing?');
match ($response) {
0 => handleCancel(),
1 => closeWithoutSaving(),
2 => saveAndClose(),
};
Alert Types
The type() method accepts these values:
- none - No icon
- info - Information icon (ℹ️)
- error - Error icon (❌)
- question - Question icon (❓)
- warning - Warning icon (⚠️)
The appearance of alert dialogs varies by platform (macOS, Windows, Linux). NativePHP uses the native alert system for each platform to provide a consistent user experience.