When building reusable Blade components that utilize Bond for reactivity, we could face a synchronization issue between the initial server-side state (Blade) and the client-side state (Alpine).
Currently, if we want a component (like a Button) to have a reactive variant prop, we have to:
Declare the prop in Blade for initial server-side rendering (SSR) styling and redeclare it in the script tag.
After this we have to manually check if x-variant exists in the $attributes bag; if not, we must manually inject it to ensure Alpine picks up the Blade prop as the starting reactive value.
@props(['variant' => 'primary'])
<script setup>
mount((props: {
variant: string,
}) => ({
init(){ ... }
}))
</script>
<button
{{ $attributes->merge(['class' => "...using $variant"]) }}
:class="{...using variant}"
>
</button>
Since the script is compiled, we cannot directly inject PHP into the JS. However, it would be beneficial to have a built-in way to declare initial values from PHP that Alpine automatically hydrates.
The goal is to allow the Blade prop to handle the initial server-side styling while simultaneously informing the Alpine reactive state, without the developer having to manually sync the two in the attribute bag to set the initial x-variant prop.
We could avoid the server-side blade props but this will produce a flickering until Alpine starts and evaluate the classes.
A solution I tought could be to declare the initial values by using a blade directive like:
@jsProps(['variant' => $variant])
Or simple
@jsProps(['variant'])
And it will use the $variant value automatically.
Let me know if this makes sense or not.
When building reusable Blade components that utilize Bond for reactivity, we could face a synchronization issue between the initial server-side state (Blade) and the client-side state (Alpine).
Currently, if we want a component (like a Button) to have a reactive variant prop, we have to:
Declare the prop in Blade for initial server-side rendering (SSR) styling and redeclare it in the script tag.
After this we have to manually check if x-variant exists in the $attributes bag; if not, we must manually inject it to ensure Alpine picks up the Blade prop as the starting reactive value.
Since the script is compiled, we cannot directly inject PHP into the JS. However, it would be beneficial to have a built-in way to declare initial values from PHP that Alpine automatically hydrates.
The goal is to allow the Blade prop to handle the initial server-side styling while simultaneously informing the Alpine reactive state, without the developer having to manually sync the two in the attribute bag to set the initial x-variant prop.
We could avoid the server-side blade props but this will produce a flickering until Alpine starts and evaluate the classes.
A solution I tought could be to declare the initial values by using a blade directive like:
@jsProps(['variant' => $variant])
Or simple
@jsProps(['variant'])
And it will use the $variant value automatically.
Let me know if this makes sense or not.