Chapter 12

Extending PixiFormUI

12.1 A custom control in six methods#

src/examples/StarRating.ts is a complete, typechecked example. The essential shape:

export class StarRating extends Control<number> {
  private value = 0;
  private readonly stars = new Graphics();

  constructor(ui: FormUI, opts: ControlOptions<number> & { value?: number }) {
    super(ui, opts);                         // label, helper, focus, theme, validation: all inherited
    this.value = opts.value ?? 0;
    this.body.addChild(this.stars);          // draw inside this.body (positioned under the label)
    this.body.eventMode = 'static';
    this.bindTap(this.body, (e) => this.setValue(Math.floor(e.getLocalPosition(this.body).x / 36) + 1));
    this.redraw();                           // ALWAYS last in the constructor
  }

  getValue() { return this.value; }
  setValue(v: number, emit = true) { this.value = v; this.drawBody(); this.ui.focus.sync(this); this.commitChange(emit); }
  reset() { this.value = 0; this.touched = false; this.setError(null); this.redraw(); }
  isEmpty() { return this.value === 0; }
  protected bodyHeight() { return 36; }
  protected drawBody() {
    if (!this.stars) return;                 // the base class may call this before your fields exist
    /* draw with this.theme.colors..., then: */
    this.drawFocus(0, 0, 180, 36, 8);
  }
}

12.2 Rules for custom controls#

Do Why
Call this.redraw() at the end of the constructor Field initialisers run after super().
Guard drawBody() against missing fields The base class may draw during construction.
Read every colour and size from this.theme Global themes, setTheme and per-instance overrides then just work.
Use this.bindTap(target, fn) for presses Tap-not-press semantics: correct on touch and ignores scroll gestures.
Call this.commitChange(emit) after value changes Emits change and re-validates when appropriate.
Call this.ui.focus.sync(this) after state changes Updates screen-reader state (aria-checked, aria-valuenow, …).
Override ariaRole() and ariaState() Screen-reader semantics.
Override handleKeyDown(e) and return true when handled Keyboard support; returning true prevents default.
Set this.ui.focus.pointerCaptured = true while dragging Stops scroll views from panning during your drag.
Clean up tickers and timers in destroy() Avoid leaks.
Inline label? Call this.placeInfo(this.body, x, cy) in drawBody() Supports the info option (labels above get it automatically).

12.3 Controls that accept text#

Set override readonly acceptsText = true (and optionally inputMode, enterKeyHint, secure = true), then implement handleText(text), handleDelete(direction), handlePaste(text) and handleCopy(cut). Usually it's simpler to extend TextField<V> and get caret, selection, wrapping, undo, clipboard and sanitisation for free. Store text in a SecureBuffer, never in a string field.

12.4 Overlays and popups#

Add popups to ui.overlay (always above ui.content). For modal keyboard handling, set ui.focus.keyInterceptor = (e) => boolean while open and clear it on close (see Select). Set ui.focus.claimed = true in pointer handlers on popup elements so the background click doesn't blur the control.

12.5 Icons#

Icons.check | dash | close | eye | chevron | errorCircle | lock | sun | moon draw vector icons into a Graphics: Icons.eye(g, cx, cy, size, color, off). Add your own the same way: plain functions that draw into a Graphics. Start every sub-path with moveTo() so it doesn't connect to the previous shape.

12.6 Runtime events on FormUI#

Event Arguments
themechange (theme)
focuschange (control | null, viaKeyboard)
viewportchange none (mobile visual viewport resized)
scroll (scrollY) emitted by ScrollView (closes tooltips)

ui.tooltip is the shared tooltip: show(anchor, text, owner?), hide(owner?), toggle(...), isOpen. Use it for your own hints so only one bubble is ever visible.

ui.onInteraction(fn) subscribes to trusted user interactions (used by Form for bot checks and idle timers); it returns an unsubscribe function.