- Docs
- Components
Bindings
Give an attribute a property reference and it binds both ways: the element shows the variable, and what the user types writes back to it.
21 °C feels mild.
<script kotlin>
var celsius by state(21.0)
</script>
<label>Celsius <input type="number" step="0.5" valueAsNumber={::celsius}></label>
<input type="range" min="-20" max="45" step="0.5" aria-label="Celsius" valueAsNumber={::celsius}>
What binds
These are bound on an element:
| Attribute | On | Type | The element shows |
|---|---|---|---|
value |
a text input, <textarea> |
String |
The text. |
value |
<select> |
String |
The option whose value it is. |
value |
<select multiple> |
List<String> |
The options whose value is in the list. |
valueAsNumber |
an input of type number or range |
Double |
The number, NaN for an empty field. |
checked |
a checkbox | Boolean |
The box. |
group |
a radio | String |
Checked when the group is its value, and sets the group to it when it gets checked. |
group |
a checkbox | List<String> |
Checked when its value is in the list, and adds or removes it when it changes. |
open |
<details> |
Boolean |
Open or closed. |
<script kotlin>
var accepted by state(false)
var volume by state(30.0)
var plan by state("pro")
var tags by state(listOf("a"))
var city by state("lyon")
</script>
<input type="checkbox" checked={::accepted}>
<input type="range" valueAsNumber={::volume}>
@for (option in listOf("free", "pro"), key = option) {
<label><input type="radio" name="plan" value={option} group={::plan}> {option}</label>
}
<input type="checkbox" value="a" group={::tags}>
<select value={::city}>
<option value="paris">Paris</option>
<option value="lyon">Lyon</option>
</select>
-
A radio or checkbox that binds a
groupneeds avalue, which is what the group holds for it. -
The server writes
checkedfor the ones the state holds, andselectedfor the options of a<select>whosevaluethe state holds. An option without avalueattribute isn't matched. A select whose options change later keeps the pick of the browser until the state changes. -
What the user typed or checked in a server rendered control before the page hydrated is written to the state instead of being replaced.
-
A binding owns the event that writes it back, so these pairs can't share an element:
Bound attribute Can't share the element with value@inputchecked@changea bound <select>@changeopen@toggleA listener on a parent element, which the event reaches as it bubbles, is the place for code that runs on each change.
What can be bound
Only what the markup can follow is bound:
::nameis aprop var, or avarbystate,derivedor a scroll position, of the script or of the top level of a.ktfile.receiver::nameis such avarof a class or anobject, whose type the compiler has to see from the receiver.
<script kotlin>
class Form {
var name by state("")
}
val form = Form()
</script>
<input value={form::name}>
<p>Hello {form.name}</p>
The receiver is an object, or a prop, a loop variable, a @val or a script val declared with its type or built by calling the class, as form is.
An element that gets any other reference on one of those attributes is a compile error. A component receives it as the plain value it is, so sortBy={User::name} passes the reference.
The build reads the .kt files of every module for their top-level var by state or derived and the ones of their classes and objects. In a Gradle project, the plugin reads the .kt files of jsMain and commonMain.
Components
On a component, prop={::variable} binds one of its prop var:
<Slider value={::volume} />
Limits
- An element binds the properties of the table only: any other property,
indeterminate,filesor the state of a media element, isn't possible yet, because the runtime has no binding for it. - A number is a
Double, so anIntstate isn't bound tovalueAsNumber.