Download the PHP package masmerise/livewire-toaster without Composer
On this page you can find all versions of the php package masmerise/livewire-toaster. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package livewire-toaster
Beautiful toast notifications for Livewire
Toaster provides a seamless experience to display toast notifications in your Livewire powered Laravel apps.
Unlike many other toast implementations that are available, Toaster makes it effortless to dispatch a toast notification
from either a standard Controller
or a Livewire Component
. You don't have to think about "flashing" things to the
session or "dispatching browser events" from your Livewire components. Just dispatch your toast and Toaster will route the message accordingly.
Showcase
Compatibility
Livewire | PHP | Laravel |
---|---|---|
| | [LW2](https://laravel-livewire.com/docs/2.x) | [LW3](https://livewire.laravel.com/docs) | |-|-|-| | [1.x](https://github.com/masmerise/livewire-toaster/tree/1.3.0) | ✅ | ❌ | | 2.x | ❌ | ✅ | | | | PHP 8.2 | PHP 8.3 | PHP 8.4 | |-|-|-|-| | 1.0 - ∞ | ✅ | ✅ | ✅ | | | | L10 | L11 | |-|-|-| | 1.0 - 2.1 * | ✅ | ❌ | | 2.2 - ∞ | ❌ | ✅ | |
* feature complete
Contents
Looking for v1 docs? Click here.
- Installation
- Preparing your template
- Configuring scripts
- Tailwind styles
- RTL support
- Usage
- Sending toasts from the back-end
- Sending toasts from the front-end
- Automatic translation of messages
- Accessibility
- Replacing similar toasts
- Suppressing duplicate toasts
- Unit testing
- Extending behavior
- View customization
- Testing
- Changelog
- Security
- Credits
- License
Installation
You can install the package via composer:
You can publish the package's config file:
This is the contents of the toaster.php
config file:
Preparing your template
Next, you'll need to use the <x-toaster-hub />
component in your master template:
Configuring scripts
After that, you'll need to import Toaster
at the top of your resources/js/app.js
bundle to start listening to incoming toasts:
Tailwind styles
[!NOTE] Skip this step if you're going to customize Toaster's default view.
Toaster provides a minimal view that utilizes Tailwind CSS defaults.
If the default toast appearances suffice your needs, you'll need to register it with Tailwind's purge list:
Otherwise, please refer to View customization.
RTL support
[!NOTE] LTR will be assumed regardless of whether you apply the
ltr
attribute or not.
If your app makes use of an RTL language such as Arabic and Hebrew, don't forget to add the rtl
attribute to the document root:
This will make sure the UI elements (such as the close button) are flipped and the text is properly aligned.
Usage
Sending toasts from the back-end
[!NOTE] Toaster supports the dispatch of multiple toasts at once, you are not limited to dispatching a single toast.
Toaster
The standard recommended way for dispatching toast messages is through the Toaster
facade.
If you need fine-grained control, you can always use the PendingToast
class directly to which Toaster
proxies its calls:
Toastable
You can make any class Toastable
to dispatch toasts from:
Redirects
Whenever you return a RedirectResponse
from anywhere in your app, you can chain any of the Toaster
methods
to dispatch a toast message:
This is, of course, not limited to Controller
s as you can also redirect in Livewire Component
s.
Dependency injection
If you'd like to keep things "pure", you can also inject the Collector
contract
and use the ToastBuilder
to dispatch your toasts:
Sending toasts from the front-end
You can invoke the globally available Toaster
instance to dispatch any toast message from anywhere:
Available methods: error
, info
, warning
& success
Automatic translation of messages
[!NOTE] The
translate
configuration value must be set totrue
.
Instead of doing this:
Toaster makes it possible to do this:
You can mix and match without any problems:
You can do whatever you want, whenever you want.
Accessibility
[!NOTE] The
accessibility
configuration value must be set totrue
.
Toaster will add an additional second to a toast's on-screen duration for every 100th word. This way, your users will have enough time to read toasts that are a tad larger than usual.
So, if your base duration value is 3 seconds
and your toast contains 223 words,
the total on-screen duration of the toast will be 3 + 2 = 5 seconds
Replacing similar toasts
[!NOTE] The
replace
configuration value must be set totrue
.[!WARNING] Takes precedence over
suppress
.
Toaster will dispose of any toast that is similar to the one being dispatched prior to displaying the new toast.
A toast is considered similar if it has the same duration
, message
, and type
.
Suppressing duplicate toasts
[!NOTE] The
suppress
configuration value must be set totrue
.
Toaster will prevent the display of duplicate toast messages while another toast with the same message is still on-screen.
A toast is considered a duplicate if it has the same duration
, message
, and type
.
Unit testing
[!NOTE] If you make use of automatic translation of messages, you should assert whether the translation keys are passed along correctly instead of the human readable messages that are replaced by Laravel's translator. Otherwise, your tests are going to fail as the messages are not translated during unit testing.
Toaster provides a couple of testing capabilities in order for you to build a robust application:
Extending behavior
Imagine that you'd like to keep track of how many toasts are dispatched daily to display on an admin dashboard. First, create a new class that encapsulates this logic:
After that, extend the behavior in your AppServiceProvider
:
That's it!
View customization
Even though the default toasts are pretty, they might not fit your design and you may want to customize them.
You can do so by publishing Toaster's views:
The hub.blade.php
view will be published to your application's resources/views/vendor/toaster
directory.
Feel free to modify anything to your liking.
Available viewData
$alignment
- can be used to align the toast container vertically depending on the configuration$closeable
- whether the close button should be rendered by the Blade component$config
- default configuration values, used by the Alpine component$position
- can be used to position the toasts depending on the configuration$toasts
- toasts that were flashed to the session by Toaster, used by the Alpine component
[!WARNING] You must keep the
x-data
andx-init
directives and you must keep using thex-for
loop. Otherwise, the Alpine component that powers Toaster will start malfunctioning.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Muhammed Sari
- Greg Korba
- All Contributors
License
The MIT License (MIT). Please see License File for more information.