Parent Property Synchronization
A RizzyUI component keeps its own Alpine state: whether a toggle is pressed, which values a slider holds, whether a dialog is open. Your application's state lives on an element around it. This page shows how the two talk to each other: events and x-on for "something happened", and x-syncprop for "keep this value in step with mine".
Your state goes on an ancestor
Every RizzyUI component owns its Alpine scope, so x-data, x-init and x-load are reserved on its attributes. Passing one throws an exception that names the component. Put your state on an element around it instead; everything inside, the component's own markup included, can read it.
<!-- Throws: RzToggle owns its x-data -->
<RzToggle x-data="{ bold: false }">B</RzToggle>
<!-- State on an ancestor -->
<div x-data="editor">
<RzToggle x-on:click="formatChanged()">B</RzToggle>
</div>Reacting with x-on
An x-on:* handler you put on a component runs alongside the component's own handler for the same event: the toggle still toggles, the menu item still closes its menu, and your handler runs too. RizzyUI renders your handler with Alpine's equivalent @event form, so the two never replace each other. The expression runs in the component's scope, which includes yours, so it can read your state. Inside it, $el is the component's element: to run code of yours that works with its own element, dispatch an event (x-on:click="$dispatch('editor:changed')") and listen for it in your component.
<div x-data="schedule">
<RzMenubar>
<MenubarMenu>
<MenubarTrigger>Session</MenubarTrigger>
<MenubarContent>
<MenubarItem x-on:click="$dispatch('schedule:publish')">Publish</MenubarItem>
</MenubarContent>
</MenubarMenu>
</RzMenubar>
</div>Syncing a value with x-syncprop
When your state should follow a component's value, and the other way round, put x-syncprop="parent.path -> component.path" on the component. The left side is a path in your state, the right side a property of the component's state (each component page names its properties). Changes flow both ways. By default your value wins when the page loads; x-syncprop.init-child lets the component's value win instead.
<div x-data="editor">
<!-- editor.bold follows the toggle, and the toggle follows editor.bold -->
<RzToggle x-syncprop="bold -> pressed">B</RzToggle>
<!-- The slider's state lives on an inner element: x-syncprop moves there by itself -->
<RzSlider Value="@(new double[] { 30 })" x-syncprop.init-child="volume -> values" />
</div>Some components keep their state on an element inside their root (charts, sliders, date pickers). You do not need to know which: on a component root without its own state, x-syncprop moves to the element that holds the component's state.
Inside dialogs, sheets and popovers
"Parent" means the nearest state around the component that is not a RizzyUI component's. A toggle inside a dialog therefore syncs with your state around the dialog, not with the dialog's own open flag, also though the dialog's content is moved to the end of the page. To name the parent explicitly, add data-syncprop-parent="#selector" next to x-syncprop; the .direct modifier syncs with the closest state even when it is a RizzyUI component's (RizzyUI's own sidebar uses it to drive its mobile sheet).
<div x-data="editor">
<RzDialog>
<DialogTrigger AsChild><RzButton>Format</RzButton></DialogTrigger>
<DialogContent AriaLabel="Format">
<!-- Syncs with editor.bold, not with the dialog's state -->
<RzToggle x-syncprop="bold -> pressed">B</RzToggle>
</DialogContent>
</RzDialog>
</div>Alpine directives on components
The rules for directives you put on a RizzyUI component:
| Directive | Rule |
|---|---|
x-data, x-init, x-load | Reserved; throws. Put your state on an ancestor. |
x-on:* | Allowed; runs alongside the component's own handlers. |
x-syncprop | Allowed; moves to the element that holds the component's state. |
x-bind:* / :attr | Allowed, except for attributes the component computes itself (such as aria-pressed on a toggle): those are dropped, with a warning in Development. |
x-show, x-cloak, x-transition | Allowed, except on components that manage their own visibility (dialog, sheet, popover and menu content, collapsible content), where they throw. |
x-ref | Allowed, except the ref names the component uses itself; those throw. |
x-model | Allowed on components that hold a value (form inputs, input-group inputs, CommandInput, RzColorSwatch); elsewhere it throws. Use x-syncprop instead. |