From 8994a0f36cefbe3c1e90c6d9a128aa41065a368e Mon Sep 17 00:00:00 2001 From: joeknex <6383286+joeknex@users.noreply.github.com> Date: Sun, 24 May 2020 04:05:08 +0100 Subject: [PATCH] Updated Readme.md file. Added error(), primary(), secondary(), light() and dark() themes. Added dismissible() and fixed() options. Added alert.blade.php file. --- readme.md | 152 +++++++++++++------------- src/Laracasts/Flash/FlashNotifier.php | 95 ++++++++++++++-- src/Laracasts/Flash/Message.php | 14 +++ src/views/alert.blade.php | 15 +++ src/views/message.blade.php | 22 ++-- src/views/modal.blade.php | 11 +- 6 files changed, 205 insertions(+), 104 deletions(-) create mode 100644 src/views/alert.blade.php diff --git a/readme.md b/readme.md index a47bea0..871fe1c 100644 --- a/readme.md +++ b/readme.md @@ -2,6 +2,8 @@ This composer package offers a Twitter Bootstrap optimized flash messaging setup for your Laravel applications. +> [Learn exactly how to build this very package on Laracasts!](https://laracasts.com/lessons/flexible-flash-messages) + ## Installation Begin by pulling in the package through Composer. @@ -10,15 +12,17 @@ Begin by pulling in the package through Composer. composer require laracasts/flash ``` -Next, as noted above, the default CSS classes for your flash message are optimized for Twitter Bootstrap. As such, either pull in the Bootstrap's CSS within your HTML or layout file, or write your own CSS based on these classes. +Next, as noted above, the default CSS classes for your flash message are optimized for Twitter Bootstrap. As such, either pull in the Bootstrap's CSS within your HTML or layout file, or write your own CSS based on these classes: ```html - + ``` +(See https://getbootstrap.com/docs/ for the latest CSS version) + ## Usage -Within your controllers, before you perform a redirect, make a call to the `flash()` function. +Within your controllers, before you perform a redirect, make a call to the `flash()` function: ```php public function store() @@ -29,109 +33,109 @@ public function store() } ``` -You may also do: +You may also append to the function with the following actions: + +Flash Alert Themes: - `flash('Message')->success()`: Set the flash theme to "success". -- `flash('Message')->error()`: Set the flash theme to "danger". - `flash('Message')->warning()`: Set the flash theme to "warning". -- `flash('Message')->overlay()`: Render the message as an overlay. -- `flash()->overlay('Modal Message', 'Modal Title')`: Display a modal overlay with a title. -- `flash('Message')->important()`: Add a close button to the flash message. -- `flash('Message')->error()->important()`: Render a "danger" flash message that must be dismissed. +- `flash('Message')->danger()` / `flash('Message')->error()`: Set the flash theme to "danger". +- `flash('Message')->info()`: Set the flash theme to "info". +- `flash('Message')->primary()`: Set the flash theme to "primary". +- `flash('Message')->secondary()`: Set the flash theme to "secondary". +- `flash('Message')->light()`: Set the flash theme to "light". +- `flash('Message')->dark()`: Set the flash theme to "dark". -With this message flashed to the session, you may now display it in your view(s). Because flash messages and overlays are so common, we provide a template out of the box to get you started. You're free to use - and even modify to your needs - this template how you see fit. +Flash Alert Dismissible Options: -```html -@include('flash::message') -``` +- `flash('Message')->dismissible()`: Add a close button to the flash message. +- `flash('Message')->important()`: Add a close button to the flash message and prevent auto hiding (if in combination with Javascript). +- `flash('Message')->fixed()`: Stops the flash message from ever being closed (Overrides `dismissible()` and `important()`). -## Example +Render as Overlay Modal: -```html - - - - - Document - - - +- `flash('Message')->overlay()`: Render the message as an overlay modal. +- `flash()->overlay('Modal Message', 'Modal Title')`: Render the message as an overlay modal with a title. -
- @include('flash::message') +**TIP** You can combine actions: -

Welcome to my website...

-
+- `flash('Message')->error()->important()`: Render a "danger" flash message that must be dismissed. - - - +With this message flashed to the session, you may now display it in your view(s). Because flash messages and overlays are so common, we provide a template out of the box to get you started. You're free to use and even modify this template how you see fit: - - - - +```html +@include('flash::message') ``` -If you need to modify the flash message partials, you can run: +### Modal Alerts Javascript -```bash -php artisan vendor:publish --provider="Laracasts\Flash\FlashServiceProvider" +For overlay modal alerts add the following Javascript: + +```html + ``` -The two package views will now be located in the `resources/views/vendor/flash/` directory. +### Auto Hiding Flash Messages Javascript -```php -flash('Welcome Aboard!'); +For auto hiding flash messages you can write a simple bit of JavaScript. For example, using jQuery, you might add the following snippet just before the closing `` tag: -return home(); +```html + ``` -![https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/message.png](https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/message.png) +This will find any alerts (excluding the `->important()` ones) which should remain visible until manually closed by the user, wait three seconds, and then fade out. -```php -flash('Sorry! Please try again.')->error(); +### Multiple Flash Messages -return home(); -``` - -![https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/error.png](https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/error.png) +If you need to flash multiple alerts do the following: ```php -flash()->overlay('You are now a Laracasts member!', 'Yay'); +flash('Message 1')->warning(); +flash('Message 2')->error()->important(); +flash('Message 3')->info(); -return home(); +return redirect('somewhere'); ``` -![https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/overlay.png](https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/overlay.png) - -> [Learn exactly how to build this very package on Laracasts!](https://laracasts.com/lessons/flexible-flash-messages) - -## Hiding Flash Messages - -A common desire is to display a flash message for a few seconds, and then hide it. To handle this, write a simple bit of JavaScript. For example, using jQuery, you might add the following snippet just before the closing `` tag. +## Example ```html - + + + + + Document + + + +
+ @include('flash::message') + +

Welcome to my website...

+
+ + + + + + + + ``` -This will find any alerts - excluding the important ones, which should remain until manually closed by the user - wait three seconds, and then fade them out. +## Modify Flash Message Partials -## Multiple Flash Messages - -Need to flash multiple flash messages to the session? No problem. - -```php -flash('Message 1'); -flash('Message 2')->important(); +If you need to modify the flash message partials, you can run: -return redirect('somewhere'); +```bash +php artisan vendor:publish --provider="Laracasts\Flash\FlashServiceProvider" ``` -Done! You'll now see two flash messages upon redirect. - - +The two package views will now be located in the `resources/views/vendor/flash/` directory. diff --git a/src/Laracasts/Flash/FlashNotifier.php b/src/Laracasts/Flash/FlashNotifier.php index d581d5d..7b59c73 100644 --- a/src/Laracasts/Flash/FlashNotifier.php +++ b/src/Laracasts/Flash/FlashNotifier.php @@ -34,29 +34,29 @@ function __construct(SessionStore $session) } /** - * Flash an information message. + * Flash a success style message. * * @param string|null $message * @return $this */ - public function info($message = null) + public function success($message = null) { - return $this->message($message, 'info'); + return $this->message($message, 'success'); } /** - * Flash a success message. + * Flash a warning style message. * * @param string|null $message * @return $this */ - public function success($message = null) + public function warning($message = null) { - return $this->message($message, 'success'); + return $this->message($message, 'warning'); } /** - * Flash an error message. + * Flash a error style message. * * @param string|null $message * @return $this @@ -67,14 +67,69 @@ public function error($message = null) } /** - * Flash a warning message. + * Flash a danger style message. * * @param string|null $message * @return $this */ - public function warning($message = null) + public function danger($message = null) { - return $this->message($message, 'warning'); + return $this->message($message, 'danger'); + } + + /** + * Flash a information style message. + * + * @param string|null $message + * @return $this + */ + public function info($message = null) + { + return $this->message($message, 'info'); + } + + /** + * Flash a primary style message. + * + * @param string|null $message + * @return $this + */ + public function primary($message = null) + { + return $this->message($message, 'primary'); + } + + /** + * Flash a secondary style message. + * + * @param string|null $message + * @return $this + */ + public function secondary($message = null) + { + return $this->message($message, 'secondary'); + } + + /** + * Flash an light style message. + * + * @param string|null $message + * @return $this + */ + public function light($message = null) + { + return $this->message($message, 'light'); + } + + /** + * Flash a dark style message. + * + * @param string|null $message + * @return $this + */ + public function dark($message = null) + { + return $this->message($message, 'dark'); } /** @@ -142,6 +197,26 @@ public function important() return $this->updateLastMessage(['important' => true]); } + /** + * Add an "dismissible" flash to the session. + * + * @return $this + */ + public function dismissible() + { + return $this->updateLastMessage(['dismissible' => true]); + } + + /** + * Add an "fixed" flash to the session. + * + * @return $this + */ + public function fixed() + { + return $this->updateLastMessage(['fixed' => true]); + } + /** * Clear all registered messages. * diff --git a/src/Laracasts/Flash/Message.php b/src/Laracasts/Flash/Message.php index dafda52..1fb95b2 100644 --- a/src/Laracasts/Flash/Message.php +++ b/src/Laracasts/Flash/Message.php @@ -32,6 +32,20 @@ class Message implements \ArrayAccess */ public $important = false; + /** + * Whether the message should be dismissible. + * + * @var bool + */ + public $dismissible = false; + + /** + * Whether the message should be fixed. + * + * @var bool + */ + public $fixed = false; + /** * Whether the message is an overlay. * diff --git a/src/views/alert.blade.php b/src/views/alert.blade.php new file mode 100644 index 0000000..cfb0453 --- /dev/null +++ b/src/views/alert.blade.php @@ -0,0 +1,15 @@ + \ No newline at end of file diff --git a/src/views/message.blade.php b/src/views/message.blade.php index 7d8af13..4341c35 100644 --- a/src/views/message.blade.php +++ b/src/views/message.blade.php @@ -6,21 +6,13 @@ 'body' => $message['message'] ]) @else - + @include('flash::alert', [ + 'level' => $message['level'], + 'message' => $message['message'], + 'dismissible' => $message['dismissible'], + 'fixed' => $message['fixed'], + 'important' => $message['important'] + ]) @endif @endforeach diff --git a/src/views/modal.blade.php b/src/views/modal.blade.php index 020f2f4..6dbfc53 100644 --- a/src/views/modal.blade.php +++ b/src/views/modal.blade.php @@ -1,10 +1,11 @@ -