Skip to content

Commit

Permalink
Merge pull request #24 from /issues/19
Browse files Browse the repository at this point in the history
Use namespace for components instead of prefix
  • Loading branch information
bilfeldt committed Dec 16, 2022
2 parents af4109a + ee6d1a1 commit b7c7ca9
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 37 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,3 +5,17 @@ All notable changes to `laravel-flash-message` will be documented in this file.
## 1.0.0 - 202X-XX-XX

- initial release

### Breaking changes

Blade component are now namespaced instead of prefixed. This means that usage of the components should be changed from the old prefix syntax

```blade
<x-flash-alert ... /> // no longer supported
```

to the new namespace syntax

```blade
<x-flash::alert ... /> // new syntax
```
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -120,7 +120,7 @@ Messages can be adding bacially anywhere in the codebase using the `View` facade
Once messages have been passed to the frontend these can be shown by simply using the following component in any view file:

```php
<x-flash-messages />
<x-flash::messages />
```

The above will display messages from the `default` bag - see below for details when using multiple bags.
Expand All @@ -138,16 +138,16 @@ return redirect('/posts')->withMessage($message, 'bag-name');
and when displaying the messages simply pass the bag name as well:

```php
<x-flash-messages bag='bag-name' />
<x-flash::messages bag='bag-name' />
```

## Tip

You might have a layout where you would always like to flash the messages above the main content or just below the title. You can simply add the `<x-flash-messages />` to your layout file in the place you would like the messages to show and that way you do not need to call this in multiple views. In order to avoid issues with the `$messages` not being set in case the `ShareMessagesFromSession` has not been applied then it advised to show the message like so:
You might have a layout where you would always like to flash the messages above the main content or just below the title. You can simply add the `<x-flash::messages />` to your layout file in the place you would like the messages to show and that way you do not need to call this in multiple views. In order to avoid issues with the `$messages` not being set in case the `ShareMessagesFromSession` has not been applied then it advised to show the message like so:

```php
@isset($messages)
<x-flash-messages />
<x-flash::messages />
@endif
```

Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/messages.blade.php
@@ -1,5 +1,5 @@
@foreach($messages->getBag($bag) as $message)
<x-flash-alert
<x-flash::alert
:level="$message->getLevel()"
:text="$message->getText()"
:title="$message->getTitle()"
Expand Down
27 changes: 16 additions & 11 deletions src/FlashMessageServiceProvider.php
Expand Up @@ -10,6 +10,7 @@
use Bilfeldt\LaravelFlashMessage\View\Components\AlertWarning;
use Bilfeldt\LaravelFlashMessage\View\Components\Messages;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Blade;
use Illuminate\View\Factory;
use Illuminate\View\View;
use Spatie\LaravelPackageTools\Package;
Expand All @@ -29,21 +30,25 @@ public function configurePackage(Package $package): void
$package
->name('laravel-flash-message')
->hasConfigFile()
->hasViews() // required for the view component blade files to be registered
->hasViewComponents(
self::VIEW_COMPONENT_NAMESPACE,
Messages::class,
Alert::class,
AlertError::class,
AlertInfo::class,
AlertMessage::class,
AlertSuccess::class,
AlertWarning::class
);
->hasViews(); // required for the view component blade files to be registered
// The package does not allow for namespaces (`<x-namespace:component />`) but only prefixes (`<x-prefix-component />`
// so we register those manually using `componentNamespace()`
//->hasViewComponents(
// self::VIEW_COMPONENT_NAMESPACE,
// Messages::class,
// Alert::class,
// AlertError::class,
// AlertInfo::class,
// AlertMessage::class,
// AlertSuccess::class,
// AlertWarning::class
//);
}

public function packageBooted()
{
Blade::componentNamespace('Bilfeldt\\LaravelFlashMessage\\View\\Components', self::VIEW_COMPONENT_NAMESPACE);

// This is used when adding a message from a controller: view('posts-index')->withMessage(...)
View::macro('withMessage', function (Message $message, string $bag = 'default'): View {
/** @var ViewFlashMessageBag $viewFlashMessageBag */
Expand Down
8 changes: 4 additions & 4 deletions tests/AlertErrorTest.php
Expand Up @@ -73,7 +73,7 @@ public function test_component_with_links(): void
public function test_can_render_with_only_text(): void
{
$view = $this->blade(
'<x-flash-alert-error :text="$text" />',
'<x-flash::alert-error :text="$text" />',
['text' => 'Example text']
);

Expand All @@ -84,7 +84,7 @@ public function test_can_render_with_only_text(): void

public function test_can_render_with_title(): void
{
$view = $this->blade('<x-flash-alert-error :text="$text" :title="$title" />', [
$view = $this->blade('<x-flash::alert-error :text="$text" :title="$title" />', [
'text' => 'Example text',
'title' => 'Example Title',
]);
Expand All @@ -97,7 +97,7 @@ public function test_can_render_with_title(): void

public function test_can_render_with_messages(): void
{
$view = $this->blade('<x-flash-alert-error :text="$text" :messages="$messages" />', [
$view = $this->blade('<x-flash::alert-error :text="$text" :messages="$messages" />', [
'text' => 'Example text',
'messages' => [
'field_1' => 'Example message 1',
Expand All @@ -114,7 +114,7 @@ public function test_can_render_with_messages(): void

public function test_can_render_with_links(): void
{
$view = $this->blade('<x-flash-alert-error :text="$text" :links="$links" />', [
$view = $this->blade('<x-flash::alert-error :text="$text" :links="$links" />', [
'text' => 'Example text',
'links' => [
'Link 1' => 'https://example.com/test1',
Expand Down
8 changes: 4 additions & 4 deletions tests/AlertInfoTest.php
Expand Up @@ -73,7 +73,7 @@ public function test_component_with_links(): void
public function test_can_render_with_only_text(): void
{
$view = $this->blade(
'<x-flash-alert-info :text="$text" />',
'<x-flash::alert-info :text="$text" />',
['text' => 'Example text']
);

Expand All @@ -84,7 +84,7 @@ public function test_can_render_with_only_text(): void

public function test_can_render_with_title(): void
{
$view = $this->blade('<x-flash-alert-info :text="$text" :title="$title" />', [
$view = $this->blade('<x-flash::alert-info :text="$text" :title="$title" />', [
'text' => 'Example text',
'title' => 'Example Title',
]);
Expand All @@ -97,7 +97,7 @@ public function test_can_render_with_title(): void

public function test_can_render_with_messages(): void
{
$view = $this->blade('<x-flash-alert-info :text="$text" :messages="$messages" />', [
$view = $this->blade('<x-flash::alert-info :text="$text" :messages="$messages" />', [
'text' => 'Example text',
'messages' => [
'field_1' => 'Example message 1',
Expand All @@ -114,7 +114,7 @@ public function test_can_render_with_messages(): void

public function test_can_render_with_links(): void
{
$view = $this->blade('<x-flash-alert-info :text="$text" :links="$links" />', [
$view = $this->blade('<x-flash::alert-info :text="$text" :links="$links" />', [
'text' => 'Example text',
'links' => [
'Link 1' => 'https://example.com/test1',
Expand Down
8 changes: 4 additions & 4 deletions tests/AlertMessageTest.php
Expand Up @@ -73,7 +73,7 @@ public function test_component_with_links(): void
public function test_can_render_with_only_text(): void
{
$view = $this->blade(
'<x-flash-alert-message :text="$text" />',
'<x-flash::alert-message :text="$text" />',
['text' => 'Example text']
);

Expand All @@ -84,7 +84,7 @@ public function test_can_render_with_only_text(): void

public function test_can_render_with_title(): void
{
$view = $this->blade('<x-flash-alert-message :text="$text" :title="$title" />', [
$view = $this->blade('<x-flash::alert-message :text="$text" :title="$title" />', [
'text' => 'Example text',
'title' => 'Example Title',
]);
Expand All @@ -97,7 +97,7 @@ public function test_can_render_with_title(): void

public function test_can_render_with_messages(): void
{
$view = $this->blade('<x-flash-alert-message :text="$text" :messages="$messages" />', [
$view = $this->blade('<x-flash::alert-message :text="$text" :messages="$messages" />', [
'text' => 'Example text',
'messages' => [
'field_1' => 'Example message 1',
Expand All @@ -114,7 +114,7 @@ public function test_can_render_with_messages(): void

public function test_can_render_with_links(): void
{
$view = $this->blade('<x-flash-alert-message :text="$text" :links="$links" />', [
$view = $this->blade('<x-flash::alert-message :text="$text" :links="$links" />', [
'text' => 'Example text',
'links' => [
'Link 1' => 'https://example.com/test1',
Expand Down
8 changes: 4 additions & 4 deletions tests/AlertSuccessTest.php
Expand Up @@ -73,7 +73,7 @@ public function test_component_with_links(): void
public function test_can_render_with_only_text(): void
{
$view = $this->blade(
'<x-flash-alert-success :text="$text" />',
'<x-flash::alert-success :text="$text" />',
['text' => 'Example text']
);

Expand All @@ -84,7 +84,7 @@ public function test_can_render_with_only_text(): void

public function test_can_render_with_title(): void
{
$view = $this->blade('<x-flash-alert-success :text="$text" :title="$title" />', [
$view = $this->blade('<x-flash::alert-success :text="$text" :title="$title" />', [
'text' => 'Example text',
'title' => 'Example Title',
]);
Expand All @@ -97,7 +97,7 @@ public function test_can_render_with_title(): void

public function test_can_render_with_messages(): void
{
$view = $this->blade('<x-flash-alert-success :text="$text" :messages="$messages" />', [
$view = $this->blade('<x-flash::alert-success :text="$text" :messages="$messages" />', [
'text' => 'Example text',
'messages' => [
'field_1' => 'Example message 1',
Expand All @@ -114,7 +114,7 @@ public function test_can_render_with_messages(): void

public function test_can_render_with_links(): void
{
$view = $this->blade('<x-flash-alert-success :text="$text" :links="$links" />', [
$view = $this->blade('<x-flash::alert-success :text="$text" :links="$links" />', [
'text' => 'Example text',
'links' => [
'Link 1' => 'https://example.com/test1',
Expand Down
2 changes: 1 addition & 1 deletion tests/AlertTest.php
Expand Up @@ -36,7 +36,7 @@ public function test_can_show_message(string $level): void
*/
public function test_can_render_message(string $level): void
{
$view = $this->blade('<x-flash-alert :level="$level" :text="$text" />', [
$view = $this->blade('<x-flash::alert :level="$level" :text="$text" />', [
'level' => $level,
'text' => 'Example text',
]);
Expand Down
8 changes: 4 additions & 4 deletions tests/AlertWarningTest.php
Expand Up @@ -73,7 +73,7 @@ public function test_component_with_links(): void
public function test_can_render_with_only_text(): void
{
$view = $this->blade(
'<x-flash-alert-warning :text="$text" />',
'<x-flash::alert-warning :text="$text" />',
['text' => 'Example text']
);

Expand All @@ -84,7 +84,7 @@ public function test_can_render_with_only_text(): void

public function test_can_render_with_title(): void
{
$view = $this->blade('<x-flash-alert-warning :text="$text" :title="$title" />', [
$view = $this->blade('<x-flash::alert-warning :text="$text" :title="$title" />', [
'text' => 'Example text',
'title' => 'Example Title',
]);
Expand All @@ -97,7 +97,7 @@ public function test_can_render_with_title(): void

public function test_can_render_with_messages(): void
{
$view = $this->blade('<x-flash-alert-warning :text="$text" :messages="$messages" />', [
$view = $this->blade('<x-flash::alert-warning :text="$text" :messages="$messages" />', [
'text' => 'Example text',
'messages' => [
'field_1' => 'Example message 1',
Expand All @@ -114,7 +114,7 @@ public function test_can_render_with_messages(): void

public function test_can_render_with_links(): void
{
$view = $this->blade('<x-flash-alert-warning :text="$text" :links="$links" />', [
$view = $this->blade('<x-flash::alert-warning :text="$text" :links="$links" />', [
'text' => 'Example text',
'links' => [
'Link 1' => 'https://example.com/test1',
Expand Down

0 comments on commit b7c7ca9

Please sign in to comment.