Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions blade.md
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,38 @@ You may specify which attributes should be considered data variables using the `
Given the component definition above, we may render the component like so:

<x-alert type="error" :message="$message" class="mb-4"/>

<a name="accessing-parent-data"></a>
#### Accessing Parent Data

Sometimes you may want to access data from a parent component inside a child component. In these cases, you can use the `@aware` directive to make outside data available inside the component.

For example, imagine we are building a complex menu component consisting of a parent `<x-menu>` and child `<x-menu.item>` that would be used like so:

<x-menu color="purple">
<x-menu.item>...</x-menu.item>
<x-menu.item>...</x-menu.item>
</x-menu>

Given the above usage, here's what the `<x-menu>` component might look like:

<!-- /resources/views/components/menu/index.blade.php -->

@props(['color' => 'gray'])

<ul {{ $attributes->merge(['class' => 'bg-'.$color.'-200']) }}>
{{ $slot }}
</ul>

Because the `$color` prop was only passed into the parent `<x-menu>`, it won't be available inside `<x-menu.item>`. However, if we use the `@aware` directive, we can make it available inside `<x-menu.item>` as well:

<!-- /resources/views/components/menu/item.blade.php -->

@aware(['color'])
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to set the default in the parent and the child because @props doesn't get evaluated before the child has finished rendering. This may be something we want to consider before merging…

Suggested change
@aware(['color'])
@aware(['color' => 'gray'])


<li {{ $attributes->merge(['class' => 'text-'.$color.'-800']) }}>
{{ $slot }}
</li>

<a name="dynamic-components"></a>
### Dynamic Components
Expand Down