Ref

A handle to a view. Create it before app.build(), pass it to a view as ref=, read the view back as ref.current from any handler. Use it to set props or call methods such as focus() on a view declared inline.

All views accept ref=. One Ref holds one view.

Constructor

nib.Ref()
nib.Ref[nib.TextField]()   # optional type parameter for editor completions

Properties

Property Type Description
current View | None The attached view. None until the view that received ref= is constructed.

Examples

Clear and focus a field

import nib

def main(app: nib.App):
    name = nib.Ref[nib.TextField]()

    def clear():
        name.current.value = ""  # prop mutation: diffed and patched
        name.current.focus()     # imperative call, routed to the runtime

    app.build(
        nib.VStack(
            controls=[
                nib.TextField(placeholder="Name", ref=name),
                nib.Button("Clear", action=clear),
            ],
            spacing=8,
            padding=16,
        )
    )

nib.run(main)

Scroll a list from a button

log = nib.Ref[nib.ScrollView]()

def jump_to_end():
    log.current.scroll_to_bottom()

nib.ScrollView(controls=[...], ref=log)
nib.Button("Latest", action=jump_to_end)