Recursive AI

Scroll a list item into view in C#

OneUI is a free, open-source .NET library from Recursive AI for writing UI components in HTML, CSS, and C# that run in native windows on desktops and phones, or in a browser via WebAssembly, with OneApp. MIT or Apache-2.0.

Set ref="{Target}" on the row. Once it renders, scroll it into view.

Terminal
dotnet add package RecursiveAI.OneUI
dotnet add package RecursiveAI.OneApp

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
Target.ScrollIntoView(
    ScrollAlignment.Nearest);

The example is the full program below: unzip it and dotnet run.

The sample moves the list from 0 px to 60 px, just enough to show Reports.

The full program

Save this as Results.html next to Example.cs. OneUI’s source generator pairs it with the partial Results class and binds List and Target. The zip pins RecursiveAI.OneUI at 1.2.1 and RecursiveAI.OneApp at 1.2.0 as ordinary PackageReference entries. OneApp lays out the list without opening a visible window.

HTML
<ul ref="{List}" style="height: 60px; overflow: auto; margin: 0; padding: 0">
  <li style="height: 30px">Invoices</li>
  <li style="height: 30px">Receipts</li>
  <li style="height: 30px">Statements</li>
  <li ref="{Target}" style="height: 30px">Reports</li>
  <li style="height: 30px">Exports</li>
</ul>
C#
using OneApp;
using OnePlatform;
using OneUI.Components;
 
var results = new Results();
using var app = AppHost.Create(new AppOptions(results)
{
    Backend = PlatformBackendKind.Headless,
    UseGpu = false,
    Width = 400,
    Height = 200
});
app.Step(0);
 
Console.WriteLine($"Before: {results.List.ScrollTop} px");
results.Reveal();
Console.WriteLine($"After: {results.List.ScrollTop} px");
 
public partial class Results
{
    public ElementRef List { get; } = new();
    ElementRef Target { get; } = new();
 
    public void Reveal() =>
        Target.ScrollIntoView(ScrollAlignment.Nearest);
}
Text
Before: 0 px
After: 60 px

In a windowed app, wire a button’s on:click="Reveal" in Results.html to run Reveal() when the user clicks it.

An unbound reference throws InvalidOperationException; a bound row with display: none has no box to scroll to.

Variations

Replace the call in Reveal with this to center Reports instead. The After line becomes 75 px.

C#
Target.ScrollIntoView(ScrollAlignment.Center);

The short answer

Use a OneUI ElementRef on the list item, not the list itself. After the item exists, ScrollIntoView(ScrollAlignment.Nearest) moves its scroll containers only as far as needed to reveal it. If you’ve just set state to add the item and your next line uses its reference, call UiContext.Commit() first so the new ref is bound.

How do you scroll an item to the center of a list?

Use ScrollAlignment.Center for vertical alignment in a normal top-to-bottom list. In the example, Reports starts 90 CSS pixels from the top and is 30 pixels tall, so its center starts at 105. The list shows 60 pixels, so a scroll offset of 75 puts the row’s center 30 pixels from the visible top. That calculation doesn’t have to live in your component: OneApp uses the measured boxes and keeps the result within the scrollable range. A row near the end may not land exactly in the center because there isn’t enough content after it.

Why isn’t ScrollIntoView scrolling my list?

Check the box around the list first. A list that grows to fit all its rows has no overflow to scroll. Give it a constrained height and overflow: auto, as in the example. Then check the row: if the conditional markup hasn’t created it, the reference won’t be bound. If the row exists but has no layout box, the request does nothing. With Nearest, a row that’s already visible also doesn’t need to move.

For nested scroll containers, scrolling starts with the innermost one and then works outward using the row’s updated position. Reading ScrollTop on the outer list alone won’t tell you whether an inner one moved. ScrollIntoView does not ask OneUI to scroll the document viewport itself; that is up to the backend. See the scrolling rules for alignment and behavior details.

All oneui recipes