{"id":13423010,"url":"https://github.com/jantinnerezo/livewire-alert","last_synced_at":"2025-05-14T01:02:09.163Z","repository":{"id":37724833,"uuid":"272130835","full_name":"jantinnerezo/livewire-alert","owner":"jantinnerezo","description":"SweetAlert2 integration for Livewire","archived":false,"fork":false,"pushed_at":"2025-03-20T10:41:03.000Z","size":11792,"stargazers_count":750,"open_issues_count":0,"forks_count":80,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-03T07:57:38.403Z","etag":null,"topics":["alpinejs","laravel","livewire","sweetalert2"],"latest_commit_sha":null,"homepage":"https://livewire-alert.laravel.cloud","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jantinnerezo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":"https://www.paypal.com/paypalme/jantinn"}},"created_at":"2020-06-14T04:12:10.000Z","updated_at":"2025-04-03T03:44:50.000Z","dependencies_parsed_at":"2023-10-14T16:29:10.208Z","dependency_job_id":"1c8a1330-3480-4a20-b221-0eada20884b2","html_url":"https://github.com/jantinnerezo/livewire-alert","commit_stats":{"total_commits":111,"total_committers":16,"mean_commits":6.9375,"dds":"0.28828828828828834","last_synced_commit":"40322eeb8f1d3589563ede00462214a681405a33"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jantinnerezo%2Flivewire-alert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jantinnerezo%2Flivewire-alert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jantinnerezo%2Flivewire-alert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jantinnerezo%2Flivewire-alert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jantinnerezo","download_url":"https://codeload.github.com/jantinnerezo/livewire-alert/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248217181,"owners_count":21066633,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alpinejs","laravel","livewire","sweetalert2"],"created_at":"2024-07-30T23:01:05.250Z","updated_at":"2025-04-10T12:18:44.890Z","avatar_url":"https://github.com/jantinnerezo.png","language":"PHP","funding_links":["https://www.paypal.com/paypalme/jantinn"],"categories":["UI","Packages / Plugins"],"sub_categories":[],"readme":"\n# Livewire Alert\n\n\u003ca href=\"https://github.com/jantinnerezo/livewire-alert/actions\"\u003e\u003cimg src=\"https://github.com/jantinnerezo/livewire-alert/workflows/PHPUnit/badge.svg\" alt=\"Build Status\"\u003e\u003c/a\u003e [![PHPStan Analysis](https://github.com/jantinnerezo/livewire-alert/workflows/PHPStan/badge.svg)](https://github.com/jantinnerezo/livewire-alert/actions) \u003ca href=\"https://packagist.org/packages/jantinnerezo/livewire-alert\"\u003e\u003cimg src=\"https://img.shields.io/packagist/dt/jantinnerezo/livewire-alert\" alt=\"Total Downloads\"\u003e\u003c/a\u003e \u003ca href=\"https://packagist.org/packages/jantinnerezo/livewire-alert\"\u003e\u003cimg src=\"https://img.shields.io/packagist/l/jantinnerezo/livewire-alert\" alt=\"License\"\u003e\u003c/a\u003e\n\nLivewire Alert is a Laravel Livewire package designed to integrate SweetAlert2 notifications seamlessly into Livewire projects. This package simplifies the process of displaying simple, customizable alerts to users, enhancing the interactivity and user experience of your Livewire projects.\n\nYou can check the interactive demo here: [livewire-alert.laravel.cloud](https://livewire-alert.laravel.cloud)\n\n## Requirements\n- PHP 8.1 or higher\n- Laravel 10.x or higher\n- Livewire 3.x\n- Composer\n\n## Installation\n\nFirst, require the package with Composer:\n\n``` bash\ncomposer require jantinnerezo/livewire-alert\n```\n\nOptionally, if you want to customize the global configuration, you can publish the config file:\n\n``` bash\nphp artisan vendor:publish --tag=livewire-alert:config\n```\n\nNext, install SweetAlert2 via npm or yan:\n\nNPM \n``` bash\nnpm install sweetalert2\n```\n\nYarn \n``` bash\nyarn add sweetalert2\n```\n\nAfter installing SweetAlert2, import it into your `resources/js/app.js` file\n\n``` js\nimport Swal from 'sweetalert2'\n\nwindow.Swal = Swal\n```\n\nIf you prefer not to use package manager installation, you can include SweetAlert2 directly via CDN. Add the following script to your Blade layout file `(e.g., resources/views/layouts/app.blade.php)` before the closing `\u003c/body\u003e` tag:\n\n``` html\n\u003cbody\u003e\n    \u003cscript src=\"https://cdn.jsdelivr.net/npm/sweetalert2@11\"\u003e\u003c/script\u003e\n\u003c/body\u003e\n```\n\n## Filament\n\nIntegrate Livewire Alert in your Filament project, simply register the JavaScript asset in the boot method of your `AppServiceProvider` to import SweetAlert2.\n\n``` php\nuse Filament\\Support\\Facades\\FilamentAsset;\nuse Illuminate\\Support\\Facades\\Vite;\nuse Filament\\Support\\Assets\\Js;\n\npublic function boot()\n{\n    FilamentAsset::register([\n        // Local asset build using Vite\n        Js::make('sweetalert2', Vite::asset('resources/js/sweetalert2.js')),\n\n        // Or via CDN\n        Js::make('sweetalert2', 'https://cdn.jsdelivr.net/npm/sweetalert2@11'),\n    ]);\n}\n```\n\n\n## Usage\nThis package provides a convenient Facade that allows you to trigger customizable SweetAlert2-based alerts in your Laravel Livewire application. The Facade uses a fluent interface, enabling you to chain methods to configure your alert before displaying it.\n\n### Basic Usage\n\n``` php\nuse Jantinnerezo\\LivewireAlert\\Facades\\LivewireAlert;\n\npublic function save()\n{\n    LivewireAlert::title('Changes saved!')\n        -\u003esuccess()\n        -\u003eshow();\n}\n```\n\n### Adding text\n\n``` php\nLivewireAlert::title('Item Saved')\n    -\u003etext('The item has been successfully saved to the database.')\n    -\u003esuccess()\n    -\u003eshow();\n```\n\n### Available alert icons\n``` php\nLivewireAlert::title('Success')-\u003esuccess();\n```\n``` php\nLivewireAlert::title('Error')-\u003eerror();\n```\n``` php\nLivewireAlert::title('Warning')-\u003ewarning();\n```\n``` php\nLivewireAlert::title('Info')-\u003einfo();\n```\n``` php\nLivewireAlert::title('Question')-\u003equestion();\n```\n\n### Position\nThe `position()` method allows you to specify where the alert appears on the screen. You can use either the Position enum for type safety or a plain string for flexibility.\n\n#### Using the Position Enum\nLeverage the Position enum for predefined, type-safe options:\n\n``` php\nuse Jantinnerezo\\LivewireAlert\\Enums\\Position;\n\nLivewireAlert::title('Question')\n    -\u003eposition(Position::Center)\n    -\u003equestion()\n    -\u003eshow();\n```\n\n#### Using a String Value\nAlternatively, you can pass a string directly, but it must exactly match one of the `Position` enum values, See: [Position](https://github.com/jantinnerezo/livewire-alert/blob/v4/src/Enums/Position.php) enum.\n\n``` php\nLivewireAlert::title('Question')\n    -\u003eposition('center')\n    -\u003equestion()\n    -\u003eshow();\n```\n\n### Toast Notification\nCreate a toast-style alert with the `toast()` method:\n\n``` php\nLivewireAlert::title('Welcome!')\n    -\u003etext('You have logged in successfully.')\n    -\u003einfo()\n    -\u003etoast()\n    -\u003eposition('top-end')\n    -\u003eshow();\n```\n\n### Timer\nThe `timer()` method sets an auto-dismiss timer for the alert, specifying how long (in milliseconds) the alert remains visible before closing automatically.\n\n#### Usage\nPass an integer representing the duration in milliseconds:\n\n``` php\nLivewireAlert::title('Success')\n    -\u003etext('Operation completed successfully.')\n    -\u003esuccess()\n    -\u003etimer(3000) // Dismisses after 3 seconds\n    -\u003eshow();\n```\n\n### Buttons\n\nThe LivewireAlert package provides methods to add and customize buttons in your alerts: `withConfirmButton()`, `withCancelButton()`, and `withDenyButton()`. Each button can trigger specific events, handled via `onConfirm()`, `onDeny()`, or `onDismiss()`, allowing you to execute custom logic in your Livewire component.\n\n#### Confirm\n``` php\nLivewireAlert::title('Save?')\n    -\u003ewithConfirmButton('Yes, Save')\n    -\u003eshow();\n```\n\n#### Cancel\n``` php\nLivewireAlert::title('Hey cancel')\n    -\u003ewithCancelButton('Cancel')\n    -\u003eshow();\n```\n\n#### Deny\n``` php\nLivewireAlert::title('Are you do you want to do it?')\n    -\u003ewithDenyButton('No')\n    -\u003eshow();\n```\n\n### Button text\n\nAlternatively, you can use `confirmButtonText()`, `cancelButtonText()`, and `denyButtonText()` to set the text after enabling the buttons with `withConfirmButton()`, `withCancelButton()`, or `withDenyButton()` without text:\n\n``` php\nLivewireAlert::title('Save?')\n    -\u003ewithConfirmButton() // Enables button with default text\n    -\u003econfirmButtonText('Yes')\n    -\u003ewithDenyButton()    // Enables button with default text\n    -\u003edenyButtonText('No')\n    -\u003ewithCancelButton()  // Enables button with default text\n    -\u003ecancelButtonText('Cancel')\n    -\u003eshow();\n```\n\n### Button Color\nYou can customize the appearance of buttons by setting their colors using the `confirmButtonColor()`, `cancelButtonColor()`, and `denyButtonColor()` methods. These methods accept a color value (e.g., color names, hex codes, or CSS-compatible strings) to style the respective buttons.\n\n``` php\nLivewireAlert::title('Save?')\n    -\u003econfirmButtonColor('green')\n    -\u003ewithDenyButton('red')\n    -\u003ewithCancelButton('blue')\n    -\u003eshow();\n```\n\n### Button Events\n\nEach button can trigger a corresponding event when clicked, allowing you to handle user interactions in your Livewire component.\n\n#### `onConfirm()`\n\n``` php\nLivewireAlert::title('Save?')\n    -\u003ewithConfirmButton('Save')\n    -\u003eonConfirm('saveData', ['id' =\u003e $this-\u003eitemId])\n    -\u003eshow();\n\npublic function saveData($data)\n{\n    $itemId = $data['id'];\n    // Save logic\n}\n```\n\n#### `onDismiss()`\n\n``` php\nLivewireAlert::title('Delete?')\n    -\u003ewithConfirmButton('Delete')\n    -\u003ewithCancelButton('Keep')\n    -\u003eonDismiss('cancelDelete', ['id' =\u003e $this-\u003eitemId])\n    -\u003eshow();\n\npublic function cancelDelete($data)\n{\n    $itemId = $data['id'];\n    // Cancel logic\n}\n```\n\n#### `onDeny()`\n\n``` php\nLivewireAlert::title('Update?')\n    -\u003ewithConfirmButton('Update')\n    -\u003ewithDenyButton('Discard')\n    -\u003eonDeny('discardChanges', ['id' =\u003e $this-\u003eitemId])\n    -\u003eshow();\n\npublic function discardChanges($data)\n{\n    $itemId = $data['id'];\n    // Discard logic\n}\n```\n\n#### Using them together example\n\n``` php\nLivewireAlert::title('Process File')\n    -\u003etext('What would you like to do?')\n    -\u003equestion()\n    -\u003ewithConfirmButton('Save')\n    -\u003ewithCancelButton('Cancel')\n    -\u003ewithDenyButton('Delete')\n    -\u003eonConfirm('saveFile', ['id' =\u003e $this-\u003efileId])\n    -\u003eonDeny('deleteFile', ['id' =\u003e $this-\u003efileId])\n    -\u003eonDismiss('cancelAction', ['id' =\u003e $this-\u003efileId])\n    -\u003eshow();\n```\n\n### Confirm Dialog\n\nThe `asConfirm()` method configures the alert as a confirmation dialog with a predefined options. It automatically applies a question icon, adds confirm and deny buttons with default text from the configuration, and disables the auto-dismiss timer, making it ideal for scenarios requiring explicit user input.\n\n``` php\nLivewireAlert::title('Are you sure?')\n    -\u003etext('Do you want to proceed with this action?')\n    -\u003easConfirm()\n    -\u003eshow();\n```\n\n#### Handling Events\n\nCombine with `onConfirm()` and `onDeny()` to handle user responses:\n\n``` php\nLivewireAlert::title('Delete Item')\n    -\u003etext('Are you sure you want to delete this item?')\n    -\u003easConfirm()\n    -\u003eonConfirm('deleteItem', ['id' =\u003e $this-\u003eitemId])\n    -\u003eonDeny('keepItem', ['id' =\u003e $this-\u003eitemId])\n    -\u003eshow();\n\npublic function deleteItem($data)\n{\n    $itemId = $data['id'];\n    // Delete logic\n}\n\npublic function keepItem($data)\n{\n    $itemId = $data['id'];\n    // Keep logic\n}\n```\n\n### Inputs\n\nThe LivewireAlert package allows you to add input fields to alerts using the `withOptions()` method, leveraging SweetAlert2’s input capabilities. This is useful for collecting user input (e.g., text, selections) directly within the alert, with the input value returned via event handlers like `onConfirm()`.\n\n#### Usage\nUse `withOptions()` to pass an array containing SweetAlert2 input options. Common input types include `text`, `email`, `password`, `number`, `textarea`, `select`, `radio`, `checkbox`, and `file`.\n\nAdd a simple text input:\n\n``` php\nLivewireAlert::title('Enter Your Name')\n    -\u003ewithOptions([\n        'input' =\u003e 'text',\n        'inputPlaceholder' =\u003e 'Your name here',\n    ])\n    -\u003ewithConfirmButton('Submit')\n    -\u003eonConfirm('saveName')\n    -\u003eshow();\n\npublic function saveName($data)\n{\n    $name = $data['value']; // User’s input from the text field\n    // Save the name\n}\n```\n\nSelect Input Example\n\n``` php\nLivewireAlert::title('Choose an Option')\n    -\u003ewithOptions([\n        'input' =\u003e 'select',\n        'inputOptions' =\u003e [\n            'small' =\u003e 'Small',\n            'medium' =\u003e 'Medium',\n            'large' =\u003e 'Large',\n        ],\n        'inputPlaceholder' =\u003e 'Select a size',\n    ])\n    -\u003ewithConfirmButton('Confirm')\n    -\u003eonConfirm('processSelection')\n    -\u003eshow();\n\npublic function processSelection($data)\n{\n    $size = $data['value']; // Selected value (e.g., 'small')\n    // Process the selection\n}\n```\n\n#### Handling Input Values\nWhen an input is present, the `$data` parameter in event methods (e.g., `onConfirm()`, `onDeny()`) includes a value property containing the user’s input. This value depends on the input type:\n\n- Text, email, password, number, textarea: The entered string or number.\n- Select, radio: The selected option’s key.\n- Checkbox: true or false.\n- File: The file data (if applicable).\n\n### Flash Alert\n\nNeed to flash alerts across requests? In this package you can leverage Laravel’s session flashing alerts and display them in your Livewire components. This feature, inspired by version 3’s simplicity, gives you full freedom to define your own session keys and structure, allowing tailored flash alerts that appear automatically `(e.g., on mount())` after actions like redirects.\n\n``` php\npublic function mount()\n{\n    if (session()-\u003ehas('saved')) {\n        LivewireAlert::title(session('saved.title'))\n            -\u003etext(session('saved.text'))\n            -\u003esuccess()\n            -\u003eshow();\n    }\n}\n\npublic function changesSaved()\n{\n    session()-\u003eflash('saved', [\n        'title' =\u003e 'Changes Saved!',\n        'text' =\u003e 'You can safely close the tab!',\n    ]);\n\n    $this-\u003eredirect('/dashboard');\n}\n```\n\n### Options \n\nThe `withOptions()` method allows you to extend the alert’s configuration with any SweetAlert2-compatible options, giving you full control to customize its appearance, behavior, or functionality beyond the built-in methods. This is ideal for advanced use cases like adding inputs, modifying styles, or setting custom SweetAlert2 features.\n\n``` php\nLivewireAlert::title('Custom Alert')\n    -\u003etext('This alert has a unique style.')\n    -\u003esuccess()\n    -\u003ewithOptions([\n        'width' =\u003e '400px',\n        'background' =\u003e '#f0f0f0',\n        'customClass' =\u003e ['popup' =\u003e 'animate__animated animate__bounceIn'],\n        'allowOutsideClick' =\u003e false,\n    ])\n    -\u003eshow();\n```\n\nFor a comprehensive guide to customization and available configuration options, please refer to the [SweetAlert2](https://sweetalert2.github.io/#configuration) documentation.\n\n### Dependency Injection\nWho said you can only use the Facade? With this package, you can also inject the `Jantinnerezo\\LivewireAlert\\LivewireAlert` class directly into your Livewire component methods via dependency injection. This approach lets you access the alert functionality within the context of your component, offering a clean alternative to the Facade syntax.\n\n``` php\nuse Jantinnerezo\\LivewireAlert\\LivewireAlert;\n\npublic function save(LivewireAlert $alert)\n{\n    $alert-\u003etitle('Success!')\n        -\u003etext('What would you like to do?')\n        -\u003equestion()\n        -\u003ewithConfirmButton('Save')\n        -\u003ewithCancelButton('Cancel')\n        -\u003ewithDenyButton('Delete')\n        -\u003eonConfirm('saveFile', ['id' =\u003e $this-\u003efileId])\n        -\u003eonDeny('deleteFile', ['id' =\u003e $this-\u003efileId])\n        -\u003eonDismiss('cancelAction', ['id' =\u003e $this-\u003efileId])\n        -\u003eshow();\n}\n```\n\nAll methods remain available, and you can chain them fluently just like with the Facade!\n\n### Looking for v3?\n\nIf you’re seeking documentation for livewire-alert v3, note that the last release of the version 3 series was v3.0.3, available on GitHub at [v3.0.3](https://github.com/jantinnerezo/livewire-alert/releases/tag/v3.0.3) Released on March 11, 2024, this version supports Livewire 3 and latest Laravel 12 and includes features like basic alert functionality with the `LivewireAlert` trait.\n\nThis release, however, is a major refactor of the package, introducing a new architecture and enhanced features (like the fluent Facade interface and dependency injection). As a result, the documentation below focuses on v4. \n\nFor v3-specific usage:\n\n``` bash \ncomposer require jantinnerezo/livewire-alert:^3.0\n```\n\nFor ongoing projects, I recommend upgrading to v4.0 to take advantage of the improved API and feature set detailed in this documentation.\n\n## Testing\n``` bash\ncomposer test\n```\n\n## Contributors\n\n\u003ca href=\"https://github.com/jantinnerezo/livewire-alert/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=jantinnerezo/livewire-alert\" width=\"300\" /\u003e\n\u003c/a\u003e\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Security\n\nIf you discover any security related issues, please email erezojantinn@gmail.com instead of using the issue tracker.\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjantinnerezo%2Flivewire-alert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjantinnerezo%2Flivewire-alert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjantinnerezo%2Flivewire-alert/lists"}