Sprites.Optional

Helpers for values that may be absent. Default reads an optional value and falls back to a default when it is missing, so a caller reads one value that is always present. Every argument is a constant or a reactive value, so the result stays live.

Sprites.Optional

Sprites.Optional.Default Sprites.Reactive.Value

Sprites.Optional.Default(optionalValue, defaultValue)

The optional value when it is present, else the default. A value is present when it is anything other than null or undefined, so zero and the empty string stand as given. Either argument may be a constant or a reactive value, so the result follows both.

InputTypeDescription
optionalValueAny or Sprites.Reactive.ValueThe value that may be missing.
defaultValueAny or Sprites.Reactive.ValueThe fallback used while the value is missing.

Returns a read-only Sprites.Reactive.Value, the optional value while it is present and the default while it is missing.

const provided = Sprites.Reactive.Value(false);
const width = Sprites.Reactive.Calculate(() => {
  return Sprites.Reactive.Get(provided) ? 8 : null;
});
const size = Sprites.Optional.Default(width, 32);
Sprites.Ui.Input.Checkbox(provided);
Sprites.Ui.Dom.Text(' size = ', size);
Unchecked the width is missing and the size reads 32; checked it reads 8.