Skip to content

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 ​

AttributeValue formatPermissionEffect
data-state-bindkeystate:writeField ↔ state, two-way
data-state-text[!]keystate:readState → textContent
data-state-show[!]keystate:readState → hidden
data-state-class[!]key:classNamestate:readState → classList.toggle
data-state-iskey<operator>valuestate:readComparison → hidden
data-state-is-classkey<operator>value:className[; …]state:readComparison → class
data-state-setkey=valuestate:writeWrites on click
data-state-togglekeystate:writeFlips on click
data-state-initk=v k=v or JSONstate:writeOne-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.parse first: 0 is a number, true a boolean, list a 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 value the 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.
ElementRead as
checkboxboolean
radiothe checked one's value (bind the whole group to one key)
number / rangenumber (empty string → null)
<select multiple>string[]
othersstring

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 ​

WhenRoot
Page bootdocument
After every fragment insertThe 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 innerHTML is not scanned. Always go through renderBlade.