Declarative Bindings
data-state-* attributes connect the DOM to state. When a value changes the DOM updates, with no JavaScript.
Every attribute must sit inside an @renderObject that grants the matching action.
All attributes
| Attribute | Value format | Permission | Effect |
|---|---|---|---|
data-state-bind | key | state:write | Field ↔ state, two-way |
data-state-text | [!]key | state:read | State → textContent |
data-state-show | [!]key | state:read | State → hidden |
data-state-class | [!]key:className | state:read | State → classList.toggle |
data-state-is | key<operator>value | state:read | Comparison → hidden |
data-state-is-class | key<operator>value:className[; …] | state:read | Comparison → class |
data-state-set | key=value | state:write | Writes on click |
data-state-toggle | key | state:write | Flips on click |
data-state-init | k=v k=v or JSON | state:write | One-time initial values |
Prefixes:
!negates (only text / show / class support it).@escapes the namespace and uses a global key; can combine:!@theme.
Initial values: data-state-init
blade
<section @renderObject('calcA', ['state:read', 'state:write'])
data-state-init="display=0 memory=0 showTape=false">- Values try
JSON.parsefirst:0is a number,truea boolean,lista string. - Only writes keys that don't exist yet, so a re-render never overwrites what the user did.
- Values containing spaces need JSON:
data-state-init='{"title": "hello world"}'.
Two-way fields: data-state-bind
blade
<input name="name" data-state-bind="name" value="{{ $customer->name }}">Precedence: value already in state > the HTML value > leave it alone.
- First render: state is empty → takes the
valuethe server rendered. - Later re-renders: state has a value → written back into the field, so unsaved edits remain.
- A focused field is never overwritten, so typing doesn't jump the cursor.
| Element | Read as |
|---|---|
| checkbox | boolean |
| radio | the checked one's value (bind the whole group to one key) |
| number / range | number (empty string → null) |
<select multiple> | string[] |
| others | string |
Don't bind type="file"
It stores C:\fakepath\…. Use FileDrop for files.
Comparisons: data-state-is / data-state-is-class
Six operators: = != > >= < <=. The right-hand side must be a literal.
blade
<div data-state-is="step=2">Step two</div>
<li data-state-is-class="step=2:is-current; step>2:is-done">Payment</li>Separate several rules with semicolons. 2 and "2" are equal; if both sides convert to numbers they're compared as numbers.
No arithmetic
You can compare but not compute: no a+b, no step=max-1. Compute in a CGEvent, or have the server return the value.
Example: tabs
blade
<section @renderObject('report', ['state:read', 'state:write']) data-state-init="tab=list">
<nav>
<button type="button" data-state-set="tab=list" data-state-is-class="tab=list:is-current">List</button>
<button type="button" data-state-set="tab=chart" data-state-is-class="tab=chart:is-current">Chart</button>
</nav>
<div data-state-is="tab=list">…list…</div>
<div data-state-is="tab=chart">…chart…</div>
</section>When scanning happens
| When | Root |
|---|---|
| Page boot | document |
| After every fragment insert | The insertion point |
The order is fixed: inline <script> → CGEvent.bind() → bindState() → renderBladeAuto().
- Each slot on each element is bound only once (
WeakMap), so repeated scans are safe. - Subscriptions are released automatically when nodes are replaced. Callers do nothing.
- HTML you insert yourself with
innerHTMLis not scanned. Always go through renderBlade.