ObservableStack
Description
ObservableStack<T> renders a list of items into a host Stack and keeps it in sync with an observable source. On every change it reconciles the DOM in place — only inserted, removed, or moved rows are touched — so scroll position and focus on the surrounding rows are preserved. This makes it suited to dynamic lists where re-rendering the whole stack would be wasteful or disruptive.
Usage
Create an ObservableStack<T> by supplying an IObservable<IReadOnlyList<T>> source and a renderItem delegate that builds a component for each item. ObservableList<T> implements that interface, so it can be passed directly; mutating the list (Add, RemoveAt, ReplaceAll, …) drives the reconciliation. An optional host Stack lets you control orientation and layout (defaults to a vertical Stack).
API reference
public sealed class ObservableStack<T> : IComponent where T : classA list container that keeps a Stack in sync with an observable list of items by performing a minimal, reference-keyed diff (common-prefix / common-suffix matching). Only the rows whose backing item actually changed are removed or re-created; rows whose item is unchanged keep their existing rendered IComponent, and therefore their DOM identity, focus and scroll state. This avoids the full subtree rebuild that re-rendering the whole list (e.g. with Defer) would cause. Items are matched by reference (ReferenceEquals), so the observable must surface the same item instances across changes for the rows that should be preserved. A matched row keeps its existing component rather than being rebuilt, so per-row content is expected to refresh through the component's own observation rather than re-creation.
Remarks
This is the reference-keyed counterpart to KeyedObservableStack. Use ObservableStack{T} when you have a list of data models with stable instance identity (matched by reference), want each row built lazily from a factory, and want matched rows preserved across updates. Use KeyedObservableStack instead when the source surfaces logically-equal-but-different instances that must still be matched (it keys pre-built IComponentWithID components by a stable string key plus a content hash), or when you need reordering or content-hash-driven re-rendering. ObservableStack{T} only diffs a common prefix/suffix, so a reorder of interior rows rebuilds that span, whereas KeyedObservableStack handles arbitrary reorders.
- Namespace
- Tesserae
- Implements
- IComponent
Constructors
| Name | Description |
|---|---|
| ObservableStack | Creates a list that reconciles source into host. |
public ObservableStack(IObservable<IReadOnlyList<T>> source, Func<T, IComponent> renderItem, Stack host = null)Creates a list that reconciles source into host.
Parameters
- source
- The observable list of items to render. Reconciliation runs immediately with the current value and on every future change.
- renderItem
- Factory invoked once per newly-inserted item to build its component.
- host
- The Stack the rows are rendered into. Defaults to a vertical Stack.
Methods
| Name | Description |
|---|---|
| Render | Renders the host element. |
KeyedObservableStack
A stack whose children are driven by an ObservableList<IComponentWithID> and reconciled by key rather than rebuilt. Existing children are matched by their Identifier, re-rendered only when their ContentHash changes, reordered to match the new sequence, with dropped items removed and new ones inserted. Use it for a server-driven or streaming list, such as a chat transcript.
public class KeyedObservableStack : IComponent, IHasBackgroundColor, IHasMarginPadding, ISpecialCaseStyling, ICanWrapA Stack whose children are driven by an ObservableList{T} of IComponentWithID. On every change it performs a keyed reconcile against the live DOM rather than rebuilding: existing children are matched by their string Identifier, a matched child is re-rendered (replaceChild) only when its ContentHash changes, surviving children are reordered to match the new sequence, dropped items are removed and new items inserted. Changes are debounced by default.
Remarks
Use this when your items are (or can cheaply be) rendered components that each expose a stable key and a content hash, and you want reordering and/or content-hash-driven replacement (e.g. a server-driven or streaming list such as Chat). For the complementary case, see ObservableStack{T}: it keys data models by reference identity, builds each row lazily via a factory, and never replaces a matched row (rows refresh their own content via observation). Prefer that when items are data objects identified by reference and each row is a self-managing component you do not want rebuilt. Note that it only diffs a common prefix/suffix, so a reorder of interior rows rebuilds that span, whereas KeyedObservableStack handles arbitrary reorders.
- Namespace
- Tesserae
- Implements
- IComponent, IHasBackgroundColor, IHasMarginPadding, ISpecialCaseStyling, ICanWrap
Constructors
| Name | Description |
|---|---|
| KeyedObservableStack | Initializes a new instance of this class. |
Properties
| Name | Description |
|---|---|
| StackOrientation | Gets or sets the stack orientation. |
| CanWrap | Gets or sets a value indicating whether the component's text can wrap onto multiple lines. |
| IsInline | Returns a value indicating whether the component is inline. |
| InnerElement | Gets the underlying DOM element backing this component. |
| Background | Gets or sets the CSS background of the component. |
| Margin | Gets or sets the CSS margin of the component. |
| Padding | Gets or sets the CSS padding of the component. |
| StylingContainer | Gets or sets the styling container. |
| PropagateToStackItemParent | Gets or sets the propagate to stack item parent. |
public Orientation StackOrientation { get ; set ; }Gets or sets the stack orientation.
public bool CanWrap { get ; set ; }Gets or sets a value indicating whether the component's text can wrap onto multiple lines.
public bool IsInline { get ; set ; }Returns a value indicating whether the component is inline.
public HTMLElement InnerElement { get; private set; }Gets the underlying DOM element backing this component.
public string Background { get ; set ; }Gets or sets the CSS background of the component.
public string Margin { get ; set ; }Gets or sets the CSS margin of the component.
public string Padding { get ; set ; }Gets or sets the CSS padding of the component.
public HTMLElement StylingContainerGets or sets the styling container.
Methods
| Name | Description |
|---|---|
| AlignItems | Sets the align-items css property for this stack |
| AlignItemsCenter | Sets the align-items css property for this stack to 'center' |
| Relative | Make this stack relative (i.e. position:relative) |
| AlignContent | Sets the align-content CSS property for this stack. |
| JustifyContent | Sets the justify-content CSS property for this stack. |
| JustifyItems | Sets the justify-items CSS property for this stack. |
| RemovePropagation | Removes the given propagation from the component. |
| OnMouseOver | Registers a callback invoked when the mouse over event fires. |
| OnMouseOut | Registers a callback invoked when the mouse out event fires. |
| Clear | Clears the component's current state. |
| Render | Renders the component's root HTML element. |
| Horizontal | Configures the component to horizontal. |
| Vertical | Configures the component to vertical. |
| HorizontalReverse | Configures the horizontal reverse on the component. |
| VerticalReverse | Configures the vertical reverse on the component. |
| Wrap | Allows the component's content to wrap onto multiple lines. |
| Inline | Renders the component inline. |
| NoWrap | Removes / disables the wrap on the component. |
| OverflowHidden | Hides any content that overflows the component's bounds. |
| NoDefaultMargin | Removes / disables the default margin on the component. |
| Skeleton | Configures the component to skeleton. |
public KeyedObservableStack AlignItems(ItemAlign align)Sets the align-items css property for this stack
Parameters
- align
public KeyedObservableStack AlignItemsCenter()Sets the align-items css property for this stack to 'center'
Parameters
- align
public KeyedObservableStack Relative()Make this stack relative (i.e. position:relative)
public KeyedObservableStack AlignContent(ItemAlign align)Sets the align-content CSS property for this stack.
Parameters
- align
- The alignment.
Returns
The current instance.
public KeyedObservableStack JustifyContent(ItemJustify justify)Sets the justify-content CSS property for this stack.
Parameters
- justify
- The justification.
Returns
The current instance.
public KeyedObservableStack JustifyItems(ItemJustify justify)Sets the justify-items CSS property for this stack.
Parameters
- justify
- The justification.
Returns
The current instance.
public KeyedObservableStack RemovePropagation()Removes the given propagation from the component.
public KeyedObservableStack OnMouseOver(ComponentEventHandler<KeyedObservableStack, Event> onMouseOver)Registers a callback invoked when the mouse over event fires.
public KeyedObservableStack OnMouseOut(ComponentEventHandler<KeyedObservableStack, Event> onMouseOut)Registers a callback invoked when the mouse out event fires.
public virtual HTMLElement Render()Renders the component's root HTML element.
public KeyedObservableStack Horizontal()Configures the component to horizontal.
public KeyedObservableStack Vertical()Configures the component to vertical.
public KeyedObservableStack HorizontalReverse()Configures the horizontal reverse on the component.
public KeyedObservableStack VerticalReverse()Configures the vertical reverse on the component.
public KeyedObservableStack Wrap()Allows the component's content to wrap onto multiple lines.
public KeyedObservableStack NoWrap()Removes / disables the wrap on the component.
public KeyedObservableStack OverflowHidden()Hides any content that overflows the component's bounds.
public KeyedObservableStack NoDefaultMargin()Removes / disables the default margin on the component.
public interface IComponentWithID : IComponent- Namespace
- Tesserae
- Implements
- IComponent
Samples
Basic ObservableStack Usage
The following sample demonstrates how to set up an ObservableStack, attach it to an observable list of components, and render it.