---
title: "Dark mode and theme colors in a C# app"
url: https://recursiveai.org/oneapp/cookbook/theme-desktop-ui-kit/
description: "Switch a OneApp desktop window between light and dark mode and change button colors with CSS theme tokens. Download and run the .NET 10 example."
version: "1.0.0"
---

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

```shell
dotnet add package RecursiveAI.OneApp.Kit
dotnet add package RecursiveAI.OneApp
dotnet add package RecursiveAI.OneUI
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (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](https://recursiveai.org/oneapp/cookbook/theme-desktop-ui-kit/theme-desktop-ui-kit.zip) 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`:

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

```csharp
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](https://recursiveai.org/oneapp/images/theme-desktop-ui-kit/light.png)

![Dark mode with the same teal Toggle theme button](https://recursiveai.org/oneapp/images/theme-desktop-ui-kit/dark.png)

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:

```csharp
public ThemePage() => Dark = true;
```

## Related recipes

- [Show a confirmation dialog and toast](https://recursiveai.org/oneapp/cookbook/show-dialog-and-toast-with-ui-kit/)
- [Package a C# desktop app for Linux](https://recursiveai.org/oneapp/cookbook/package-dotnet-desktop-app-for-linux/)

## 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](https://recursiveai.org/oneapp/docs/kit/#tokens) lists the other pairs, including cards and popovers, and the tokens used for borders and focus rings.
