Download the PHP package mattlibera/livewire-flash without Composer
On this page you can find all versions of the php package mattlibera/livewire-flash. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mattlibera/livewire-flash
More information about mattlibera/livewire-flash
Files in mattlibera/livewire-flash
Package livewire-flash
Short Description Flash notifications using Livewire
License MIT
Informations about the package livewire-flash
Livewire Flash
This package provides flash message capability using Laravel Livewire. It is based very literally on laracasts/flash
but has been extended to add the ability to flash a message to a the flash container (a Livewire component) without reloading the page.
This package also retains much (though not all) of the same capability for "normal" flash messages, which are displayed on refresh by the same Livewire component.
Installation
Install via composer:
Requirements
- Laravel >=7.0
- Livewire ^1.2 or ^2.0
For new applications, consider using the TALL preset for Laravel: [https://github.com/laravel-frontend-presets/tall], or this package also works well with Laravel Jetstream: [https://jetstream.laravel.com/]
Recommended add-ons
Out of the box, the default alert component uses:
- TailwindCSS
- FontAwesome
However, it's fairly trivial to implement your own views / styles instead, by publishing the config and overriding defaults. See below for more on that.
Tailwind setup
If you are using Tailwind CSS, you'll likely want to amend the content
section of your tailwind.config.js
to include the appropriate files from this package:
Of course, if you publsh the views/config, you'll reference your own copies instead.
Usage
Normal flash messages (on reload)
Call the flash()
helper from your code somewhere, before you redirect.
Livewire flash message (before reload)
From your Livewire component, flash your message using the normal syntax, but then call the livewire()
helper method as the last method in the chain. You must pass in $this
as the argument, as this package utilizes the emit
helper that exists on all Livewire components. Example:
Message types
Message types are defined in the livewire-flash.php
config file, which can be published (see below) if desired. By default, there are four supported message types: info
(default if nothing else is specified), success
, warning
, and error
.
To set a message's type, either:
- Pass it as the second argument to
flash()
- example:flash('Your action succeeded', 'success')
, or - Chain it as a method name fluently after
flash()
- example:flash('Your action succeeded')->success()
Both of those will change the message's display (colors and icon) to the configured styles.
Overlay Message
Overlay message is defined in the livewire-flash.php
config file, which can be published (see below) if desired.
To set an overlay message, chain the method name overlay()
after flash()
. When using overlay leave the flash()
parameter empty. Enter your message as the first parameter and title as second parameter for overlay()
. This can be used with or without the livewire($this)
suffix:
Note that the out-of-the-box overlay component does support HTML code for the body and title, using the Blade unescaped {!! !!}
tags.
Customization
To change the styles used by each message type, OR to add your own types, first publish the config file:
Then, in the styles
key you can change whatever you want:
Or you can add your own:
Whatever the case, just ensure that you call the alert by its config key: flash('An important message')->notice()
To customize overlay styles, see the overlay
key of the config file.
Templates
Out of the box, the Livewire Flash Container component is registered for you. All you have to do is include it in your template:
There are also some sample alert components (styled using TailwindCSS) included with this package. However, if you do not wish to use those...
Customization
You can change the views that the Livewire components use for rendering, and the styles applied to each message type.
If you are not using TailwindCSS and/or FontAwesome, you should definitely do this to call your own alert component/partial to fit whatever your stack is using.
First, publish the config file:
Then, edit the views
area:
You can access the public message properties on MattLibera\LivewireFlash\Message
, as well as $styles
(which is injected via the Livewire component) in your template.
Dismissable Messages
By default, each message will be set to be dismissable (that is, have an X icon at the right that will close the alert). If you wish to prevent this, you can chain ->notDismissable()
(or ->dismissable(false)
) to your flash directive.
You can add your own magic via AlpineJS or whatever else if you want to fade messages out automatically - right now each message is a Livewire component and uses Livewire logic to hide it when it is dismissed.
Note that the overlay does not support this directive.
Multiple Flash Messages
Multiple flash messages can be sent to the session:
OR
However, at the moment, because of the way Livewire handles the session, you cannot mix-and-match... that is, you cannot do:
Contributing
I am open to contributions to this package, and will do the best I can to maintain it over time. Pull requests are welcome, and in fact encouraged. Right now there are no specific guidelines for a PR.
Road Map
Some considerations for future versions:
- Fluent options for setting an icon or colors on the fly
- Auto-dismissing option for flash messages
Credits and License
Credit for the original package goes to Jeffrey Way and Laracasts. Additional thanks:
- Caleb Porzio and his Livewire contributors for the awesome framework
- Adam Wathan and the Tailwind crew
- Taylor Otwell and co. for Laravel
This is an MIT-licensed package. Please read license.md for the details.
All versions of livewire-flash with dependencies
illuminate/support Version ^7.0|^8.0|^9.0|^10.0|^11.0
livewire/livewire Version ^1.2|^2.12.7|^3.5.2