Skip to content

[9.x] Migrate mail components - #44527

Merged
taylorotwell merged 11 commits into
laravel:9.xfrom
timacdonald:mail-component-migration
Oct 10, 2022
Merged

[9.x] Migrate mail components#44527
taylorotwell merged 11 commits into
laravel:9.xfrom
timacdonald:mail-component-migration

Conversation

@timacdonald

@timacdonald timacdonald commented Oct 10, 2022

Copy link
Copy Markdown
Member

This PR allows mail components to be used with the <x-component> syntax.

These two are now identical:

@component('mail::button', ['url' => 'https://forge.laravel.com'])
Deploy with Forge
@endcomponent

<x-mail::button url="https://forge.laravel.com">
Deploy with Forge
</x-mail>

Below are some of the test views I've used to ensure things are working as expected. You may like to fire these up in a standalone Mailable to test out.

<x-mail::message>

# TEST: @@component('mail::button', ['url' => ''])

@component('mail::button', ['url' => ''])
    slot content
@endcomponent

# TEST: &lt;x-mail::button url="">

<x-mail::button url="">
    slot content
</x-mail::button>

# TEST: @@component('mail::button', ['url' => 'https://forge.laravel.com'])

@component('mail::button', ['url' => 'https://forge.laravel.com'])
    slot content
@endcomponent

# TEST: &lt;x-mail::button url="https://vapor.laravel.com">

<x-mail::button url="https://vapor.laravel.com">
    slot content
</x-mail::button>

# TEST: @@component('mail::button', ['url' => 'https://forge.laravel.com', 'color' => 'success'])

@component('mail::button', ['url' => 'https://forge.laravel.com', 'color' => 'success'])
    slot content
@endcomponent

# TEST: &lt;x-mail::button url="https://vapor.laravel.com" color="success">

<x-mail::button url="https://vapor.laravel.com" color="success">
    slot content
</x-mail::button>

# TEST: @@component('mail::table')

<style> .table { background-color: palegoldenrod } </style>

@component('mail::table')
Table content
@endcomponent

# TEST: &lt;x-mail::table&gt;

<x-mail::table>
Table content
</x-mail::table>

# TEST: @@component('mail::subcopy')

@component('mail::subcopy')
## Slot content
@endcomponent

# TEST: &lt;x-mail::subcopy&gt;

<x-mail::subcopy>
## Slot content
</x-mail::subcopy>

# TEST: @@component('mail::panel')

@component('mail::panel')
## Slot content
@endcomponent

# TEST: &lt;x-mail::panel&gt;

<x-mail::panel>
## Slot content
</x-mail::panel>

# TEST: @@component('mail::header', ['url' => 'https://forge.laravel.com']) Laravel @@endcomponent

<table>
@component('mail::header', ['url' => 'https://forge.laravel.com'])
Laravel
@endcomponent
</table>

# TEST: &lt;x-mail::header url="https://vapor.laravel.com"&gt; Laravel &lt;/x-mail::header&gt;

<table>
<x-mail::header url="https://vapor.laravel.com">Laravel</x-mail::header>
</table>

# TEST: @@component('mail::header', ['url' => 'https://forge.laravel.com']) Acme @@endcomponent

<table>
@component('mail::header', ['url' => 'https://forge.laravel.com'])
Acme
@endcomponent
</table>

# TEST: &lt;x-mail::header url="https://vapor.laravel.com"&gt; Acme &lt;/x-mail::header&gt;

<table>
<x-mail::header url="https://vapor.laravel.com">Acme</x-mail::header>
</table>

# TEST: @@component('mail::footer')

<table>
@component('mail::footer', ['url' => 'https://forge.laravel.com'])
## Slot content
@endcomponent
</table>

# TEST: &lt;x-mail::footer&gt;

<table>
<x-mail::footer url="https://vapor.laravel.com">
## Slot content
</x-mail::footer>
</table>

# TEST: Pass slots instead of props...

@component('mail::button')
@slot('url')
https://forge.laravel.com
@endslot
slot content
@endcomponent

<x-mail::button>
<x-slot:url>
https://forge.laravel.com
</x-slot:url>
slot content
</x-mail::button>

</x-mail::message>
@component('mail::message')

# TEST: @@component('mail::button', ['url' => ''])

@component('mail::button', ['url' => ''])
    slot content
@endcomponent

# TEST: &lt;x-mail::button url="">

<x-mail::button url="">
    slot content
</x-mail::button>

# TEST: @@component('mail::button', ['url' => 'https://forge.laravel.com'])

@component('mail::button', ['url' => 'https://forge.laravel.com'])
    slot content
@endcomponent

# TEST: &lt;x-mail::button url="https://vapor.laravel.com">

<x-mail::button url="https://vapor.laravel.com">
    slot content
</x-mail::button>

# TEST: @@component('mail::button', ['url' => 'https://forge.laravel.com', 'color' => 'success'])

@component('mail::button', ['url' => 'https://forge.laravel.com', 'color' => 'success'])
    slot content
@endcomponent

# TEST: &lt;x-mail::button url="https://vapor.laravel.com" color="success">

<x-mail::button url="https://vapor.laravel.com" color="success">
    slot content
</x-mail::button>

# TEST: @@component('mail::table')

<style> .table { background-color: palegoldenrod } </style>

@component('mail::table')
Table content
@endcomponent

# TEST: &lt;x-mail::table&gt;

<x-mail::table>
Table content
</x-mail::table>

# TEST: @@component('mail::subcopy')

@component('mail::subcopy')
## Slot content
@endcomponent

# TEST: &lt;x-mail::subcopy&gt;

<x-mail::subcopy>
## Slot content
</x-mail::subcopy>

# TEST: @@component('mail::panel')

@component('mail::panel')
## Slot content
@endcomponent

# TEST: &lt;x-mail::panel&gt;

<x-mail::panel>
## Slot content
</x-mail::panel>

# TEST: @@component('mail::header', ['url' => 'https://forge.laravel.com']) Laravel @@endcomponent

<table>
@component('mail::header', ['url' => 'https://forge.laravel.com'])
Laravel
@endcomponent
</table>

# TEST: &lt;x-mail::header url="https://vapor.laravel.com"&gt; Laravel &lt;/x-mail::header&gt;

<table>
<x-mail::header url="https://vapor.laravel.com">Laravel</x-mail::header>
</table>

# TEST: @@component('mail::header', ['url' => 'https://forge.laravel.com']) Acme @@endcomponent

<table>
@component('mail::header', ['url' => 'https://forge.laravel.com'])
Acme
@endcomponent
</table>

# TEST: &lt;x-mail::header url="https://vapor.laravel.com"&gt; Acme &lt;/x-mail::header&gt;

<table>
<x-mail::header url="https://vapor.laravel.com">Acme</x-mail::header>
</table>

# TEST: @@component('mail::footer')

<table>
@component('mail::footer', ['url' => 'https://forge.laravel.com'])
## Slot content
@endcomponent
</table>

# TEST: &lt;x-mail::footer&gt;

<table>
<x-mail::footer url="https://vapor.laravel.com">
## Slot content
</x-mail::footer>
</table>

# TEST: Pass slots instead of props...

@component('mail::button')
@slot('url')
https://forge.laravel.com
@endslot
slot content
@endcomponent

<x-mail::button>
<x-slot:url>
https://forge.laravel.com
</x-slot:url>
slot content
</x-mail::button>

@endcomponent

@timacdonald timacdonald changed the title Migrate mail component [9.x] Migrate mail components Oct 10, 2022
Comment on lines -152 to +153
return "<?php foreach(\$attributes->onlyProps{$expression} as \$__key => \$__value) {
return "<?php \$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag; ?>
<?php foreach(\$attributes->onlyProps{$expression} as \$__key => \$__value) {

@timacdonald timacdonald Oct 10, 2022

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures that the older style syntax works can show views that use the @props directive. In place for BC.

Comment on lines +315 to +317
if (Str::startsWith($component, 'mail::') && $viewFactory->exists($component)) {
return $component;
}

@timacdonald timacdonald Oct 10, 2022

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've given special treatment to mail:: prefixed components, as they are given special treatment in the framework.

This is explicitly performed after the loop to ensure it does not change current functionality in anyway.

@timacdonald

Copy link
Copy Markdown
Member Author

Documentation: laravel/docs#8284

@timacdonald
timacdonald marked this pull request as ready for review October 10, 2022 05:47
@taylorotwell
taylorotwell merged commit 5fb31a5 into laravel:9.x Oct 10, 2022
@DougSisk

Copy link
Copy Markdown

I'm now getting an exception when running view:cache:

  InvalidArgumentException 

  Unable to locate a class or view for component [mail::message].

  at vendor/laravel/framework/src/Illuminate/View/Compilers/ComponentTagCompiler.php:319

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants