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.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.
| Input | Type | Description |
|---|---|---|
optionalValue | Any or Sprites.Reactive.Value | The value that may be missing. |
defaultValue | Any or Sprites.Reactive.Value | The 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);