--- url: /reference/items/DataBind/examples.md --- # Examples ## Virtual bindings The examples below show each [virtual binding](./js-api.md#virtual-bindings) in isolation. All of them can be combined on the same element. ### Property binding The `data-bind:prop.` binding assigns a DOM property. The button below enables itself only when the email value looks valid, by assigning its `disabled` property. :::code-group ```js import { registerComponents } from '@studiometa/js-toolkit'; import { Action, DataBind, DataModel, DataScope } from '@studiometa/ui'; registerComponents(Action, DataScope, DataBind, DataModel); ``` ::: ### Attribute binding The `data-bind:attr.` binding writes an attribute, and removes it for `false`, `null`, or `undefined` results. The disclosure below drives both the `aria-expanded` state of its button and the `hidden` attribute of its panel from one value. Note the explicit `String(…)` on `aria-expanded`: ARIA states must keep their `"false"` value instead of removing the attribute. :::code-group ```js import { registerComponents } from '@studiometa/js-toolkit'; import { Action, DataBind, DataModel, DataScope } from '@studiometa/ui'; registerComponents(Action, DataScope, DataBind, DataModel); ``` ::: ### Class binding The `data-bind:class.` binding toggles a class according to the result's boolean value. An empty expression passes the raw value through, so a checkbox's boolean value can drive classes directly. :::code-group ```js import { registerComponents } from '@studiometa/js-toolkit'; import { Action, DataBind, DataModel, DataScope } from '@studiometa/ui'; registerComponents(Action, DataScope, DataBind, DataModel); ``` ::: ### Style binding The `data-bind:style.` binding writes an inline style, and clears it for `false`, `null`, or `undefined` results. The progress bar below computes its `width` from a range input. Custom properties work too, for example `data-bind:style.--progress="value"`. :::code-group ```js import { registerComponents } from '@studiometa/js-toolkit'; import { Action, DataBind, DataModel, DataScope } from '@studiometa/ui'; registerComponents(Action, DataScope, DataBind, DataModel); ``` ::: ### Text binding The `data-bind:text` binding assigns `textContent`. Use an expression to format the value, or an empty attribute to render it as is. :::code-group ```js import { registerComponents } from '@studiometa/js-toolkit'; import { Action, DataBind, DataModel, DataScope } from '@studiometa/ui'; registerComponents(Action, DataScope, DataBind, DataModel); ``` ::: ### Conditional rendering The `data-bind:if` binding adds or removes the content of a `