Livewire Alert

repository·master·Indexed 21 days ago

https://github.com/jantinnerezo/livewire-alert

A Laravel Livewire package that integrates SweetAlert2 notifications into Livewire components using a fluent, chainable PHP API. It allows developers to trigger alerts, confirmation dialogs, and input fields directly from PHP, eliminating the need for manual JavaScript. Supports PHP 8.1+, Laravel 10.x+, and Livewire 3.x or 4.x, with specific integration support for Filament.

Tokens
2K
Snippets
8
Records
11
Agent score
74%

What's inside livewire-alert

  1. Configure SweetAlert2 for Filament

    master

    If you are using Filament, you need to register SweetAlert2 as a Filament asset in your AppServiceProvider to ensure it is loaded correctly within the Filament admin panel.

    use Filament\Support\Facades\FilamentAsset;
    use Illuminate\Support\Facades\Vite;
    use Filament\Support\Assets\Js;
    
    public function boot()
    {
        FilamentAsset::register([
            Js::make('sweetalert2', Vite::asset('resources/js/sweetalert2.js')),
            // Or via CDN:
            // Js::make('sweetalert2', 'https://cdn.jsdelivr.net/npm/sweetalert2@11'),
        ]);
    }
  2. Install Livewire Alert

    master

    To use Livewire Alert in your Laravel project, follow these steps:

    1. Install the package via Composer:

      composer require jantinnerezo/livewire-alert
    2. Publish the configuration (optional):

      php artisan vendor:publish --tag=livewire-alert:config
    3. Install SweetAlert2: You must have SweetAlert2 available in your frontend. You can install it via npm/yarn or use a CDN.

    Via npm/yarn:

    npm install sweetalert2
    # or
    yarn add sweetalert2

    Then, import it in your resources/js/app.js to make it globally available:

    import Swal from 'sweetalert2'
    
    window.Swal = Swal

    Via CDN: Include this script before the closing </body> tag:

    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    composer require jantinnerezo/livewire-alert
  3. Use the LivewireAlert Facade to trigger alerts

    master

    Livewire Alert provides a fluent, chainable API via the LivewireAlert facade. You can trigger different types of notifications (like success, error, warning, etc.) directly from your Livewire component methods.

    use Jantinnerezo\LivewireAlert\Facades\LivewireAlert;
    
    public function save()
    {
        LivewireAlert::title('Changes saved!')
            ->success()
            ->show();
    }
  4. Configure alert buttons

    master

    You can customize the visibility and appearance of the confirmation, cancel, and deny buttons:

    • withConfirmButton(?string $confirmButtonText = null): Shows the confirm button. Uses config value if text is null.
    • withCancelButton(?string $cancelButtonText = null): Shows the cancel button.
    • withDenyButton(?string $denyButtonText = null): Shows the deny button.
    • confirmButtonText(string $text): Sets the text for the confirm button.
    • cancelButtonText(string $text): Sets the text for the cancel button.
    • denyButtonText(string $text): Sets the text for the deny button.
    • confirmButtonColor(string $color): Sets the CSS color for the confirm button.
    • cancelButtonColor(string $color): Sets the CSS color for the cancel button.
    • denyButtonColor(string $color): Sets the CSS color for the deny button.
    • reverseButtons(bool $reverse = true): Reverses the order of the buttons.
  5. Handle alert confirmation, denial, and dismissal

    master

    You can attach custom actions to an alert that trigger when a user interacts with it. These actions are dispatched as Livewire events. Use the following methods to define these behaviors:

    • onConfirm(string $action, mixed $data = null): Triggers when the user confirms the alert.
    • onDeny(string $action, mixed $data = null): Triggers when the user selects the deny option.
    • onDismiss(string $action, mixed $data = null): Triggers when the alert is dismissed (e.g., clicking outside or closing).

    The $action string is the name of the event that will be dispatched, and $data is an optional payload sent with the event.

    $this->alert()
        ->question('Are you sure?')
        ->onConfirm('delete-user', ['id' => 1])
        ->onDeny('cancel-delete')
        ->show();
  6. Access the LivewireAlert instance via Service Container

    master

    The LivewireAlert class is bound to the Laravel service container. You can resolve it using either the class name Jantinnerezo\LivewireAlert\LivewireAlert or the alias livewire-alert. The instance is automatically injected with the current Livewire component.

    // Resolving via class name
    $livewireAlert = app(\Jantinnerezo\LivewireAlert\LivewireAlert::class);
    
    // Resolving via alias
    $livewireAlert = app('livewire-alert');
  7. Configure and trigger SweetAlert2 notifications

    master

    The LivewireAlert class provides a fluent API to configure and trigger SweetAlert2 notifications from within a Livewire component. You can chain methods to set the title, text, icon, position, and various other SweetAlert2 options before calling show() to display the alert.

    Common configuration methods include:

    • title(string $title): Sets the alert title.
    • text(string $text): Sets the alert body text.
    • success(), error(), warning(), info(), question(): Sets the alert icon.
    • toast(bool $toast): Configures the alert as a toast notification.
    • asToast(): A shortcut for a toast notification positioned at TopEnd with a timer and progress bar.
    • asConfirm(): A shortcut for a confirmation dialog with confirm and deny buttons.
    • asLoading(string $title): A shortcut for a loading state alert.
    • timer(int|null $timer): Sets the auto-close timer in milliseconds.
    • position(Enums\​Position|string $position): Sets the alert position.

    To display the alert, you must call show().

    $this->alert()
        ->title('Success!')
        ->text('Your changes have been saved.')
        ->success()
        ->show();
  8. Add input fields to alerts

    master

    Livewire Alert supports various input types to collect data from the user via SweetAlert2. You can use the following methods to add inputs:

    • withTextInput(?string $label = null, ?string $placeholder = null, ?string $value = null)
    • withEmailInput(?string $label = null, ?string $placeholder = null, ?string $value = null)
    • withPasswordInput(?string $label = null, ?string $placeholder = null)
    • withNumberInput(?string $label = null, ?string $placeholder = null, ?string $value = null)
    • withTextareaInput(?string $label = null, ?string $placeholder = null, ?string $value = null)
    • withSelectInput(array $options = [], ?string $label = null, ?string $value = null)
    • withRadioInput(array $options = [], ?string $label = null, ?string $value = null)
    • withCheckboxInput(?string $label = null, bool $checked = false)
    • withFileInput(?string $label = null)
    $this->alert()
        ->title('Enter your name')
        ->withTextInput('Name', 'John Doe')
        ->onConfirm('submit-name')
        ->show();