LavaUI docs

struct Binding

Swift

@propertyWrapper struct Binding<Value>

A two-way reference to a value owned somewhere else.

Sources/LavaUI/State.swift:114

Constructors

init<Root>(_ root: Root, _ keyPath: ReferenceWritableKeyPath<Root, Value>) where Root : AnyObject

Binds one property of a reference-type model.

Replaces the hand-written pair, which named the property three times and gave three chances to name a different one by mistake:

Binding(get: { session.rules }, set: { session.rules = $0 })
Binding(session, \.rules)

The read on the first line is deliberate, and is the reason this is not purely cosmetic. Observation registers a dependency only on properties some body actually read while it was recording. A binding that is merely constructed and handed to a control that never reads it during body — overlay(isPresented:) keeps a live closure and reads it at emit time — registers nothing, so a write to the model invalidates nothing. That is exactly how opening a settings panel from a menu could change the model and paint no frame until an unrelated hover happened to repaint. Touching the value here, while this initialiser runs inside body, registers the dependency for whatever the binding is passed to.

Cheap regardless of Value: this is one property read, and for a large String it is a retain, not a copy.

Sources/LavaUI/Bindable.swift:27

init(get: @escaping () -> Value, set: @escaping (Value) -> Void)

Not documented.

Sources/LavaUI/State.swift:118

Properties

var projectedValue: Binding<Value> { get }

Not documented.

Sources/LavaUI/State.swift:145

var wrappedValue: Value { get nonmutating set }

Not documented.

Sources/LavaUI/State.swift:123

Methods

static func constant(_ value: Value) -> Binding<Value>

A binding that never changes. For previews and for controls that are shown in a fixed state — a disabled toggle has to display something, and inventing a throwaway @State for it is worse.

Sources/LavaUI/State.swift:150