Sprites.Ui.Dom
Build elements, attributes, text, classes, styles and events.
- Sprites.Ui.Dom.Attribute
- Sprites.Ui.Dom.Class
- Sprites.Ui.Dom.Event
- Sprites.Ui.Dom.Style
- Sprites.Ui.Dom.Tag
- Sprites.Ui.Dom.Text
Sprites.Ui.Dom.Attribute Void
Sprites.Ui.Dom.Attribute(name, value)
Set an attribute from a value, a reader function, or a constant. For class and style, prefer Class and Style, which compose.
| Input | Type | Description |
|---|---|---|
name | String | The attribute name. For class and style, prefer Class and Style. |
value | Any, Sprites.Reactive.Value or Function | The attribute value, reactive or constant. |
Returns nothing.
const off = Sprites.Reactive.Value(false);
Sprites.Ui.Input.Checkbox(off);
Sprites.Ui.Dom.Tag('button', () => {
Sprites.Ui.Dom.Attribute('disabled', off);
Sprites.Ui.Dom.Text('Save');
});
Sprites.Ui.Dom.Class Void
Sprites.Ui.Dom.Class(...parts)
Add classes to the current element. Parts are strings, numbers, values or readers, joined with spaces. It may be used more than once, and inside If or Each on the parent, so logic can choose which classes apply. Each call is removed when its scope ends.
| Input | Type | Description |
|---|---|---|
parts | String, Number, Sprites.Reactive.Value or Function | Class names, joined with spaces, empties dropped. |
Returns nothing.
const wide = Sprites.Reactive.Value(true);
Sprites.Ui.Input.Checkbox(wide);
Sprites.Ui.Dom.Tag('button', () => {
Sprites.Ui.Dom.Class(() => {
return Sprites.Reactive.Get(wide) ? 'stretch' : '';
});
Sprites.Ui.Dom.Text('Wide');
});
Sprites.Ui.Dom.Event Void
Sprites.Ui.Dom.Event(name, handler)
Listen for a DOM event. A form control passes its latest value to the handler.
| Input | Type | Description |
|---|---|---|
name | String | The event name, with or without the on prefix. |
handler | Function | Runs in the reverse phase, then both heaps flush. A form control passes its latest value. |
Returns nothing.
const n = Sprites.Reactive.Value(0);
Sprites.Ui.Dom.Tag('button', () => {
Sprites.Ui.Dom.Event('onclick', () => {
const v = Sprites.Reactive.Freeze(n);
Sprites.Reactive.Set(n, v + 1);
});
Sprites.Ui.Dom.Text('Clicked ', n);
});
Sprites.Ui.Dom.Style Void
Sprites.Ui.Dom.Style(name, ...parts)
Set one style property on the current element to its parts concatenated. Like Class it may be used more than once, one property each, and inside If or Each on the parent. A later call for the same property wins while it lasts.
| Input | Type | Description |
|---|---|---|
name | String | The style property. |
parts | String, Number, Sprites.Reactive.Value or Function | The value pieces, concatenated. |
Returns nothing.
const size = Sprites.Reactive.Value(16);
Sprites.Ui.Input.Slider(size, 10, 40);
Sprites.Ui.Dom.Tag('span', () => {
Sprites.Ui.Dom.Style('font-size', size, 'px');
Sprites.Ui.Dom.Text('Resize me');
});
Sprites.Ui.Dom.Tag Element
Sprites.Ui.Dom.Tag(name, ...content)
Add an element and build its content. One build closure runs as it is; a string, a number or a value (or several) become text.
| Input | Type | Description |
|---|---|---|
name | String | The tag name. |
content | Function, String, Number or Sprites.Reactive.Value | One build closure, or text parts. |
Returns the created element.
const n = Sprites.Reactive.Value(0);
Sprites.Ui.Dom.Tag('button', () => {
Sprites.Ui.Dom.Event('onclick', () => {
const v = Sprites.Reactive.Freeze(n);
Sprites.Reactive.Set(n, v + 1);
});
Sprites.Ui.Dom.Text('Tapped ', n);
});
Sprites.Ui.Dom.Text Node
Sprites.Ui.Dom.Text(...parts)
Add a text node from one or more parts, joined. Each part is a string, a number, any value, a reactive value or a reader, so there is no need to cast to a string. It stays live while any part is reactive.
| Input | Type | Description |
|---|---|---|
parts | String, Number, Sprites.Reactive.Value or Function | Parts joined into the text node. Stays live while any part is reactive. |
Returns the text node.
const name = Sprites.Reactive.Value('world');
Sprites.Ui.Input.Text(name);
Sprites.Ui.Dom.Text('Hello ', name);