Bind the button’s active class to a C# boolean. The button keeps its base chip class.
dotnet add package RecursiveAI.OneUIRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
Complete Filter.html: the click handler in Filter.cs flips IsActive, which controls the active class.
<button class="chip" class:active="{IsActive}" on:click="Toggle">In stock</button>
The example is the full program below: unzip it and dotnet run.
Prints chip, chip active, then chip as the same button is clicked twice.
The full program
To build by hand, run dotnet new console with the .NET 10 SDK, then run the install command above. Save Filter.html, Filter.cs, and Filter.css next to your project file. OneUI pairs the template with the class and implements the partial state property. The download pins OneUI 1.1.0.
Filter.cs flips IsActive when the button’s click handler runs:
using OneUI.Components; public partial class Filter { [State] public partial bool IsActive { get; set; } private void Toggle() => IsActive = !IsActive; }
Filter.css defines the base style and the style that applies only when active is present:
.chip { padding: 4px 12px; } .active { font-weight: bold; }
Replace the starter Program.cs with the code below. It clicks the button through an in-memory document and prints the node ID and class list each time:
using OneUI.Components; using OneUI.Components.Reference; var dom = new ReferenceDocument(); var ui = new UiContext(dom); dom.Context = ui; ui.Mount(new Filter()); PrintClasses(); dom.Click("button"); PrintClasses(); dom.Click("button"); PrintClasses(); void PrintClasses() { var button = dom.Require("button"); var classes = button.GetAttribute("class"); Console.WriteLine( $"Node {button.Id}: {classes}"); }
Node 2: chip Node 2: chip active Node 2: chip
The node ID stays 2: neither click recreates the button.
If the binding names a property that doesn’t exist, the markup generator reports it at build time; see markup diagnostics.
Variations
Invert the condition when the class should be present by default and removed on the first click:
<button class="chip" class:active="{!IsActive}" on:click="Toggle">In stock</button>
Related recipes
The short answer
Write class:active="{IsActive}" on the element and declare IsActive as a boolean [State] property in the matching C# component. OneUI adds active when the expression becomes true and removes it when it becomes false. Other classes on the element stay in place.
How do you conditionally add a CSS class without replacing other classes?
Put classes that always apply in the normal class attribute. In Filter.html, class="chip" sets the baseline and class:active controls just the extra class. The click handler only changes state; it doesn’t query the document or construct a class string. The class binding produces an AddClass or RemoveClass change for that node. Setting the whole class attribute instead would replace the class list.
A single class binding is useful when base styles and conditional styles have different jobs. Here the padding is the same whether the filter is selected or not, and only the font weight changes. Write the stylesheet against .chip and .active separately. The in-memory document shows the class list; a rendering backend applies the corresponding styling.
Should you toggle a class or conditionally render a new element?
Keep one element and toggle its class when only its appearance changes. In the output, node 2 remains node 2 across both clicks; the binding changes a class, not the tree. That also means the element and its click listener don’t need to be recreated to switch the style. Use a conditional template when the content should be absent from the document, such as a panel that has no role until a user opens it. A class binding by itself doesn’t remove content: any hiding behavior comes from the CSS you attach to the class.