Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Properly Inherit Parent Attributes #32576

Merged
merged 3 commits into from
Apr 28, 2020
Merged

Conversation

taylorotwell
Copy link
Member

@taylorotwell taylorotwell commented Apr 28, 2020

Imagine you have two components: button and danger-button, where danger-button contains an instance of button with some additional classes. You are not able to proxy down any custom attributes to the button component using $attributes. For example, this will not work:

<!-- danger-button.blade.php -->

<x-button class="bg-red-200" {{ $attributes }}>
    {{ $slot }}
</x-button>

This also will not work as expected since the button component will end up with an attribute named attribute:

<!-- danger-button.blade.php -->

<x-button class="bg-red-200" :attributes="$attributes">
    {{ $slot }}
</x-button>

This PR fixes this behavior by detecting any bound attribute bags and merging any other classes with them before setting them as the parent's attribute bag. So, the syntax above using :attributes="$attributes" will be handled properly.

It would be possible to handle the {{ $attributes }} passing syntax using some regular expressions to translate that syntax to :attributes="$attributes" for custom Blade tags but will leave that to someone with more regular expression knowledge.

Sending this to master since developers may already be working around this in more cumbersome ways that would be broken by this PR.

@taylorotwell taylorotwell merged commit 7d27e54 into master Apr 28, 2020
@taylorotwell
Copy link
Member Author

Test

@driesvints driesvints deleted the parent-attributes branch April 29, 2020 13:44
@GrahamCampbell GrahamCampbell changed the title Properly Inherit Parent Attributes [8.x] Properly Inherit Parent Attributes Apr 30, 2020
@fcolecumberri
Copy link

Imagine you have two components: button and danger-button, where danger-button contains an instance of button with some additional classes. You are not able to proxy down any custom attributes to the button component using $attributes. For example, this will not work:

<!-- danger-button.blade.php -->

<x-button class="bg-red-200" {{ $attributes }}>
    {{ $slot }}
</x-button>

This also will not work as expected since the button component will end up with an attribute named attribute:

<!-- danger-button.blade.php -->

<x-button class="bg-red-200" :attributes="$attributes">
    {{ $slot }}
</x-button>

This PR fixes this behavior by detecting any bound attribute bags and merging any other classes with them before setting them as the parent's attribute bag. So, the syntax above using :attributes="$attributes" will be handled properly.

It would be possible to handle the {{ $attributes }} passing syntax using some regular expressions to translate that syntax to :attributes="$attributes" for custom Blade tags but will leave that to someone with more regular expression knowledge.

Sending this to master since developers may already be working around this in more cumbersome ways that would be broken by this PR.

It would be lovely if you could add all of this here → https://laravel.com/docs/8.x/blade

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.

2 participants