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

class

ObservableStack<T>

public sealed class ObservableStack<T> : IComponent where T : class

A 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

NameDescription
ObservableStackCreates a list that reconciles source into host.
Constructor
ObservableStack
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

NameDescription
RenderRenders the host element.
Method
ObservableStack.Render
public HTMLElement 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.

class

KeyedObservableStack

public class KeyedObservableStack : IComponent, IHasBackgroundColor, IHasMarginPadding, ISpecialCaseStyling, ICanWrap

A 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

NameDescription
KeyedObservableStackInitializes a new instance of this class.
Constructor
KeyedObservableStack
public KeyedObservableStack(ObservableList<IComponentWithID> observableList, Orientation orientation = Orientation.Vertical, bool debounce = true)

Initializes a new instance of this class.

Properties

NameDescription
StackOrientationGets or sets the stack orientation.
CanWrapGets or sets a value indicating whether the component's text can wrap onto multiple lines.
IsInlineReturns a value indicating whether the component is inline.
InnerElementGets the underlying DOM element backing this component.
BackgroundGets or sets the CSS background of the component.
MarginGets or sets the CSS margin of the component.
PaddingGets or sets the CSS padding of the component.
StylingContainerGets or sets the styling container.
PropagateToStackItemParentGets or sets the propagate to stack item parent.
Property
KeyedObservableStack.StackOrientation
public Orientation StackOrientation { get ; set ; }

Gets or sets the stack orientation.

Property
KeyedObservableStack.CanWrap
public bool CanWrap { get ; set ; }

Gets or sets a value indicating whether the component's text can wrap onto multiple lines.

Property
KeyedObservableStack.IsInline
public bool IsInline { get ; set ; }

Returns a value indicating whether the component is inline.

Property
KeyedObservableStack.InnerElement
public HTMLElement InnerElement { get; private set; }

Gets the underlying DOM element backing this component.

Property
KeyedObservableStack.Background
public string Background { get ; set ; }

Gets or sets the CSS background of the component.

Property
KeyedObservableStack.Margin
public string Margin { get ; set ; }

Gets or sets the CSS margin of the component.

Property
KeyedObservableStack.Padding
public string Padding { get ; set ; }

Gets or sets the CSS padding of the component.

Property
KeyedObservableStack.StylingContainer
public HTMLElement StylingContainer

Gets or sets the styling container.

Property
KeyedObservableStack.PropagateToStackItemParent
public bool PropagateToStackItemParent { get; private set; }

Gets or sets the propagate to stack item parent.

Methods

NameDescription
AlignItemsSets the align-items css property for this stack
AlignItemsCenterSets the align-items css property for this stack to 'center'
RelativeMake this stack relative (i.e. position:relative)
AlignContentSets the align-content CSS property for this stack.
JustifyContentSets the justify-content CSS property for this stack.
JustifyItemsSets the justify-items CSS property for this stack.
RemovePropagationRemoves the given propagation from the component.
OnMouseOverRegisters a callback invoked when the mouse over event fires.
OnMouseOutRegisters a callback invoked when the mouse out event fires.
ClearClears the component's current state.
RenderRenders the component's root HTML element.
HorizontalConfigures the component to horizontal.
VerticalConfigures the component to vertical.
HorizontalReverseConfigures the horizontal reverse on the component.
VerticalReverseConfigures the vertical reverse on the component.
WrapAllows the component's content to wrap onto multiple lines.
InlineRenders the component inline.
NoWrapRemoves / disables the wrap on the component.
OverflowHiddenHides any content that overflows the component's bounds.
NoDefaultMarginRemoves / disables the default margin on the component.
SkeletonConfigures the component to skeleton.
Method
KeyedObservableStack.AlignItems
public KeyedObservableStack AlignItems(ItemAlign align)

Sets the align-items css property for this stack

Parameters

align
Method
KeyedObservableStack.AlignItemsCenter
public KeyedObservableStack AlignItemsCenter()

Sets the align-items css property for this stack to 'center'

Parameters

align
Method
KeyedObservableStack.Relative
public KeyedObservableStack Relative()

Make this stack relative (i.e. position:relative)

Method
KeyedObservableStack.AlignContent
public KeyedObservableStack AlignContent(ItemAlign align)

Sets the align-content CSS property for this stack.

Parameters

align
The alignment.

Returns

The current instance.

Method
KeyedObservableStack.JustifyContent
public KeyedObservableStack JustifyContent(ItemJustify justify)

Sets the justify-content CSS property for this stack.

Parameters

justify
The justification.

Returns

The current instance.

Method
KeyedObservableStack.JustifyItems
public KeyedObservableStack JustifyItems(ItemJustify justify)

Sets the justify-items CSS property for this stack.

Parameters

justify
The justification.

Returns

The current instance.

Method
KeyedObservableStack.RemovePropagation
public KeyedObservableStack RemovePropagation()

Removes the given propagation from the component.

Method
KeyedObservableStack.OnMouseOver
public KeyedObservableStack OnMouseOver(ComponentEventHandler<KeyedObservableStack, Event> onMouseOver)

Registers a callback invoked when the mouse over event fires.

Method
KeyedObservableStack.OnMouseOut
public KeyedObservableStack OnMouseOut(ComponentEventHandler<KeyedObservableStack, Event> onMouseOut)

Registers a callback invoked when the mouse out event fires.

Method
KeyedObservableStack.Clear
public virtual void Clear()

Clears the component's current state.

Method
KeyedObservableStack.Render
public virtual HTMLElement Render()

Renders the component's root HTML element.

Method
KeyedObservableStack.Horizontal
public KeyedObservableStack Horizontal()

Configures the component to horizontal.

Method
KeyedObservableStack.Vertical
public KeyedObservableStack Vertical()

Configures the component to vertical.

Method
KeyedObservableStack.HorizontalReverse
public KeyedObservableStack HorizontalReverse()

Configures the horizontal reverse on the component.

Method
KeyedObservableStack.VerticalReverse
public KeyedObservableStack VerticalReverse()

Configures the vertical reverse on the component.

Method
KeyedObservableStack.Wrap
public KeyedObservableStack Wrap()

Allows the component's content to wrap onto multiple lines.

Method
KeyedObservableStack.Inline
public KeyedObservableStack Inline()

Renders the component inline.

Method
KeyedObservableStack.NoWrap
public KeyedObservableStack NoWrap()

Removes / disables the wrap on the component.

Method
KeyedObservableStack.OverflowHidden
public KeyedObservableStack OverflowHidden()

Hides any content that overflows the component's bounds.

Method
KeyedObservableStack.NoDefaultMargin
public KeyedObservableStack NoDefaultMargin()

Removes / disables the default margin on the component.

Method
KeyedObservableStack.Skeleton
public IComponent Skeleton(bool enabled = true)

Configures the component to skeleton.

interface

IComponentWithID

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.

See Also

© 2026 Curiosity. All rights reserved.