Recursive AI

Open a non-modal popover on click in C#

OneApp is a free, open-source framework for apps in .NET whose UI is HTML and CSS, drawn natively with no web view on Windows, macOS, Linux, iOS and Android, and run in the browser, from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

Open a small panel next to a button without a modal dialog. In this example, the panel holds a short writing tip.

Terminal
dotnet new console -o Example
cd Example
dotnet add package RecursiveAI.OneApp
dotnet add package RecursiveAI.OneApp.Kit

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

HTML
<Popover Id="tips" Open="{Open}"
  OnOpenChange="{SetOpen}"
  AriaLabel="Writing tip">
  <template slot="trigger">
    <Button AriaHasPopup="dialog"
      AriaControls="tips"
      AriaExpanded="{Expanded}">
      Writing tip
    </Button>
  </template>
  <p>Put the action first.</p>
  <Button OnClick="{Close}">Done</Button>
</Popover>

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

The snippet goes in Demo.html; Demo.cs supplies Open, Expanded, SetOpen, and Close.

In the example, click Writing tip, then press Escape or click Done. The terminal prints:

Text
Writing tip opened
Writing tip closed

Full program

Checked on Linux (X11) with OneApp and OneApp Kit 1.1.1. OneApp also runs on Windows and macOS (Apple silicon), but this recipe wasn’t run on either.

The zip pins both packages in Example.csproj. OneUI, OneApp’s UI layer, comes with RecursiveAI.OneApp; no separate install is needed. Its source generator implements the [State] properties in Demo.cs. It pairs Demo.html with the partial class Demo by matching the template’s base name to the class name.

XML
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference
      Include="RecursiveAI.OneApp"
      Version="1.1.1" />
    <PackageReference
      Include="RecursiveAI.OneApp.Kit"
      Version="1.1.1" />
  </ItemGroup>
</Project>

Demo.html:

HTML
<Theme>
  <main style="padding: 32px">
    <Popover Id="tips"
      AriaLabel="Writing tip"
      Open="{Open}"
      OnOpenChange="{SetOpen}"
      Align="{Alignment}">
      <template slot="trigger">
        <Button Id="show"
          AriaHasPopup="dialog"
          AriaControls="tips"
          AriaExpanded="{Expanded}">
          Writing tip
        </Button>
      </template>
      <p>Put the action first.</p>
      <Button Id="done" OnClick="{Close}">
        Done
      </Button>
    </Popover>
    <p id="outside">
      Click here to dismiss.
    </p>
  </main>
</Theme>

Demo.cs:

C#
using System;
using OneUI.Components;
using OneApp.Kit;
 
public partial class Demo
{
    [State]
    public partial bool Open { get; set; }
 
    private OverlayAlign Alignment =>
        OverlayAlign.Start;
    private string Expanded =>
        Open ? "true" : "false";
 
    private void SetOpen(bool open)
    {
        Open = open;
        Console.WriteLine(open
            ? "Writing tip opened"
            : "Writing tip closed");
    }
 
    private void Close() => SetOpen(false);
}

Program.cs opens the window:

C#
using OneApp;
 
var options = new AppOptions(new Demo())
{
    Title = "Writing tip",
    Width = 480,
    Height = 340,
};
using var host = AppHost.Create(options);
host.Run();

Writing tip popover below and left-aligned with its trigger button

Click outside the panel to dismiss it as well. If the button appears stuck in its expanded state, check that OnOpenChange writes back to Open and that the trigger’s AriaExpanded reads from that same property.

Variations

Align the right edges. In Demo.cs, change the property bound to Align:

C#
private OverlayAlign Alignment =>
    OverlayAlign.End;

There still needs to be room for the panel. Near the window edge, the kit can shift it into view.

The short answer

Put a Button in the trigger slot of OneApp’s Popover. The kit places your panel relative to that button and handles outside clicks and Escape. Bind Open and OnOpenChange to C# state so that dismissals and clicks update the same flag. Set that flag to false from a control inside the panel to close it yourself, as the Done button does here.

What’s the difference between a popover and a modal dialog?

Use a popover for content connected to a particular control: help for a field, a preview, or a couple of settings. OneApp renders it as a non-modal dialog inside the same window, not a second desktop window. The first outside click dismisses the panel; the click lands on the kit’s transparent catcher rather than also activating the control beneath it. Use a modal Dialog when the user needs to finish or cancel a task before returning to the page, and use AlertDialog when you need an explicit decision on something like deletion.

The trigger advertises which kind of panel it opens with AriaHasPopup="dialog". AriaControls matches the panel’s Id; AriaExpanded comes from the open flag. Give the panel its own AriaLabel so that the rendered dialog has a name. In this example, the panel itself received focus when opened, and Escape returned focus to the Writing tip button.

How do I position a popover next to the button that opened it?

Pick the preferred edge with Side, the alignment along that edge with Align, and the gap in pixels with SideOffset. In the sample the default side is Bottom, and Alignment is Start, so the panel begins just below the left edge of the trigger. That is a preference, not a fixed window coordinate. On open, the kit measures the panel, anchor, and viewport. It can flip the panel to the other side if there is more room there, then slide it along the edge. It doesn’t redo that placement when you resize the window while the panel is already open; close and reopen it to get a new placement. See the kit’s placement notes.

All oneapp recipes