Documentation

class Widgetmethod

Widget​.builder()

Creates a widget builder for this class.

static builder<T extends Widget>(
this: new () => T,
defineView?: (v: Binding<T>) => ViewBuilder,
): Widget.Builder<T>;

Summary

This static method creates a builder object that can be used to construct instances of the widget class. The builder provides a fluent interface similar to UIElement.ElementBuilder, with methods for configuration and extension.

Parameters

Return value

A widget builder object

Description

The builder manages a ViewBuilder.Initializer that handles property setting, bindings, and event handlers. The view function is called lazily on first build, and its result is cached for subsequent builds.

The returned builder object exposes:

and returns a view builder defining the widget’s body. If not provided, the body can be set via the defer callback in Widget.Builder.extend, or by overriding the Widget.body getter in the class itself.

Examples

class MyCounterWidget extends Widget {
  value = 0;
  onIncrement() { this.value++; }
}

function MyCounter() {
  return MyCounterWidget.builder((v) =>
    UI.Row(
      UI.Text(v.bind("value")),
      UI.Button("+").onClick("Increment"),
    )
  ).extend({
    initialValue(n: number) {
      this.initializer.set("value", n);
      return this;
    },
  });
}

Related