---
title: "Open a non-modal popover on click in C#"
url: https://recursiveai.org/oneapp/cookbook/show-desktop-popover-on-button-click/
description: "Open a non-modal OneApp popover beside a C# desktop button. Keep its open state in C# and dismiss the panel with Escape, an outside click, or Done."
version: "1.1.1"
---

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

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

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (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](https://recursiveai.org/oneapp/cookbook/show-desktop-popover-on-button-click/show-desktop-popover-on-button-click.zip) 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`:

```csharp
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:

```csharp
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](https://recursiveai.org/oneapp/images/show-desktop-popover-on-button-click/panel.png)

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`:

```csharp
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.

## Related recipes

- [Add checkable items to a desktop dropdown menu](https://recursiveai.org/oneapp/cookbook/add-checkable-items-to-desktop-dropdown-menu/)
- [Confirm a delete with an accessible alert dialog](https://recursiveai.org/oneapp/cookbook/confirm-destructive-action-with-alert-dialog/)

## 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](https://recursiveai.org/oneapp/docs/kit/#overlays).
