Recursive AI

Dark mode and theme colors in a C# app

OneApp is a free, open-source framework for desktop apps in .NET whose UI is HTML and CSS, drawn natively with no web view, from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

Switch dark mode with <Theme Dark> and set button colors with CSS variables.

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

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

HTML
<Theme Dark="{Dark}">
  <main style="--primary: #006457;
               --primary-foreground: #fff">
    <Button>Save</Button>
  </main>
</Theme>

Bind Dark to the C# state shown below.

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

The zip puts the same color overrides in a stylesheet. Run it and click Toggle theme. The background switches from light to dark while the button stays teal. Click again to restore light mode. On Linux, the window needs a graphical session and Vulkan.

Full program

Put the files below beside the .NET 10 console project. OneUI generates the other half of ThemePage from the markup and implements its [State] property.

ThemePage.html binds the kit’s Dark parameter to the C# Dark state and wires the button to Toggle:

HTML
@using OneApp.Kit
<Theme Dark="{Dark}">
  <main class="palette">
    <h1>{Mode}</h1>
    <Button Id="toggle" OnClick="{Toggle}">Toggle theme</Button>
  </main>
</Theme>

ThemePage.css sets the teal button background, white button text and corner radius, while keeping the kit’s default page colors for each mode:

CSS
.palette {
  padding: 24px;
  min-height: 100vh;
  box-sizing: border-box;
  background: var(--background);
  color: var(--foreground);
  --primary: #006457;
  --primary-foreground: #ffffff;
  --radius: 0.5rem;
}
.palette h1 { margin-top: 0; }

ThemePage.cs:

C#
using OneUI.Components;
 
public partial class ThemePage
{
    [State] private partial bool Dark { get; set; }
    private string Mode => Dark ? "Dark mode" : "Light mode";
 
    private void Toggle()
    {
        Dark = !Dark;
        Console.WriteLine(Mode);
    }
}

Program.cs:

C#
using OneApp;
 
using var host = AppHost.Create(new AppOptions(new ThemePage())
{
    Title = "ThemeDemo",
    Width = 520,
    Height = 360,
});
host.Run();

Two clicks print:

Text
Dark mode
Light mode

Light mode with a teal Toggle theme button

Dark mode with the same teal Toggle theme button

If the button keeps the kit’s default colors, check that it’s inside the element that declares --primary, not just elsewhere under Theme.

Variations

Start in dark mode by initializing the state in ThemePage.cs; the toggle still lets the user switch:

C#
public ThemePage() => Dark = true;

The short answer

Wrap the root component in <Theme Dark="{Dark}"> and set Dark from your C# code. Override colors on an element inside Theme so the kit controls below it inherit the new values. The page can use the dark background and foreground while the main button keeps the same brand color in both modes.

How do I set dark mode in a C# desktop app?

In OneApp’s kit, Dark is a Boolean parameter on Theme, not a switch on the OS window. Make a [State] property in the parent component and pass it to Theme. When the button changes that value, the kit adds or removes the dark class on its theme root. The class changes the default variables for the component subtree. The example doesn’t read or change the OS preference; it holds the setting for the life of this component. Set the initial state yourself if you want the app to open in dark mode.

How do I choose colors for a dark theme?

Decide which colors you want to keep from the kit and which you want to replace. --background and --foreground are the page pair. --primary and --primary-foreground are the default button pair. Change both halves of a pair together and check the light and dark views, including focus and hover. The example leaves --background and --foreground on the kit’s light and dark defaults, and overrides the button pair with teal and white on .palette.

Because CSS custom properties inherit, the button reads overrides from its nearest ancestor that sets them. Put the override on a shared wrapper to restyle its controls together, or on an inner section to target just that section. The override doesn’t change the kit defaults in other parts of the page. The kit token table lists the other pairs, including cards and popovers, and the tokens used for borders and focus rings.

All oneapp recipes